Multiple choices - using a "switch" script

From QuestWiki
Jump to: navigation, search

A "switch" is like an "if" script, but it caters for more options without you having to to add a load of "else if" entries.

In this example, we'll add a telephone to the lounge and set up a "dial" command. The player will be able to dial a variety of different numbers:

First, add a telephone to the lounge and give it a sensible description. You should also go to the Object tab and give it an "Other name" of "phone". Next, add a command "dial #text#". Whenever the player types "dial", the text that follows will be put into the "text" string variable.

In the command script, add "switch". You can now enter a Switch expression, and any number of cases. Here, we want to compare the variable "text" against different values, so the Switch expression is "text". Each of the cases that we add will be compared against this.

Click Add to add the first case. Enter the expression "999".

Click OK. The Script Editor will now be displayed, ready for you to enter the script that should run if #text# is equal to "999". Print the message "There's no need to call the police now." and close the window.

Follow the same process to enter the responses for the other phone numbers. When you reach the bottom of the list, add script to the "Default" section to print "Sorry, wrong number!". This will handle any other input that wasn't matched by our cases.

When you have finished, the Script Editor should look like this:

Switch.png

This is much easier to read, edit and add to than if we had used lots of "if" and "else if" scripts.

Launch the game and dial a few numbers to check that you see the correct response.

Tutorial

Beginners

  1. Introduction
  2. Creating a simple game
  3. Interacting with objects

Intermediate

  1. Using scripts
  2. Custom attributes
  3. Custom commands
  4. More things to do with objects
  5. Using containers
  6. Moving objects during the game

Advanced

  1. Using timers and turn scripts
  2. Changing templates
  3. Text formatting
  4. Using lockable exits
  5. Using lockable containers
  6. Using walkthroughs
  7. Multiple choices - using a "switch" script
  8. Debugging your game
  9. Releasing your game

Elite

  1. Creating functions which return a value
  2. Using inherited types
  3. Using libraries
  4. Using Javascript
Personal tools
Namespaces
Variants
Actions
Navigation
Quest Documentation
Toolbox