Howdy, Stranger!

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

Retrieving Array Information out of a For Loop [Twine 2 / Sugarcube 2]

Hi, I already asked this on the Q&A forum but only got 1 reply which was not related to the question so I am hoping for a better response here.
In Twine 2 / Sugarcube 2
I have a series of arrays holding data for NPCs generated in the process of my game.
The code is below but in summary a for loop counted against the number of NPCs creates a table displaying the names of the NPCs, What I am looking for is a way to select a name from the table and go to a passage which will populate with information from the series of arrays.

The simple solution would be to have the loop number each entry and then use a text box for the player to enter the number and have the game capture that to reference the array position for the next passage. But this feels crude and hackish.

What I want is a way to select the information by a single click by turning the name into a link, Abandoning the loop is acceptable for a better solution but I don't want to limit the number of stored records if at all possible.

(Note the passage is currently in a prototype stage so presentation is a low priority)
Currently you have <<print $NPCGender.length >> Generated characters

<table>
	<<for $i=0; $i<$NPCGender.length; $i++>>\
		<tr>
			<<print "Name: ">> /*Male*/<<if $NPCGender[$i] lte 3 >> <<print $MFNameList[$Firstname[$i]]>><<print ", ">> <<print "Nickname: ">> <<print $MNNameList[$Nickname[$i]]>><</if>>/*Female*/<<if $NPCGender[$i] gte 4 >> <<print $FFNameList[$Firstname[$i]]>><<print ", ">> <<print "Nickname: ">> <<print $FNNameList[$Nickname[$i]]>><</if>><<print ", ">> Gender: <<print $GenderList[$NPCGender[$i]]>>	
		</tr>
	<</for>>
</table>

Comments

  • edited June 2017
    I'm not entirely sure, what you are trying to do. Can you give some more details - also the full version number of what you are working with?

    If you want to store and retrieve information in a for loop, you'd use <<capture>> which you then could combine with a setter link - something like:
    <<for _i to 0;  _i lt $arrayname.length; _i++>>
    
        <<capture _i>>
            [[$arrayname[_i]|passage][$variable to _i]]
        <</capture>>
    
    <</for>>
    

    but for any more details I'd have to know more about what you are trying to do.
  • Capture was exactly the function I was looking for, many thanks idling with a little experimentation to fit it all together I am getting exactly the result I wanted.
Sign In or Register to comment.