Hi,
This is my first foray into twine2 - it's part of what will become a coding club activity for 10-11 year olds, if I can get it working! Twine 2 seems much easier to work with than twine 1.4.2, so I'm hoping someone can help me sort this out...
I'm having a spot of bother with getting "if" statements to work - In my story, I want to present a list of options to the viewer, depending on what has gone before. My code is an attempt to use variables to control the creation of a list of possible options, then to select one at random.
There are two problems - one is that the created list contains items it ought not to (If I've got the syntax correct!) and the other is that I can't pass a string containing a list to the "Either" macro, whereas I thought macros could be nested in twine 2.
here's the test code:
Test to see if I can make "ifs" and "Either" work!
{
(set: $Dest1 to 0)
(set: $Dest2 to 10) <!--i.e. we don't expect to see Dest2 as an available action-->
(set: $Dest3 to 0)
}
variables Dest1= $Dest1; Dest2= $Dest2; Dest3= $Dest3
{
(set: $Actions to "")
(if: $Dest1 is 0 ) (set: $Actions to $Actions + " action1" )
(if: $Dest2 is 0 ) (set: $Actions to $Actions + " action2" )
(if: $Dest3 is 0 ) (set: $Actions to $Actions + " action3" )
}
these are the possible actions (we should not see Action2): $Actions
{
(set: $YY to (either: "action1","action2","action3")) <!--this works-->
(set: $ZZ to (either: $Actions ) ) <!--this does not work -->
}
Action selected from list of strings :<br> $YY
Action selected from variable with list of strings:<br> $ZZ
------------------------------------------------
Comments
Part of my problem is that I assumed that a non-zero value in a variable will evaluate to true, which I found when trying the Harlowe example on anonymous hooks:
{
(set: $heartbeat to 10) (set: $breathing to 1)
(set: $alive to $heartbeat > 0 and $breathing)
}
You find yourself in a dark, $alive[spooky ]part of the dungeon.
If you run this, the output shows the actual $alive variable:
You find yourself in a dark, 1spooky part of the dungeon.
If I change my original script to use true/false instead of numbers, like so:
(set: $Dest1 to true)
(set: $Dest2 to false) <!--i.e. we don't expect to see Dest2 as an action-->
(set: $Dest3 to true)
and the part that creates a list of actions like so:
(set: $Actions to "")
(if: $Dest1 ) [(set: $Actions to $Actions + " action1" )]
(if: $Dest2 ) [(set: $Actions to $Actions + " action2" )]
(if: $Dest3 ) [(set: $Actions to $Actions + " action3" )]
then the $actions variable gets "action1 action3" as required.
I still haven't had any success in passing the $actions variable to the (either: ) macro, but at least I think I might be able to do it in the way the $alive example showed... provided I can use actual passage links for the actions. I'll try that next.
Something like this:
Possible actions are: $Dest1[runlink ] $Dest2[jumplink ] $Dest3[swimlink]
Sorry if having asked the question I've had anyone go off on a wild goose chase
1. The first issue is that you are missing the [ and ] symbols from your (if:) macros: 2. The second issue is that you are mixing Datatypes, you're passing a single String value to the (either:) macro when it is expecting a List of String values.
eg.
Your $Actions variable (once your (if:) macros are fixed) contains a String value: " action1 action3" so when Harlowe processes your (either: $Actions ) it is converted to (either: " action1 action3") but the result you really want is (either: "action1","action3").
Unfortunately it is not enough to just add comas to your String so what you need to do is change your $Actions variable from storing a String to storing an Array.
(note: The Harlowe documentation suggests using it when modifying the value of a variable) Because your variable is now an Array you have to use the "spread" operator (three full stops) when passing the variable to the (either:) macro So your code would now look like the following: