Howdy, Stranger!

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

Arrays, Objects and Setter Links (Sugarcube 2)

This is a partial continuation of my previous struggles with arrays in Harlowe, but I've created a new thread as this is me trying out Sugarcube 2 per expert advice. Apologies if a new thread for a new format and a new question isn't proper etiquette.

So what I have is this: $characters, a database of character arrays. For experimental purposes, it's set up thus:

<<set $sam to {name: "Sam ", health: 2}>>
<<set $frodo to {name: "Frodo ", health: 1}>>
<<set $characters = [$sam, $frodo]>>

Then, $activeCharacter is used to store the variables of the character we're using at the moment. This seems to work fine; for example, here where i is 0, Sam's variables get put into $activeCharacter:

<<set $activeCharacter to $characters[$i]>>

At a main passage I include a link of each character's name. All these links go to an "affect" passage, where the player can affect the stats of the character whose link was selected.

<<for $i to 0; $i lt $characters.length; $i++>>
<<link affect][set $activeCharacter to $characters[$i]>>
<</for>>

This all seems to work, including the links. However, the "set $activeCharacter to $characters[$i]" which worked fine outside the link setter does not seem to work inside it, and throws a SyntaxError: Unexpected Token message.

Thoughts?

Comments

  • edited July 2015
    You don't use the set keyword in Setter Links, you also don't need to use the <<link>> macro:
    <<for $i to 0; $i lt $characters.length; $i++>>
    [[$characters[$i].name|affect][$activeCharacter to $characters[$i]]]
    <</for>>
    

    WARNING: Each time you move from one passage to another in SugarCube the History sub-system stores the current value of all $variables with the previous passage and creates a new snap-shot of all the $variables (and their values) available to the new passage.
    In the case of $variables containing objects this means that those $variables will now contain a copy of the original object, not the original object itself.
  • Thanks, I evidently misapplied the documentation.
    greyelf wrote: »
    In the case of $variables containing objects this means that those $variables will now contain a copy of the original object, not the original object itself.

    So I understand. My intent is to use $activeCharacter to work with in loadout selection, combat etc. and then write short bit of code that dumps $activeCharacter back into the slot in $characters whose $name matches $activeCharacter.name when that character is no longer being used by the player.

  • greyelf wrote: »
    <<for $i to 0; $i lt $characters.length; $i++>>
    [[$characters[$i].name|affect][$activeCharacter to $characters[$i]]]
    <</for>>
    

    This does not in fact work. After some playing around, it appears that the $i in [$activeCharacter to $characters[$i]] is applied when the user selects a link, not when the for loop creates the link. Therefore, the for loop having by then completed, $i will be length of array plus one when applied, and $activeCharacter will not be filled from $characters. For example, both setter links generated by the example code run with $i = 2 because there are 2 items in $characters.

    I tried a few junky ideas to get the setter links to remember the right value of $i, but no luck; and I cannot seem to find an obvious reference in documentation, which doesn't mention setter links that use variables changed by loops.
  • edited July 2015
    The setter components of links are evaluated when the link is clicked. In this case, you need to force the $i within the setter component to be evaluated when the link is created instead, to catch its current value.

    For example:
    <<for $i to 0; $i lt $characters.length; $i++>>
    <<print "[[$characters[$i].name|affect][$activeCharacter to $characters[" + $i + "]]]">>
    <</for>>
    
Sign In or Register to comment.