Howdy, Stranger!

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

If Statement Printing Even Though Condition is Not Fulfilled

Hi!
I'm making a game with Harlowe and I want to make it so the player does not have the option to select the same item multiple times Do this I thought this code would work.

(set:$choicesandal to 0)

(if: $choicesandal is 0) Add Sandal to Stew

(if: $choicesandal is not 0) Look at North Wall

However every time I test the game both choices print despite the second "if" condition not being fulfilled.

If they click Add Sandal to Stew

You added the sandal to the stew.

(set: $choicesandal to $choicesandal+1)
(set: $stewchoice to $stewchoice+1)

Any help would be super appreciated!

Comments

  • note: Your example was missing the open/close square bracket associated with both of your (if:) macros.

    I suggest that use a boolean (true / false) instead of a number to track if something has happened or not:
    (set: $hasSandal to false)
    

    The following two examples are the same, the only differences is that one is testing for the $variable being true, the other false:
    (if: $hasSandal)[ [[You already added this item to your stew|Look at North Wall]] ]
    (else:)[ [[Add Sandal to Stew]] ]
    
    (if: not $hasSandal)[ [[Add Sandal to Stew]] ]
    (else:)[  [[You already added this item to your stew|Look at North Wall]] ]
    
    ... to indicate that the Sandal has been added to the Stew change $hasSandal to true:
    (set: $hasSandal to true)
    

    You don't need a Add Sandal to Stew passage, the whole lot can be done in a single passage using a named-hook combined with both a (link:) and (replace:) macro:
  • edited July 2015
    Thank you for your help!
    I have modified the code to this
    [Add Sandal to Stew]<addSandal|
    
    (if: $addSandal is true)[ [[You already added this item to your stew|Look at North Wall]] ]
    
    (set: $hasSandal to false) 
    (click: ?addSandal ) [You added the sandal to the stew. (set: $hasSandal to true)(set: $addSandal to true)] 
    

    However now it is starting the hook before I even click the choice. In fact the choice isn't even clickable. Am I using improper syntax?
  • You have a syntax error in your (click: ?addSandal) line, you have a space character between the close bracket ') of the (click:) macro and the open square bracket '[' of the associated hook/container, remove that extra space and the (click:) macro will work.
    (click: ?addSandal )[You added the sandal to the stew. (set: $hasSandal to true)(set: $addSandal to true)] 
    

    Your example will always show the Add Sandal to Stew link every time the passage is displayed even if the Reader has already done that action, and based on your example you may not need both the $addSandal and $hasSandal variables.

    Generally it is a good idea to initialize (give a default value) to all your $variable at the start of your story, Harlowe 1.1.1 has a new feature which helps you do this. You create a new passage (I would name it Initialize), assign it a tag of startup and any code you place in this passage will be run before the story starts.
    A possible example of a startup tagged passage for your story:
    (set: $hasSandal to false)
    (set: $addSandal to false)
    
Sign In or Register to comment.