Hi there.
I'm a complete newbie in twine 2 and SugarCube 2.18.0, and I've just begun using the arrays and <<for>>, so please understand my helplessness.
I've been trying to create a table associating the name of four characters with a color, which I've been successful at.
This is the code I've used:
<<set $name_stone to [
["Spring", "Summer", "Autumn", "Winter"],
["green", "yellow", "red", "blue"]
]>>
My problem starts here. At the beginning of the game I give the player the option of choosing one of the four names, which becomes immediately the variable $name1.
After that, I want to distribute the remaining three names to the NPC that appear during the game.
So, if you were to choose the name "Autumn", the other three would become "Spring", "Summer", and "Winter" (the order of which being unimportant).
Result: $name1 = Autumn, (and for example) $name2 = Summer, $name3 = Winter, $name4 = Spring
The thing is, I've been trying to do this with a for cycle that establishes these variables according to the player's name choice.
I tried with this code:
<<set $counter == 2>>
<<for _i to 0; _i lt $name_stone.length; _i++>>
<<capture _i>>
<<if $name1 == $name_stone[0][_i]>>
<<set $stone1 = $name_stone[1][_i]>>
<<set $flag = 0>>
<</if>>
<</capture>>
<<if $name1 !== $name_stone[0][_i] and $flag !== 0>>
<<capture _i>>
<<if $counter == 2>>
<<set $name2 = $name_stone[0][_i]>>
<<set $stone2 = $name_stone[1][_i]>>
<<set $flag = 1>><</if>><</capture>>
<<capture _i>>
<<if $counter == 3>>
<<set $name3 = $name_stone[0][_i]>>
<<set $stone3 = $name_stone[1][_i]>>
<<set $flag = 1>><</if>><</capture>>
<<capture _i>>
<<if $counter == 4>>
<<set $name4 = $name_stone[0][_i]>>
<<set $stone4 = $name_stone[1][_i]>>
<<set $flag = 1>><</if>><</capture>>
<</if>>
<<if $flag == 1>> <<set $counter++>><</if>>
<</for>>
Now, I know this is probably really rough, and there are a thousand better ways to do it, but I keep getting an error.
I try and print the variables after this section of the code, to see if it worked, so:
<<print $name1>> <<print $stone1>> <<print $name2>> <<print $stone2>> and on and on...
but I always get an error. It prints the $name1 correctly, and the everything else wrong.
Could anyone please help me out? Show me a better way, or explain what I did wrong?
Thanks!
Comments
What you should do instead of what you are doing anyway, is to be working with datamaps:
Then, when the player chooses his character, you can remove that character from the $npc array like for example: