Switch Statement

Hi,

I'm trying to use a Switch statement and I figure it can be a powerful script, but I've only found a few limited uses for it as I cannot figure out what kinds of things I can use as the Switch.

What can I type (GUI not Code View) into the Switch input box? I ask because I'd like to get input from the player and then use that input to select an option, where each option is a "key". However, I either get an error because the thing used as the Switch isn't properly named or it gives me an issue with the keys.

For a simple example, I'd like it to take player input of a number between 0-3. Then each key corresponds to each integer, and the game will display a simple message based on the number inputted. How would I go about making this simple example happen... in the GUI and NOT in code view. I specify, because the last few answers for my other problems have been exclusively in code view which isn't helpful to me right now, as I do not understand JavaScript.


If you understand the code, it is easer... but I understand your problem.
A lot of my code I start in UI, then finish in code...
for testing an idea, create a new "game" with just what you are testing...
If it works, copy it to your program...
If not, you haven't wrecked your existing program... (Can't tell you how many times I've done that!!!)
Create a new game... or copy it and work with the copy...
in your room, add new script...
(I assume your get input works)
select switch...
(not the switch on/off)
Switch [ result] from your getinput...
key: 0
script: whatever your message is.
key: 1
script: message #2
Then look at it in code view, it will be easer to add to it from there.
One thing to watch for...
getinput returns a string, if you key is a number Quest does not like it.
use ToInt(number) to make is a number or
make the key a string...
key: "0"
either should work.


Nice "slith" for your avatar.

The issue I'm having is what to type into the switch field. I've learned that sometimes the errors the program spews aren't fully reliable, but it doesn't like what I put into the switch field.

I understand how the underlying code works, but I don't know what to type into the field. So in your GetInput() example, would you type "result" into the Switch field? As for GetInt, would you type "integer" in the Switch field?

How about an image, expressing my problem... I've circled in RED what I mean by Switch Field. I gave two examples; one using the GetInput function, and one where it is just a Switch statement.

https://www.mediafire.com/?1gcg3z7lf1sgbdt


the switch text box' field:

VARIABLE_(AKA: INDIRECT_VALUE_INPUT)
or
LITERAL_(AKA: DIRECT_VALUE_INPUT)

(where-ever I have 'VARIABLE' for the switch text box field, just remember/be-aware-that, it can also be a LITERAL, instead of a VARIABLE)

which the Cases' text boxes (or whatever it is) holds/are-given the Arguments/Values which is check-compared/check-matched with the VARIABLE'S Value, if it matches up (true: do the nested script-s, and end the Switch block: aka, skip all remaining cases, including the default-case if you got it) or not (false: jump to the next case or default/case or end the Switch block if no other cases nor default-case)


so, the switch text box can have ANY VARIABLE (built-in or custom, Variable or Attribute or Parameter), some examples:

ask ("Are you alive?") {
  switch (result) { case (true) { /* scripting */ } case (false) { /* scripting */ }  ETC_MORE_OR_LESS_CASES OPTIONAL: default { /* scripting */ } }
}

// or --------------------------------------------------

game.EXAMPLE_STRING_ATTRIBUTE = "password"
switch (game.EXAMPLE_STRING_ATTRIBUTE) { case ("password") { /* THIS SCRIPT IS DONE/EXECUTED/RUN */ } case ("open sesame") { /* THIS SCRIPT IS SKIPPED AS ITS CASE IS SKIPPED */ } ALL_REMAINING_CASES_AND/OR_DEFAULT_CASE_ARE_SKIPPED }

vs (example of how the check-matching of the Cases' Arguments to the VARIABLE'S Value)

game.EXAMPLE_STRING_ATTRIBUTE = "open sesame"
switch (game.EXAMPLE_STRING_ATTRIBUTE) { case ("password") { /* THIS SCRIPT IS SKIPPED AS ITS CASE FAILED THE CHECK-MATCHING */ } case ("open sesame") { /* THIS SCRIPT IS DONE/EXECUTED/RUN */ }  ALL_REMAINING_CASES_AND/OR_DEFAULT_CASE_ARE_SKIPPED }

// or -----------------------------------------------------

show menu ("Sex?", split ("male;female", ";"), false) {
  switch (result) { case ("male") { /* scripting */ } case ("female") { /* scripting */ }  ETC_MORE_OR_LESS_CASES OPTIONAL: default { /* scripting */ } }
}

// or ---------------------------------------------------------

<object name="katana">
</object>

<object name="claymore">
</object>

<function name="example_function" parameters="sword_object_parameter">
  switch (sword_object_parameter) { case (katana) { /* scripting */ } case (claymore) { /* scripting */ }  ETC_MORE_OR_LESS_CASES OPTIONAL: default { /* scripting */ } }
  }
</function>

the 'get input()', 'show menu(Parameters) {pop up window menu}', 'ShowMenu(Parameers) {in-line, aka: in the left big text box, hyperlink menu}', 'ask(Prompt Parameter) {popup window yes:true/no:false boolean menu}', and 'Ask(Prompt Parameter) {in-line, aka: in the left big text box, hyperlink yes:true/no:false boolean menu}' Functions/Scripts, all automatically (hidden from you) store your typed-in or selected input into the built-in 'result' Variable VARIABLE, which can then be used for whatever you want.

for example:

<game name="example_game">
  <attr name="start" type="script">
    msg ("Sex? (Type in the number of/for your choice)")
    msg ("1. male")
    msg ("2. female")
    get input {
      // quest automatically (hidden from you) does this for you:
      // result = YOUR_TYPED_IN_OR_SELECTED_INPUT
      switch (result) {
        case ("1") { // checks the Case's Argument: "1", if it matches up with the Value (YOUR_TYPED_IN_OR_SELECTED_INPUT) of the 'result' Variable VARIABLE
          player.sex_string_attribute = "male"
        }
        case ("2") { // checks the Case's Argument: "2", if it matches up with the Value (YOUR_TYPED_IN_OR_SELECTED_INPUT) of the 'result' Variable VARIABLE
          player.sex_string_attribute = "female"
        }
        default {
          msg ("Wrong input, try again")
          wait {
            ClearScreen
            do (game, "start")
          }
        }
      }
      msg ("Player's Sex: " + player.sex_string-attribute)
    }
  </attr>
</game>

ask if you got any questions about anything, or if you need anything explained.


The thing that goes in the switch field is whatever can take the various values. In your example, that is indeed the "result" variable (no quotes), as that could be "heal" or whatever.

Even better, put "LCase(result)" (no quotes), and then whatever the player types will go to lower case, so it will match HEAL too.

At the end of the switch is an else (or default; cannot remember exactly), and you should use that in this instance to catch anything else the player may type.

There is documentation (but I now realise there was no link to it).
http://docs.textadventures.co.uk/quest/multiple_choices___using_a_switch_script.html

And this is kind of related (but uses if/else rather than switch).
http://docs.textadventures.co.uk/quest/asking_a_question.html

By the way, you might want to consider showing a menu of options for spells.
http://docs.textadventures.co.uk/quest/guides/showing_a_menu.html


result is the answer for getinput...
In Basic, it would make more sense...
input "What is your sex", player.sex
input "What is your age", player.age

but in Quest, result is always the variable that stores the answer.
Kinda like:
msg("What is your sex)
get input {
player.sex=result
}
msg ("What is your age?")
get input {
player.age=result
}


continuing Dark Lizard's post, more about quest's 'switch' Script/Function's switch-text-box-field:

you can use either 'result' or (for example) 'player.sex', for the Argument/Value/Input of the (put/typed/coded into the) switch-text-box-field, for example:

msg("What is your sex)
get input {
  // (input handling is missing obviously, so just pretend that the user correctly inputted/typed-in, either: 'male' or 'female')
  player.sex=result
  // switch (result) { /* blah cases and their scripting */ }
  // and/or
  // switch (player.sex) { /* blah cases and their scripting */ }
}

here's an example of a (Integer) LITERAL (using 3):

switch (3) {
  case (1) {
    // blah scripting
  }
  case (2) {
    // blah scripting
  }
  case (3) {
    // blah scripting
  }
  case (4) {
    // blah scripting
  }
  case (7) {
    // blah scripting
  }
  default {
    // blah scripting
  }
}

// -----------------------------------

using 7:

switch (7) {
  case (1) {
    // blah scripting
  }
  case (2) {
    // blah scripting
  }
  case (3) {
    // blah scripting
  }
  case (4) {
    // blah scripting
  }
  case (7) {
    // blah scripting
  }
  default {
    // blah scripting
  }
}

example of a String LITERAL:

switch ("dragon") {
  case ("king") {
    // blah scripting
  }
  case ("princess") {
    // blah scripting
  }
  case ("dragon") {
    // blah scripting
  }
  case ("sword") {
    // blah scripting
  }
  case ("wizard") {
    // blah scripting
  }
  default {
    // blah scripting
  }
}

errr... (whoopsy) .... note that (talking to myself, lol)... LITERALS are completely pointless for any check-matching (such as 'if', 'switch', and etc), lol


How do I go about creating my own variables then?

result definitely works for the switch field, but now I get errors for unknown variable / object... example:

Slap Command:
GetInput()
Switch (result)
key 1 = face
key 2 = back

but as those aren't objects, how do I go about creating face or back as variables?


Or if I have created those as objects, and I have a script that posts a message, why is it that the message doesn't get posted at all, no error or no object is not visible messages, just nothing at all.

So in the above example:

if you type in face, and you have it to say that for the script "You slap their face", in this situation, nothing gets posted.


you're probably just working with Strings, so your Cases' Values/Arguments must have double quotes (anything in double quotes is a String Value), for example:

(DO USE double quotes in the Cases)

get input {

  // result = "YOUR_TYPED_IN_INPUT" // with 'get input' Script/Function, 'result' is always a String Variable, which (MUST and indeed does) always holds a String Value

  switch (result) {
    case ("katana") {
    }
    case ("claymore") {
    }
  }
}

if you're working with Objects, the Objects must actually exist and/or still exist:

(do NOT use double quotes in the Cases!)

// created/added and thus existing Object:
<object name="katana">
</object>

// created/added and thus existing Object
<object name="claymore">
</object>

get input {

  // result = "YOUR_TYPED_IN_INPUT" // with 'get input' Script/Function, 'result' is always a String Variable, which (MUST and indeed does) always holds a String Value

  player.right_hand_object_attribute = GetObject (result) // the 'GetObject(String Parameter)' Function takes in a String and converts it (if it can --- if it exists as an Object) into an Object (Object reference/pointer), which is then being stored into my custom 'player.right_hand_object_attribute' Object Attribute 

  switch (player.right_hand_object_attribute) {
    case (katana) {
    }
    case (claymore) {
    }
  }
}

see these links (ask if you need help with anthing):

http://textadventures.co.uk/forum/samples/topic/5559/attributes-and-if-script-guide-by-hk
(you should read the whole thing, but specfically, scroll down a bit to the bolded: the two SUPER SCRIPTS, which especially when used together, lets you do 90% of everything that you want to do in~for your game: part/section)

http://textadventures.co.uk/forum/quest/topic/5387/i-really-need-help#37375 (for the desktop/offline Text Adventure, a step by step guide walkthrough demo game on the basics of Attributes)


you can also take a look at Pixie's 'character creation' guide:

http://docs.textadventures.co.uk/quest/guides/character_creation.html

and here's my thread back when I was trying to learn this quest GUI/Editor and coding stuff myself:

http://textadventures.co.uk/forum/quest/topic/3348/noobie-hks-help-me-thread (scroll down some posts, to get to the character creation part)


computers are stupid, so they must know what they're working with, and thus Data Types are very important, and you have to be very careful about not-mismatching them, for example:

no human and no computer can do mis-matched Data Type operations, for examples:

(math/arithmetic) addition operation:
STRING + INTEGER = ERROR! Can't compute!

"river" + 77 = impossible to compute!


same with VARIABLES and their Values, for example:

STRING_VARIABLE = INTEGER_VALUE // ERROR! you can NOT store an Integer Value into a String VARIABLE!

game.my_string_attribute = 23 // ERROR!

game.my_string_attribute = "23" // NO error, we anything in double quotes, is a String Value


game.my_integer_attribute = "hi" // ERROR! you can't store a String Value into an Integer Attribute

game.my_integer_attribute = "34" // ERROR! you can't store a String Value into an Integer Attribute

game.my_integer_attribute = 34 // NO error


game.my_boolean_attribute = "true" // ERROR! you can't store a String Value into a Boolean Attribute

game.my_boolean_attribute = "false" // ERROR! you can't store a String Value into a Boolean Attribute

game.my_boolean_attribute = true // NO error

game.my_boolean_attribute = false // NO error


<object name="katana">
</object>

game.my_object_attribute = "katana" // ERROR! You can't store a String Value into an Object (reference/pointer) Attribute
vs
game.my_object_attribute = katana // NO error

vs

game.my_object_attribute = katana // ERROR! There is no (no existing and/or no still existing) 'katana' Object to be stored into the Object Attribute


ask if you need help with anything or need anything explained or are confused wth anything, the syntax/format and Data Type semantics is very confusing if you don't already know coding and this coding stuff already.


Slap Command:
GetInput()
Switch (result)
key 1 = face
key 2 = back
but as those aren't objects, how do I go about creating face or back as variables?

if you type in face, and you have it to say that for the script "You slap their face", in this situation, nothing gets posted

GetInput() is returning "face"
Switch is looking for "1" or "2"...
if you type 1, then you will get "You slap their face".
if you had key ("face"), then you would get "You slap their face".


Okay, by adding the "" the switch now works as expected, so thanks for the help.

I'll probably have more questions, related to specific things, but this solves my current problem.

@hegemonkhan; I do know some stuff about variables, functions, and variable types, having tried to self-teach myself C++... though pointers and their evil cousin, linked lists, still eludes me. On the topic of C++, does the coding here for Quest allow for "global" variables or even type-casting (ie type-casting a variable of type integer to be added to a variable of type double)


C++ is a difficult language to start programming with, so good luck with that!

Quest has no global variables. All data is stored as attributes of objects. Data you need global is best done as an attribute of the game object.

Quest will automatically type cast variables to strings or lists. You can convert to a number with ToInt or ToDouble, and check if that is possible with IsInt and IsDouble.

mystring = "You have " + player.eggcount + " eggs."
mylist = "One" + NewStringList() + "Two"
myint = ToInt("42")


I too have trouble with Abstract Data Structures (linked lists, dictionaries, maps, etc).


I personally like C++'s syntax and features compared to Java (though it is hard to learn, C++ has a lot of stuff to learn!, and especially with working with arrays as you got no protections for your C++ program like in Java with its GC: garbage collecting, you have to be aware of what you're doing. Though, I do hope that, your computer is protected from memory links and dangling pointers caused/created by you messing up in C++ software ... gulps... I hope! C++ software better protect the computer from any screw ups you do in it!)


I understand pointers/references, but the hard part is understanding and putting together system/structure/design that works. My brain has trouble with this abstract OOP/OOD, as it's still oriented to the more simple proceedural coding.

in quest, the 'name' String Attribute is the ID for quest, and thus it is a reference/pointer, and thus any VARIABLE holding the 'name' of some Element, is a reference/pointer, which really helps you understand the concept and usage of references/pointers, if they're still confusing for you (again though understanding pointers/references and keeping track of them - which is literally all that ASSEMBLY LANGUAGE is - as it deals with memory, is one thing, it's another thing to design a system that uses abstract data structures that use references/pointers).

for example of using Object Attributes (one type of reference/pointer in quest):

<object name="player">
  <attr name="right_hand" type="object">unarmed</attr> // initial/starting state of the pointer/reference Object Attribute : you're holding/equiping the 'unarmed' weapon Object in your right hand
</object>

<object name="unarmed">
  <attr name="damage" type="int">1</attr>
</object>

<object name="sword">
  <attr name="damage" type="int">50</attr>
</object>

// scripting:

player.right_hand = sword // setting/re-setting/equiping the 'sword' weapon Object: new/current referenced/pointed-to Value (an 'Object' Element/OBJECT)
player.right_hand = unarmed // setting/re-setting/equiping the 'unarmed' weapon Object: new/current referenced/pointed-to Value (an 'Object' Element/OBJECT)

player.damage = player.right_hand.damage // using this reference/pointer, to apply the damage of the weapon that this pointer/reference is currently pointing/referencing to, to being the damage done by the player:
// player.right_hand = unarmed
// unarmed.damage = 1
// player.damage = right_hand.damage = unarmed.damage = 1
// player.damage = 1
// or
// player.right = sword
// sword.damage = 50
// player.damage = right_hand.damage = sword.damage = 50
// player.damage = 50

as Pixie stated, quest's global VARIABLES are Attributes (so long as the Object containing them exists and/or still exists, of course):

NAME_OF_OBJECT.NAME_OF_ATTRIBUTE = VALUE_OR_OBJECT

whereas, quest's local VARIABLES, are Variables:

NAME_OF_Variable = VALUE_OR_EXPRESSION


Type Casting in quest is done via the conversion Functions:

STRING = ToString (INTEGER_OR_DOUBLE_OR_OBJECT_REFERENCE/POINTER_OR_BOOLEAN)
INTEGER = ToInt (STRING)
DOUBLE (quest's FLOAT/FLOATING_POINT/decimal_numbers) = ToDouble (STRING)
OBJECT_REFERENCE/POINTER = GetObject (STRING)

checking of Data Types (using 'if' Script with these):

(negation and matching can be applied to these as well)
IsInt // if (IsInt (VARIABLE)) { /*scripting */ } // this (to the left) is understood (defaulted/shortened) as being this: IsInt // if (IsInt (VARIABLE) = true) { /*scripting */ } // if (not IsInt (VARIABLE)) { /*scripting */ } // if (IsInt (VARIABLE) = false) { /*scripting */ } // if (IsInt (VARIABLE) <> true) { /*scripting */ } // etc etc etc combinations/syntaxes
IsNumeric // numeric: integer or double
IsDouble

TypeOf // requires matching: if (TypeOf (VARIABLE) = "DATA_TYPE") { /* scripting */ }

checking if Object contains Attribute itself (the Value of the Attribute is ignored):

(again, used with the 'if' Script, NO matching is used)
HasAttribute
HasBoolean
HasInt
HasDouble
HasString
HasObject
HasScript

DoesInherit // matching IS used: if (DoesInherit (NAME_OF_OBJECT.NAME_OF_Inherited_Attribute) = "NAME_OF_Object_Type") { /* scripting */ }

checking if Object contains Attribute itself AND checks/matches if it has specified Value (returns false/null if either of these conditions fail, otherwise, returns the Value):

GetAttribute
GetInt // if (GetInt (player.strength) > 66) { { /* scripting */ }
GetDouble
GetString
GetObject
GetBoolean


and there's many more Functions... too... such as all the ones dealing with the List/Dictionary Attributes....


links:

http://docs.textadventures.co.uk/quest/

http://docs.textadventures.co.uk/quest/elements/
http://docs.textadventures.co.uk/quest/elements/object.html

http://docs.textadventures.co.uk/quest/scripts/
http://docs.textadventures.co.uk/quest/functions/ (categorical ordering)
http://docs.textadventures.co.uk/quest/functions/index_allfunctions.html (aphabetical ordering)
http://docs.textadventures.co.uk/quest/scopes.html (these are nicely separated as their own link, but can also be found in the 'functions' link too)


the 'switch' and 'if' block Scripts/Functions are exactly the same thing:

(so, it's just aesthetics: which design you like better and/or that works better for the coding situation, as functionally, they do exactly the same thing, the 'switch' Script/Function can handle the same or nearly-the-same complexity as can be handled by the 'if' Script/Function, as Pertex/Pixie/Jay demonstrated/shown to me, but I find doing complex scripting easier with the 'if' Script/Function)

also, you can do complexity with the text processor commands too, as you can nest multiple text processor commands... as Pertex (or maybe Pixie/Jay, but I think it was Pertex for this stuff) shown/demonstrated to me

the 'if/else if/else' if block:

if (VARIABLE = VALUE_OR_EXPRESSION_1) {
  // blah scripting
} else if (VARIABLE = VALUE_OR_EXPRESSION_2) {
  // blah scripting
} else if (VARIABLE = VALUE_OR_EXPRESSION_3) {
  // blah scripting
}  else {
  // blah scripting
}

the 'switch/case/default' switch block:

switch (VARIABLE) {
  case (VALUE_OR_EXPRESSION_1) {
    // blah scripting
  }
  case (VALUE_OR_EXPRESSION_2) {
    // blah scripting
  }
  case (VALUE_OR_EXPRESSION_3) {
    // blah scripting
  }
  default {
    // blah scripting
  }
}

// ---------

so the:

switch (VARIABLE), matches up with the first part (left of the OPERATOR) of the: if (VARIABLE ....)

case (VALUE_OR_EXPRESSION), matches up with the last part (right of the OEPRATOR) of the: if/else if (.... VALUE_OR_EXPRESSION) 

default, matches up with: else

if the (VARIABLE) is holding a String, then it's a String VARIABLE, and thus then you MUST HAVE the double quotes on the being-compared/matched-to VALUE/EXPRESSION (to make it a String Value/Expression): ("VALUE_OR_EXPRESSION")

if the (VARIABLE) is holding an Object, then it's an Object VARIABLE, and thus then you MUST NOT have double quotes on the being-compared/matched-to VALUE/EXPRESSION (making it an Object Value/EXPRESSION): (VALUE_OR_EXPRESSION)


how matching works:

(pretend that the names of my VARIABLES, makes/determine them as those Data Types of VARIABLES, for these examples/explanations)

(the VARIABLE'S DATA TYPE is actually determined by: the DATA TYPE of its Value when done in code, and if done in the GUI/Editor, you set/determine the VARIABLES' Data Type when you create/add it)

string_variable ="HK"
if (string_variable = "HK")
// DATA TYPE MATCHING:
// if (STRING = STRING)
// true: string_variable = STRING
// true: "HK" = STRING
// TRUE, thus continue below:
// STRING/TEXT MATCHING:
// if ("HK" = "HK")
// true: "H" = "H"
// true: "K" = "K"
// TRUE

string_variable ="HK"
if (string_variable = "HegemonKhan")
// DATA TYPE MATCHING:
// if (STRING = STRING)
// true: string_variable = STRING
// true: "HegemonKhan" = STRING
// TRUE, thus continue below:
// STRING/TEXT MATCHING:
// if ("HK" = "HegemonKhan")
// true: "H" = "H"
// false: "K" = "e" 
// FALSE

string_variable ="HegemonKhan"
if (string_variable = "HK")
// DATA TYPE MATCHING:
// if (STRING = STRING)
// true: string_variable = STRING
// true: "HK" = STRING
// TRUE, thus continue below:
// STRING/TEXT MATCHING:
// if ("HegemonKhan" = "HK")
// true: "H" = "H"
// false: "e" = "K"
// FALSE

string_variable ="HegemonKhan"
if (string_variable = "HegemonKhan")
// DATA TYPE MATCHING:
// if (STRING = STRING)
// true: string_variable = STRING
// true: "HegemonKhan" = STRING
// TRUE, thus continue below:
// STRING/TEXT MATCHING:
// if ("HegemonKhan" = "HegemonKhan")
// true: "H" = "H"
// true: "e" = "e"
// true: "g" = "g"
// true: "e" = "e"
// true: "m" = "m"
// true: "o" = "o"
// true: "n" = "n"
// true: "K" = "K"
// true: "h" = "h"
// true: "a" = "a"
// true: "n" = "n"
// TRUE

<object name="HK"></object>
string_variable = HK
if (string_variable = "HK")
// DOES 'HK' Object Exist?
// true: <object name="HK"></object>
// TRUE, thus continue below:
// DATA TYPE MATCHING:
// if (STRING = STRING)
// true: string_variable = STRING
// false: HK = Object
// FALSE

<object name="sword"></object>
object_variable = sword
if (object_variable = sword)
// DOES 'sword' Object Exist?
// true: <object name="sword"></object>
// TRUE, thus continue below:
// DATA TYPE MATCHING:
// if (Object = Object)
// true: object_variable = Object
// true: sword = Object
// TRUE, thus continue below:
// STRING/TEXT MATCHING:
// if ("sword" = "sword")
// true: "s" = "s"
// true: "w" = "w"
// true: "o" = "o"
// true: "r" = "r"
// true: "d" = "d"
// TRUE

object_variable = sword
if (object_variable = sword)
// DOES 'sword' Object Exist?
// false: THERE IS NO: <object name="sword"></object>
// FALSE

<object name="sword"></object>
object_variable = sword
if (object_variable = "sword")
// DOES 'sword' Object Exist?
// true: <object name="sword"></object>
// TRUE, thus continue below:
// DATA TYPE MATCHING:
// if (Object = Object)
// true: object_variable = Object
// false: "sword" = STRING
// FALSE

<object name="sword"></object>
object_variable = "sword"
// does the 'object_variable' Object VARIABLE, contain an Object Value/Expression, ???
// false: "sword" = Object
// FALSE

My $.02...
"If" is good for 1 or 2 compares...
"Switch" is good for more than 2, and easer to modify...


How do you do numerical keys with Switches though, when you are looking for a range of numbers.

ie Example below of what I mean:

if = (player.Health > 100) and (player.Health < 150)
elseif = player.Health > 149
elseif = (player.Health < 100) and (player.Health > 1)

When it is a range of numbers, I find that it can only be done with tons of If statements. Same applies when dealing with objects with flags, you need a separate if statement for each flag, particularly if that flag requires exclusivity. Such as:

shoot arrow at what body location
if object has flag AtFace
... damage face with arrow
if object has flag AtBelly
... damage belly with arrow
etc...

you'd have to use if statements for each of those situations... same applies to a switch on/off functionality... as flags afaik aren't objects, or variables or strings.


By matching against true.

switch (true) {
  case (health < 1) {
    msg("You are dead!")
  }
  case (health < 100) {
    msg("You are not feeling so good")
  }
  default {
    msg("Doing fine!")
  }
}

Quest will stop at the first case that matches, do that, and then stop, so the second case only needs to check if health is less than 100, not that it is over 0.


Thanks Pixie. That helps a lot.


Problem though. Quest keeps rearranging my switch keys, and so when I initially code it looks like this:

x > 4 ... msg
x > 2 ... msg
x > 0 ... msg

but since it re-arranged it, it now looks like:

x > 0 ... msg
x > 4 ... msg
x > 2 ... msg

How do re-rearrange them or do I need to use a double Boolean statement?

ie x > 0 and x < 3
ie x > 2 and x < 5


I have never known it do that, and I would consider that to be a bug.

Are you using the desktop version or the web version? Could you say exactly what steps you did?

To change the order, you would need to click on the Code view button, and rearrange . If you want to paste the code into a post, we could do that for you if you are not sure (and it might help workout why it rearranged).


Desktop Version

Its possible that by copying the entire Switch and pasting it again, so that I would have to do the minimal amount of retyping, as the copied Switch was going to do something similar to the initial one but slightly and specifically different. I do know that sometimes copying and pasting does screw some things up... such as emptying object has flag fields or causing some custom if expressions to get mangled; ie if I want to check for two GetBooleans with And/Or = GetBoolean() and GetBoolean(). Just something one needs to check with a copy/paste I suppose. I do recall in an old Visual Basic class the teacher saying something along the lines of be careful copy/pasting code...and I suppose these are examples of when one should be careful.

Basically what is was doing was that a character (orc) has an attribute FaceBlood, LowerTorsoBlood and an attribute BellyBlood. When they have taken a hit to the face, FaceBlood gets +1; to the belly, BellyBlood +1. So when orc.FaceBlood > 0; case = description. orc.FaceBlood > 2; case = description or orc.FaceBlood > 4; case = description. Each description would be descriptive of how much blood was flowing from that area, if the player did the command Look at Orc. So, since the switch was similar, but slightly different for BellyBlood or LowerTorsoBlood, I copied the FaceBlood switch, renamed the Keys to BellyBlood or LowerTorsoBlood, and redid the descriptions. THEN I saved the file and played the game, and noticed in game that despite having more than 8 for FaceBlood, it was only showing the description for the orc.FaceBlood > 2 and noticed the ordering for FaceBlood, BellyBlood and LowerTorsoBlood was mangled... and thus had to use double Booleans to ensure that whatever order they got put in, they'd still produce the correct logic for the situation.

I did find that doing double Booleans ensured that it would get the correct 'case' but a bunch of annoying work to do so. Had another situation where there was 6 cases, and after adding double Booleans to it, the order got mixed up again. Annoying, but I suppose its just an issue with the program... like a bunch of other ones I've encountered... like the annoying and useless arithmetic isn't defined for Objects or Int32... which I've figured out to mean that I misspelled an object name or an object attribute, and by properly spelling it the error goes away.


I use the desktop version a lot, and have never seen it do that. I do tend to type in code rather than through the GUI, but I would not have thought that was an issue.


we can always try to find the 'sorting' function within the underlying quest code... unless it's even worse... using some computer system control/code or some other 3rd party source for the sorting... meh...

otherwise... you can always just use the 'if' Script/Function/block.... hehe

or, you're just stuck with using the 'and' to create the within-range for each case... if you're dead-set on using the 'switch' Script/Function/block


Actually, I have also had a similar issue when using switches where I would order the various cases in a certain way, but the Quest program would reorder them in a different way. I haven't tried reordering them through the direct ASLX code yet. I'm also using the desktop version of Quest. I have never had a problem with the If-Then scripts though.


This topic is now closed. Topics are closed after 60 days of inactivity.

Support

Forums