Howdy, Stranger!

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

Very basic Harlowe question

I'm a new user, and though I've done some coding with Java and Python before, I'd still consider myself a novice. I'm having trouble with really basic if-else syntax in Harlowe, and this example is pretty close to my issue.

Example:

(if: $animal is "dog") (set: $legAmount to 4)
(if: $animal is "fish") (set: $legAmount to 0)
The $animal has $legAmount legs.

Regardless of whether or not I've set the $animal variable to "dog" or "fish", countless indentation variances and Boolean conditions later I can't seem to get the correct condition. It always sets $legAmount to the most recent number (in this case, 0).

The $animal variable is a string so I can easily display its contents, but I'm by no means married to this type of syntax. I'm sure there's a much easier and more efficient way to implement if: and set: conditions. Much help and patience would be appreciated.

Comments

  • You have the right idea, but your code has a few errors. You left out the brackets around what happens if your "if" statements are true, and I would use "elseif" for the second one. Try this instead:
    (set: $animal to dog)
    (if: $animal is "dog")[(set: $legAmount to 4)]
    (elseif: $animal is "fish")[(set $legAmount to 0)]
    The $animal has $legAmount legs.
    
  • That still doesn't seem to work. Weirdly enough, the output just shows brackets where the code is.

    I probably should have made it more clear, but I would ideally like $animal to be chosen by the player in a previous passage.
  • Ames wrote: »
    That still doesn't seem to work. Weirdly enough, the output just shows brackets where the code is.

    I probably should have made it more clear, but I would ideally like $animal to be chosen by the player in a previous passage.

    Hey, eddies13's code should work. If you can see the brackets when you test the story then they're not being rendered as "code" but as "text". The syntax should make the "]" bold when is interpreted as code.

    If you still can't get it to work just paste your code and let's have a look at it ;)
  • Common reasons why code that appears valid does not seem to work:

    1. Missing colon after the macro name:
    In the following two examples the first one will work while the second one wont because the colon after the set macro name is missing.
    (set: $var to "valid code")
    
    (set $val to "invalid code because colon is missing after macro name")
    

    2. Inserting Space characters in invalid places:
    Inserting Space characters between the end of the macro (the close parenthesis ')' character) and it's hook/container (the open square bracket '[' character) will make your code invalid.
    Again the first example is correct while the second is invalid because of the extra Space character:
    (if: $var is "valid code")[Notice no extra spaces]
    
    (if: $var is "invalid code") [Notice the extra space between the preceding ')' and '[' characters]
    
Sign In or Register to comment.