+2 votes
by (310 points)

Hi
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>

 

1 Answer

+2 votes
by (68.6k points)

A few issues are immediately noticeable.  You're invoking the <<print>> macro unnecessarily in several places, you cannot break up markup as you're attempting with the <table>, and you should be using a temporary variable for your loop index (i.e. _i versus $i).

Try something like the following:

Currently you have <<print $NPCGender.length>> generated characters

<<for _i = 0; _i < $NPCGender.length; _i++>>\
Name: \
<<if $NPCGender[_i] lte 3>>/* is Male */\
<<print $MFNameList[$Firstname[_i]]>>, Nickname: <<print $MNNameList[$Nickname[_i]]>>\
<<else>>/* is Female */\
<<print $FFNameList[$Firstname[_i]]>>, Nickname: <<print $FNNameList[$Nickname[_i]]>>\
<</if>>, Gender: <<print $GenderList[$NPCGender[_i]]>>
<</for>>

 

by (310 points)
cool, thanks
...