Howdy, Stranger!

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

[SugarCube 1] Trying to create a loop that creates links and saves the current value of the variable

Greetings
Twine 2.0, SugarCube 1.0.34.

I have the whole concept planned out, but then I got an error and realized the fault of it.
The loop is this:
@color:red;HP@@]
<</if>>
<<click "(Look at)">>
<<set $invEquipScreenItem = $ConsumableItemsDetails[$i].name>>
<<set $invEquipScreenAction = "lookat">>
<<goto invEquipScreen>>
<</click>>
<<if $ConsumableItemsDetails[$i].type == "food">>
<<click "(Eat)">>
<<set $invEquipScreenItem = $ConsumableItemsDetails[$i].name>>
<<set $invEquipScreenAction = "consume">>
<<goto invEquipScreen>>
<</click>>
<<elseif $ConsumableItemsDetails[$i].type == "drink">>
<<click "(Drink)">>
<<set $invEquipScreenItem = $ConsumableItemsDetails[$i].name>>
<<set $invEquipScreenAction = "consume">>
<<goto invEquipScreen>>
<</click>>
<</if>>
<</nobr>>
<</for>>

The problem is
<<set $invEquipScreenItem = $ConsumableItemsDetails[$i].name>>
At the first iteration the $i is equal to 0, and I'd like the created set macro use $ConsumableItemsDetails[0].name. Not the $i that's remaining after the entire loop, which is static for all the buttons and is equal to the latest item in the array.

Can this even be done, and if so, how? I know I can just manually list every item with a separate if statement, but I'd tried that and the formatting becomes an issue because always a new, empty line is created if amount of an item (I use static indexes) is 0, not to mention the giant amount of sheer text that is required to be copypasted. An unnecessary baggage.

Best Regards

Comments

  • I also realised the loop itself is flawed, because it ignored everything past the first 0 it encounters, so if i have a health potion in index 0 and mana potion in index 1 and I drink the health potion the loop will display as if I have no items.
    I noted this down and will figure out a solution later.

    Still, initial problems stand.
  • When you're using a loop variable within anything that delays the access of said variable until a later time (e.g. the interior of a <<click>>), then you need to capture the value of said variable. Try the following:
    @color:red;HP@@]
    <</if>>
    <<print '<<click "(Look at)">><<set $invEquipScreenItem = $ConsumableItemsDetails[' + $i + '].name; $invEquipScreenAction = "lookat">><<goto "invEquipScreen">><</click>>'>>
    <<if $ConsumableItemsDetails[$i].type == "food">>
    <<print '<<click "(Eat)">><<set $invEquipScreenItem = $ConsumableItemsDetails[' + $i + '].name; $invEquipScreenAction = "consume">><<goto "invEquipScreen">><</click>>'>>
    <<elseif $ConsumableItemsDetails[$i].type == "drink">>
    <<print '<<click "(Drink)">><<set $invEquipScreenItem = $ConsumableItemsDetails[' + $i + '].name; $invEquipScreenAction = "consume">><<goto "invEquipScreen">><</click>>'>>
    <</if>>
    <</nobr>>
    <</for>>
    <<unset $i>>
    
    n.b. As shown above, I'd also unset your loop index variable unless you need it to exist for longer than the loop itself.
  • It just works. Just like that.
    I thank you.
Sign In or Register to comment.