Greater than..

Hi,

Can anyone tell me how to use a > in Gamebook?

I've offered the player 5 choices of moves on a linear set of rooms, 1, 2, 3 ,4, 5. What I want to do is have an Else statement that says any number entered greater then 5, clears the screen, and returns the player to the start of that room.

Kind regards
Chris


The > eats the biggest one! Remember that from school?
Someone else should help with the rest.


Yeah, I remember! Thks


What exactly do you want to do? You can use > in a script without problems


Hi Pertex,

As I said above, I have a set of linear rooms in Gamebook, and offer the player a choice of the number of rooms they can move along the line. 1,2,3,4,5. If they try and enter a number greater than 5, I would like to give them an error message, and return them to the start of the room they are currently in.

So I have code working for 'If expression result = "1" then move the player one room along the line, and so on up to result = "5".

I've tried result = "<5" and result = <5 and result = greater than 5 , all followed by 'Move the player to Room (current), and none of that works, and though the text version causes an Error 520, the others seem to accept the code OK, but nothing happens during play.

TextFX_Typewriter ("You can move 1,2 3 4 or 5 spaces", 100)
GetInput() {
  if (result = "1") {
    MovePlayer (S89)
  }
  else if (result = "2") {
    MovePlayer (S90)
  }
  else if (result = "3") {
    MovePlayer (S91)
  }
  else if (result = "4") {
    MovePlayer (S92)
  }
  else if (result = "5") {
    MovePlayer (S93)
  }
  else {
    msg ("Please enter 1,2,3,4, or 5")
  }
}

The last 'else' gives the message, and that's all, and that's what I want to change to what I've said above.

Kind regards
Chris


Hello.

result is a string, and you would need to convert it to a number before mathematical operations can be performed.


https://docs.textadventures.co.uk/quest/functions/toint.html

Try this:

  else if (ToInt(result) > 5) {
    msg ("Please enter 1,2,3,4, or 5")
  }

BUT your current script is better without this. If the choices are only 1 - 5, your current code has any other input covered. If you change the else to an else if (like the code I posted):

  • it will cause an error if result cannot be converted to a number (if the player enters words rather than numbers)
  • nothing would happen if the player entered 0

I've tried result = "<5" and result = <5 and result = greater than 5

You're asking if the number result is equal to the number greater than 5. This fails because "greater than 5" isn't a number.
Although the expression result = "<5" would be true if the player literally typed <5.

What you want to do is ask if the number result is greater than the number 5. Like this:

if (result > 5) {

However, this won't actually work in this case. Because when the player types in a result, it's a sequence of characters, not a number.
If you're just checking if values are equal, you can do comparisons like (result = "5"), which checks if the two strings contain all the same characters in the same order. But Quest doesn't support less than or greater than for strings, so you will need to convert the result into a number (in this case an int) first.

For example, the expression (ToInt(result) > 5) converts result to an integer, and then compares that integer to 5.

But this brings its own problems. What if the player types "five"? ToInt will just give an error. So you'd need to start with something like:

if (IsInt (result)) {
  number = ToInt (result)
  // all the code to deal with the other numbers
  if (number > 5) {
    // whatever you want to do with it
  }
}
else {
  msg ("You need to type in a number.")
}

In this case, you should still be prepared for anything else the player could do. For example, what if they type "0", or "-5"? For situations like this, using an else clause like you posted may be the best solution, because it handles the case where result is greater than 5, the case where result is less than 1, and the case where the player types something that isn't a number.

Although I would suggest that the most efficient way of doing this particular code would be using a switch block:

TextFX_Typewriter ("You can move 1,2 3 4 or 5 spaces", 100)
GetInput() {
  switch (result) {
    case("1") {
      MovePlayer (S89)
    }
    case("2") {
      MovePlayer (S90)
    }
    case("3") {
      MovePlayer (S91)
    }
    case("4") {
      MovePlayer (S92)
    }
    case("5") {
      MovePlayer (S93)
    }
    default {
      msg ("Please enter 1,2,3,4, or 5")
    }
  }
}

Or if the numbers of the pages are all consecutive, you could shorten it like this:

TextFX_Typewriter ("You can move 1,2 3 4 or 5 spaces", 100)
GetInput() {
  if (IsInt (result)) {
    result = ToInt (result)
  }
  else {
    result = 0
  }
  if (result > 5 or result < 1) {
    msg ("Please enter 1,2,3,4, or 5")
  }
  else {
    MovePlayer (GetObject ("S" + (88 + result)))
  }
}

Looks like KV beat me to it :)


Looks like KV beat me to it

...but your post was much more informative.


Hi mrangel and K.V

Thanks for all your help.
I need to look at which of the above needs less work from me. What I'm doing is a Snakes and Ladders game with 100 rooms, and a lot of the rooms have extra things in, like going up a ladder, and down a snake, all of which work with what I have already. I like the MovePlayer (GetObject ("S" + (88 + result)))solution, but that doesn't cover the additional moves needed. I'll try TOINT first, and see what happens, then maybe the switch code.

I'll let you know, after I've amended 100 lots of script!

Kind regards
Chris


OK, hopefully I've copied the scripts in correctly, I click OK, and the GUI accepts it, but when I enter a number above 5, nothing happens, the screen just stays as is, no message sent, and with no Input box

TextFX_Typewriter ("You can move 1,2 3 4 or 5 spaces", 100)
GetInput() {
  if ( IsInt (result)) {
    number = ToInt (result)
  }
  if (number < 5) {
    play sound ("boing.wav", true, false)
    msg ("Just enter 1,2,3,4 or 5")
  }
  else if (result = "1") {
    MovePlayer (S1)
  }
  else if (result = "2") {
    msg ("It's a Ladder!")
    picture ("ladder.gif")
    play sound ("ladder.wav", true, false)
    MovePlayer (S38)
  }
  else if (result = "3") {
    MovePlayer (S3)
  }
  else if (result = "4") {
    MovePlayer (S4)
  }
  else if (result = "5") {
    MovePlayer (S5)
  }
}

Probably something small?

Kind regards
Chris


Just spotted one typo <5 now changed to >5, but I need to get the input box back up.


Where are you putting this script?

If it's in a function, you can run the function again to ask the player again.
If it's in the script on a particular page, you can use the line:

do (player.parent, "script")

Looking at that code, it seems a little strange. That means that you'll have to type in all the ladders five times, once for each page you can get there from.
To me it seems more logical have this script move the player to page 1, 2, 3, 4, or 5, and then the script on page S2 would be:

msg ("It's a Ladder!")
picture ("ladder.gif")
play sound ("ladder.wav", true, false)
MovePlayer (S38)

That way it runs as soon as the player lands there, whichever page they came from.


Many thanks for the reply, and for all the suggestions.
That script I copied in above, is the script for the 'Start' room. There is similar on each of another 100 rooms. There are 13 Ladders and 13 Snakes, so MovePlayer (S38) will only work for that ladder, and I'd have to make 12 other scripts. Plus 13 for the Snakes. I have a published version here https://textadventures.co.uk/games/view/ijfvwg_enu2dls56k7nwxg/snakes-and-ladders which does what I want, but has the bug when a player doesn't stick to 1,2,3,4 or 5. I'm happy to add the ToInt script to all the rooms, if it would work.

Kind regards
Chris


Log in to post a reply.

Support

Forums