0 votes
by (120 points)

Hi everyone. I´m using Twine 2.2.1 with Sugarcube 2.21. In my StoryInit i have a list of arrays with traits of many character lets say $char1 = ["A", "B", "C"], $char2 = ["E", "F"] (with <<set>> and all), etc. Later in a passage i set a random number of enemies appearing (between 1 and 5), next passage (because i want to conserve the previous random number) sets a random number of traits for each enemy (between 1 and 3, <<set $enemy to $traits.random()) and save it as a new array and display a list of characters that i can use to battle them (if i choose character 2 for example <<set $abc to 2>>). In the next passage i want to compare equality of traits of both enemy and selected characters.Next passage i have:

<<set $id to "$char" + $abc>>
<<if $id.includesAll($enemy)>>
    MAX BONUS
<<elseif $id.includesAny($enemy)>>
    BONUS
<<else>>
    NO BONUS
<<endif>>

The problem as far i know is that the references broke between passages so i want to know any sugerence to solve this. Because of so many randomnes in my code im reluctant to use data map.

1 Answer

0 votes
by (44.7k points)
edited by

What you wrote is a little confusing, but from the code you've included it appears that this isn't about broken references, this is about incorrectly setting the value of $id.

If you do the following:

<<set $abc to 2>>
<<set $id to "$char" + $abc>>

then $id won't get the value of $char2, it will get the value of a string with the text "$char2" in it.  The .includesAll() and .includesAny() methods only work on arrays, but you're actually trying to use them on a string.

Rather than using multiple arrays, you should probably be using a single array of arrays, like this:

<<set $char = [ ["A", "B", "C"], ["E", "F"] ]>>

(The extra spaces between the brackets are to prevent Twine from seeing that as a passage link.)

That way $char[0] would hold the array value ["A", "B", "C"], and $char[1] would hold the array value ["E", "F"].

Then you'd just do:

<<if $char[$abc].includesAll($enemy)>>
    MAX BONUS
<<elseif $char[$abc].includesAny($enemy)>>
    BONUS
<<else>>
    NO BONUS
<<endif>>

if you wanted to see if those arrays contained all or any of the elements in $enemy.

If you need to access the individual elements, you would just do something like $char[0][1], and in this case that would return the string value of "B".

Hope that helps!  :-)

...