Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

I'm having trouble with the usage of

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 $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 $flag == 1>> <<set $counter++>><</if>>

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! :)


  • There are a number of things you are doing wrong - the most obvious being that <<capture>> is only used, if the capured element needs to be called after the <<for>> is closed - for example if you want to generate links to execute code later.

    What you should do instead of what you are doing anyway, is to be working with datamaps:
    <<set $char1 to {
    name: "Spring",
    color: "green"
    <<set $char2 to {
    name: "Summer",
    color: "yellow"
    <<set $char3 to {
    name: "Autumn",
    color: "red"
    <<set $char4 to {
    name: "Winter",
    color: "blue"
    <<set $npc to [$char1, $char2, $char3, $char4]>>

    Then, when the player chooses his character, you can remove that character from the $npc array like for example:
    <<set $player to $char1>>
  • Sorry - that has to be
    <<set $npc.delete($char1)>>
    right at the end there.
  • Thanks! That does work much better! Sorry if it was a stupid question... I'll be using this from now on!
Sign In or Register to comment.