Check if X is a number?

Hi. I have this variable X, which is a single character, anything from 1 to Z. If it’s a number between 1 and (say) 6, things happen; but if it’s anything else, we continue to something else. It also constantly changes through the whole game, and is checked multiple times. So … is there a way to check if it’s number before I go to *if (X < 7) ?

No. It’s highly recommended you do not use a mix of number and plain text for variable values, as when you try to check *if a letter is something that only makes sense for numbers, you’ll always hit an error.

1 Like

The problem is user error. X is part of a longer code of digits entered via input_text (too long for input_number, I think) and it should be a number; but if a player goes and enters a letter instead, I don’t want the game to suddenly die.

Hm, I’d have to see the code to be able to recommend anything with that. The short recommendation is, however, don’t mix number values with letters. It’s generally not worth the hassle. One wrong check and everything breaks.

What did you mean by ‘too long for input number’? Does input number have a upper limit on the figure? I thought one could conceivably use something like:

*input_number 0 10000000000000000000000000000000

Or like this, if it’s a fixed number of characters which is the issue and the first number must at least be ‘1’:

*input_ number 1000000000000 9999999999999

Or does that not work?

1 Like
*if (X = "0") or ((X = "1") or ((X = "2") or ((X = "3") or ((X = "4") or ((X = "5") or ((X = "6") or ((X = "7") or ((X = "8") or (X = "9"))))))))) 
  X ${X} is numeric
  *finish
*else
  X ${X} is not numeric
3 Likes

Well, it’s meant to be a way for me to go from starting the story to some later point in the story without having to replay it exactly just so … each digit in the code represents a different choice made–that way, if I want to test a situation in chapter 7 based on a different decision in chapter 3, I can just tweak the code when I enter it in. So the whole thing would have as many digits as there are choices in the whole story.

I thought it might be nice to leave this in as a sort of bookmark/savegame system for players. I mean, I’ve done all this work, and all the choices are going to have this additional code in now…

Originally, I was going to go with *if (XX = 1) or 2 or 3 or whatever for each choice, but I just hit on what seems to be a more elegant solution:

*if not(SN > skipdist) --this checks to see if we've run out of code
  *set XX skipcode#SN --this sets XX to the digit corresponding to the current choice
  *set SN +1 --this sets us up for the next choice, if the code extends that far
  *if ((XX < 4) and (XX > 0)) --this particular choice has 3 options
    *temp next "03c"&"${XX}"
    *goto {next}
  *set skipdist 0 --so if the entered character doesn't work, we get tossed out of the skipping process and dropped back into the story; no more skipping happens.

Thanks, but I guess if the game is going to do all that checking, I might as well just stick to the old thing where I check for each individual choice.

Oh! I know…

if not(SN > skipdist)
  *set XX skipcode#SN
  *set SN +1
  *temp N1 3 --that's the number of options on this choice
  *temp N2 1
  *temp next "03c"
  *label loop
  *if (XX = ${N2})
    *set next "${next}"&"${XX}"
    *goto {next}
  *if (N2 < N1)
    *set N2 +1
    *goto loop
  *set skipdist 0

So basically we run a loop checking if XX = each of the possible numbers corresponding to a choice, and if we run out of choices without satisfying the condition, we fall out of the process. It won’t matter if XX is a letter, because obviously J != 3. Something like this should work, right?

Testing this later this evening!

1 Like

I can’t see why it wouldn’t… Do let us know if it does!

Here we go!

In startup:

*create skipcode "1"
*create skipdist 0
*create SN 1
*create XX 0
*create option "continue"
*create Nc 0
*create Nx 1

Enter your bookmark code:
*input_text skipcode
*set skipdist length(skipcode)
*finish

At the top of the chapter file:

*if not (SN > skipdist)
  *goto choice01

At each choice:

*label choice01
*set option "01c"
*if not(SN > skipdist)
  *set Nc 2
  *gosub Skip
*if not(option = "01c")
  *goto {option}
*choice
  #Do this thing!
    *label 01c1
    *set [all attribute changes here]
    *if not(SN > skipdist)
      *goto choice02
    *[text here, and goto the next scene]
  #Do this other thing!
    *label 01c2
    *set [all attribute changes here]
    *if not(SN > skipdist)
      *goto choice02
    *[text here, and goto the next scene]

And finally, the engine that makes it all work:

*Label Skip
*set XX skipcode#SN
*set SN +1
*set Nx 1
*label Loop
*if (XX = "${Nx}")
  *set option "${option}"&"${XX}"
  *return
*if (Nx < Nc)
  *set Nx +1
  *goto Loop
*set skipdist 0
*return