Cannot get (if:) to work with (variable contains ...)

Twine Version: 2.9.2

I’ve been reading a lot of documentation and previous people’s posts and I cannot work out why this isn’t working for me as I’ve followed others advice which tells me there is probably something I’ve completely missed.

I’m trying to use the (if:) macro to go to a certain passage depending on what item the player has picked up and added to the $inventory variable. However, at the moment it’s only running the (else:) macro and not (if:).
Any Ideas?

:: Startup Tagged Passage::
(set: $inventory to (array:))

::Passage::
As $name walks down the street they see a box on the floor. It flaps open in the wind and reveals two objects. They haven't got space in their bag for both, so which one does $name pick up?
{
(click: "pick up?")[(transition-depart:'slide-left')[(hide:?box1)]
    (show:?object1)(show:?object2)]
=|=
  |object1)[ (link:"object 1")[(set:$inventory to it + (array:'object 1'))(hide:?object2)
   [Keep walking]
        ]
  ]]
  
=|=
  |object2)[(link:"object 2")[(set:$inventory to it + (array:'object 2'))(hide:?object1)
    [Keep walking]
        ]
  ]]
  
|==|

  (if: $inventory contains 'object 1')[(click-goto:"Keep walking",'Path 2') ]
 (else:)[(click-goto:"Keep walking",'Path 3')]
 
 }

I know that the $inventory variable is working as I have it print in the sidebar and can see that it is collecting the correct data dependant on what option you choose. However the (click-goto:) is always choosing the (else:) option.

This is because the (if:)/(else:) section is running at the same time as the main section of your text on that passage.

I would suggest having the (click-goto:) inside the (link:):

(link:"object 1")[
    (set:$inventory to it + (array:'object 1'))
    (hide:?object2)
    [[Keep walking|Path 2]]
]
2 Likes

Brilliant! Thank you so much - this now works.

Although I don’t know if this would then cause an issue later in the story if I wanted to check the contents of the $inventory variable to run an (if:) later?

Ignore my second comment.

I put the (if:) and (else:) macros inside a hidden hook so that they wouldn’t run until the (link:) macro had run and that also works.

so it now looks like this:

 |object1)[ (link:"object 1")[(set:$inventory to it + (array:'object 1'), $extra to true)(hide:?object2)
   [_slideup[$typewriter[Keep walking]
   (show:?link)
        ]
  ]]]
  
=|=
  |object2)[(link:"object 2")[(set:$inventory to it + (array:'object 2'), $extra to true)(hide:?object1)
    [_slideup[$typewriter[Keep walking]]
       (show:?link)
       ]
  ]]
  
|==|
 }
  |link)[ (if: $inventory contains 'object 1')[(click-goto:"Keep walking",'Path 2') ]
 (else:)[(click-goto:"Keep walking",'Path 3')]
 ]
1 Like