If / else confusing me!


I have an attribute "item" which can have a value of either "knife" or "gold key" (or be empty) and have reached a point where I want the player to be able to swap the item they have for something else. I've tried this code:

{if item=knife:You have a dull breadknife
    {if item=gold key:You have a gold key}
{else:You have nothing} you can swap.

and also this:

{if item=knife:You have a dull breadknife{if item=gold key:You have a gold key}}{else:You have nothing} you can swap.

(which is the same as the above just all on one line) but in both cases, if the item is set to "knife", the correct text displays, but if the item is "gold key" it jumps to "You have nothing you can swap."

I'm new to JS so am probably missing something simple.


So, let's look at how the engine breaks it down.

  • {if - start of an if block
    • item=knife: - the condition
    • You have a dull breadknife{if item=gold key:You have a gold key} - text if the condition is true
  • }{else: - else clause
    • You have nothing - text if the condition is false
  • } - end of the block

The second if block, {if item=gold key:, is checked immediately after displaying "You have a dull breadknife" - which doesn't appear if you have the key.

I think you want:

You have {if item=knife:
    a dull breadknife
    {if item=gold key:
        a gold key
} you can swap.

(extra spaces added to make the structure clearer; once you understand it, you're probably better putting it back on one line)

Thanks, that really helps - I'm used to nested IF statements in Excel and assumed the value if false would just be picked up; didn't know I needed that extra "else"

Follow on question, though - I now have this:

You have {if item=knife:a dull breadknife}{else:{if item=gold key:a gold key}{else:nothing}} you can swap. The woman takes the {item} and gives you the box in return, before closing the shutter

How do I circumvent the character having nothing and the NPC woman taking "nothing" and yet giving the box in return?

Managed to work something out myself:

{if item=nothing:You have nothing to swap so say [[no]](noswap)}{else:Choose [[yes]](yesswap) or [[no]](noswap).}

That seems to work. :)

This topic is now closed. Topics are closed after 60 days of inactivity.