Howdy, Stranger!

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

How to store a variable through a link inside of a loop

I've been racking my brain on this for some time now and I can't think of a way to do it. I want to create a set of links inside of a for loop using the length of the array as the number of links and the items in each slot in the array as the link names. So if I have an inventory array consisting of 3 items i'd want 3 links with the items in each slot as the link names. This part, I can do.

The part I'm stuck on is they will all link to the same passage but I can't figure out how to pass through the variable that the user wants. Say the user wants to use or equip item 2 out of the 3. How can I pass the value of item 2 through the link if it's generated in a for loop for an array of unknown size?

In my example, I'm using objects to store items in classes and subclasses (Armor.TypeOfArmor.MaterialOfTypeOfArmor.Properties). Where TypeOfArmor is helm, shield, chest etc... MaterialOfTypeOfArmor is iron, steel, wood, etc... and properties are things like name, defensive capabilities, durability etc...

Code I have:
<<for $i to 0; $i lt Object.keys($Armor.Helm).length; $i++>><<set $temptext to Object.keys($Armor.Helm)[$i]>>
<<print ($i+1) + ". ">>ChosenHelm
<</for>>

This code currently spits out all the links I want properly but doesn't pass the MaterialOfTypeOfArmor through the link that I want.

Can anyone help with this problem?

Comments

  • note: Because I don't know what the value of $Armor.Helm is I am going to use a simpler array in my example.

    The trick is to use a <<print>> macro to create a markup link based on the relevant variables.
    <<set $keys to ["one","two","three"]>>
    
    <<for $i to 0; $i lt $keys.length; $i++>>
    	<<print ($i+1) + ". [[" + $keys[$i] + "|ChosenHelm]]">>
    <</for>>
    
    ... the above should generate three markup links which all have a Target Passage Name of ChosenHelm and with Link Texts of:
    1. one
    2. two
    3. three
  • I guess the more specific question I have (since I'm new to twine but have coded in Java and C before), is there a way to pass on to the next passage, which link was clicked? Say that while the player is strolling through the dungeon they pick up 2 more helms so the links generated say:

    1. none
    2. wood
    3. steel
    4. diamond
    5. Mithril

    I want the next passage to be passed a variable based on which link was clicked in a way that I can use it to change the $Character.Helm variable to either point to the specific helm in $Armor.Helm.TypeofHelm.PropertiesofHelm Datamap object or have that part saved to another array to just become an array consisting of Typeofhelm.PropertiesofHelm so I can use it and have the game recognize it's "equipped".

    Basically the character will go into their inventory and click on a link generated and I want the game to set a variable based on which link they clicked. so if they click the third link then the game will set a variable so I can use it to tell the $Character object that the steel helm is equipped. same with the second link or fourth link, or hell, if I include 20 helms, I want it to know that the player clicked that 20th link.

    Any way I can do this using a <<click>> macro or anything?
  • The same <<print>> technique works for Markup based Link w/ Text & Setter. Note the usage of both single and double quotes because the Setter part of the markup link requires the embedded string value to be quoted.
    <<set $keys to ["one","two","three"]>>
    <<set $item to "">>
    
    <<for $i to 0; $i lt $keys.length; $i++>>
    	<<print ($i+1) + '. [[' + $keys[$i] + '|ChosenHelm][$item to "' + $keys[$i] + '"]]'>>
    <</for>>
    
    ... the above uses the $item variable to passage the key associated with the selected option to the ChosenHelm passage, which you can see using the following within the ChosenHelm passage:
    Item: $item
    
  • You've solved the issue I was having though another question remains, how do I get the syntax properly written to make <<set $Character.Helm to $Armor.Helm.($item)>>. I've tried brackets, braces, quotations, everything. I just can't get the $Character.Helm value to essentially point to the TypeOfHelmet being worn so that when I call $Character.Helm later whether it be for .durability or .ability or .cost it gives me the values I want.
  • Without actual examples of value (and type) being stored in variables like $Armor.Helm I am going to assume it is a set of key/value pairs, if this is true then try the following:
    <<set $Character.Helm to $Armor.Helm[$item]>>
    
  • I swear I've tried that code before and it didn't work for some reason. Just popped that in and it worked!! Thanks greyelf, really appreciate the work you do for everyone!!
Sign In or Register to comment.