Sugarcube 2, Twine 2
In the "Start" passage I create my objects:
<<set $player to {name: Player, level: 1, exp: 0}>>
<<set $npc1 to {name: NPC1, level: 1, exp: 0}>>
<<set $npc2 to {name: NPC1, level: 1, exp: 0}>>
<<set $characters to [$player, $npc1, $npc2]>>
<<set $attackers to []>>
<<set $victims to []>>
In passage 1 I'm copying the arrays and give the player some choice options:
<<set $attackers to $characters.slice(0)>>
<<set $victims to $characters.slice(0)>>
In passage 2 I want to go through the $attackers array with a loop and choose a victim for each attack. The result of the attack should then change the stats in the $characters array:
<<set $attackers.shuffle()>>
<<for $i to 0; $i lt $attackers.length; $i++>>
<<set $attacker to $attackers[$i]>>
...
...
<<set $characters[$characters.indexOf($attacker)].exp to $characters[$characters.indexOf($attacker)].exp + 100>>
<</for>>
But unfortunately the indexOf-function is providing only -1 as result. If I do everything (passage 1 & passage 2) in one passage it works but I run into some other problems and I dislike the design.
I found a way to work around the problem with the not-working indexOf-function but it's not elegant. My guess is that with the "<<set $attacker to $attackers[$i]>> I'm not copying the datamap-object but just setting a reference to the array.
Would the indexOf-function work if I properly copy the datamap object?
Thank you.
Comments
Please correct me if I'm wrong.
Second. Generic objects, which is what you're currently using, are not Map objects (which are not called datamaps; that's Harlowe nomenclature). That said, if you wished, you may use Map objects in SugarCube 2.
Third. You really should not use the starting passage for initialization. The StoryInit special passage exists solely for that purpose.
You are correct. The story history requires that new moments/states have their own copies of the story variables, so they're cloned upon passage navigation, which breaks references.
As a workaround, instead of using an array and assigning references, you could use a generic object to hold your character objects and indirectly reference them via their property name/"key". For example:
In the StoryInit special passage:
In passage 1:
In passage 2:
I'm doing if differently by adding a unique index-value to each generic object and looking through the array to find the right generic object with a widget.
It saves me the trouble of going through my whole story to change the code and how to access all the different variables/properties.