Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Right and Wrong answers in SugarCube 2

So im trying to make it where there's a keypad that has the code 9-4-6-2, I want the player to input the code into a textbox. If the player gets it right, he/she will get sent to a specific link. But if they get it wrong it will say Incorrect or something like that.

Comments

  • It's usually best if you give us the full version number of the story format.

    Something like this should work:
    Enter the code:
    
    <<textbox "$code" "" autofocus>> 
    
    <span id="textbox-error"></span>
    
    <<button "Confirm">>
        <<set $code to $code.trim>>
        <<if !$code is  '9462'>>
            <<replace '#textbox-error'>>\
                Incorrect code.\
            <</replace>>
        <<else>>
            <<goto 'next passage'>>
        <</if>>
    <</button>>
    
  • edited May 2017
    note: SugarCube (1.x and 2.x) also has a neq and an isnot operator, so the condition of the <<if>> macro could also be written as either of the following.
    <<if $code neq '9462'>>
    
    
    <<if $code isnot '9462'>>
    
  • edited May 2017
    @Chapel Two issues with your example.
    1. The logical-NOT operator has higher precedence than the strict equality operator, so your conditional is effectively doing the following:
      (!$code) is '9462'
      
      That's not going to do what you need. As pointed out by greyelf, simply use one of the inequality operators—I recommend, isnot, the strict inequality operator.
    2. You're missing the parenthesis on the call to the <String>.trim() method, which assigns $code a reference to the method rather than invoking it.

    @Gallant247 Try something like the following:
    Enter the code:
    <<textbox "$code" "" autofocus>> 
    <span id="textbox-error"></span>
    
    <<button "Confirm">>
    	<<set $code to $code.trim()>>
    	<<if $code isnot "9462">>
    		<<replace "#textbox-error">>Incorrect code.<</replace>>
    	<<else>>
    		<<goto "next passage">>
    	<</if>>
    <</button>>
    
  • edited May 2017
    @TheMadExile, thanks for that. @Gallant247, sorry for steering you a bit wrong there.
Sign In or Register to comment.