How to make an if statement respond to a player input?

CarmenK72
01 Sept 2016, 22:34

I want an object to kill a player if the player doesn't run away from it, so I used the 'If: expression run' script for success, and 'else: finish game' script for failure. However, whenever I get to that point in my game, the player has no way of even inputting "run", as it gives me an error message. Does anyone know what the problem is?


hegemonkhan
02 Sept 2016, 00:42
there's two ways to get typed-in input:

1. the 'get input' Script/Function ( http://docs.textadventures.co.uk/quest/scripts/get_input.html )
2. Commands ( http://docs.textadventures.co.uk/quest/elements/command.html )

Commands are a bit more complex but also powerful/useful, but if you want to jump into using/learning them, that's fine too.

in the GUI/Editor, to find/use the 'get input' Script/Function:

'whatever' Element ( http://docs.textadventures.co.uk/quest/elements/ ) (Verb, Object's Script Attribute, Functions, Commands, Turnscripts, Timers) -> run as script -> add new script -> 'output' section/category -> 'get input' Script

then, 

-> add new script -> 'scripts' section/category -> the 'if' Script/Function -> [EXPRESSION] // this let's you code/type in what you want -> (see below)

if [EXPRESSION] result = "run"

-> then,

->-> add new script -> 'output' section/category -> 'print a message' Script/Function -> [EXPRESSION] -> (see below)

print [EXPRESSION] "You run away just in time, saving your life."

-> add else -> add new script -> 'output' section/category -> 'print a message' Script/Function -> [EXPRESSION] -> (see below)

print [EXPRESSION] "You didn't run away in time, and were killed.")

-> add new script -> 'output' section/category -> 'print a message' Script/Function -> [EXPRESSION] -> (see below)

print [EXPRESSION] "GAME OVER")

-> add new script -> '???' section/category -> 'finish the game' Script/Function

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

both the 'get input' and 'show menu / ShowMenu' Scripts/Functions, automatically (hidden) from you, do this:

result = YOUR_TYPED_IN_INPUT_OR_YOUR_MENU_SELECTED_CHOICE

// Programming operation: ASSIGNMENT ( = ):
// an ASSIGMENT operation is not the same as math's EQUAL TO operation.
// ASSIGNMENT: STORE/SAVE the result of the expression on the RIGHT SIDE of the ASSIGMENT OPERATOR ( = ) INTO the VARIABLE on the LEFT SIDE of the ASSIGNMENT OPERATOR ( = )
// so for our example:
// result <------- YOUR_TYPED_IN_INPUT_OR_YOUR_MENU_SELECTED_CHOICE
// the 'result' Variable VARIABLE now "holds" your 'YOUR_TYPED_IN_INPUT_OR_YOUR_MENU_SELECTED_CHOICE'
// so let's say I typed in: run
// result = "run"
// result <-------- "run"
// the 'result' Variable VARIABLE now holds/contains the String Value of "run", which makes it (the 'result' Variable VARIABLE a String Variable VARIABLE)
// think of like algebra and algebraic substitution:
// x = 100
// we can then use that 'x' variable:
// y = x + 50
// y = (x:100) + 50
// y = (100) + 50
// y = 150
//
// a programming example of how it's the same as algebraic substitution:
//
// our math 'x=100' variable is going to correlate to this programming variable (an Integer Attribute VARIABLE): player.x = 100
// our math 'y' variable is going to correlate to this programming Script/Function (an operation - so it's not really a variable per se): msg ()
// our math statement of 'y=x+50'is going to correlate to our programming statement of:
// msg ((player.x + 50)) // hopefully this is able to work... lol
// which would then (again, hopefully it works, lol) output:
// 150
//
// however, since it's an ASSIGMENT OPERATOR and not an EQUAL TO OPERATOR, if you did this, you'd get an error, using the math example above for this example of an error:
// x + 50 = y --------> ERROR!
// pretend that you were coding it in directly, and pretending you could set it up the 'get input' to do this instead:
// YOUR_TYPED_IN_INPUT_OR_YOUR_MENU_SELECTED_CHOICE = result -------> ERROR!

which, you can then use that built-in 'result' Variable VARIABLE, for whatever you want... in the example above, we used the 'result' Variable VARIABLE for a "string comparison" for our 'if' Script's/Function's condition:

if (result = "run") { /* scripting */ }
// conceptually:
// let's say I typed in: run
// if ("(result:run)" = "run")
// if ("(run)" = "run") -> TRUE, and thus do its 'then/TRUE' scripting (our 'msg' Script/Function: print [EXPRESSION] "You run away just in time, saving your life.")
// whereas, let's say I typed in: foo
// if ("(result:foo)" = "run") { /* scripting */ }
// if ("(foo)" = "run")
// if ("foo" = "run") -> FALSE, and thus do the 'else' scripting (our 'msg' Scripts/Functions: print [expression] "You didn't run away in time, and were killed." and print [EXPRESSION] "GAME OVER", and our 'finish the game' Script/Function)

here's a good link to a guide that helps in understanding this stuff well:

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

though it's about a RPG's creating/generating of a character.


here's what my example (the scripting only --- you got to decide where/how to add in these scripts/scripting: such as most easily: added to/into an Object's Verb, or maybe you're using the 'onenterroom' Script/Function, meh) would look like in code:

msg ("Input?") // this wasn't shown in my example above, I'm adding it in here, as an extra code line (prompting the person playing the game that they need to put in a input --- I should be telling them they have to type in an input... lol... my bad, meh. As someone new to quest may not know what type / how to, provide an input - bad programming when the user, person playing the game, doesn't know what they got to do and how they do it)
get input {
  if (result = "run">
    msg ("You run away just in time, saving your life."
  } else {
    msg ("You didn't run away in time, and thus were killed.")
    msg ("GAME OVER")
    finish
  }
}

CarmenK72
02 Sept 2016, 01:56

That worked perfectly, thank you so much!