How do I create a link to set a variable from within a for: loop?

0 votes
asked Apr 9 by Valareos (200 points)
edited Apr 10 by Valareos

Format: Harlowe 2.1


	(set: _racenames to (datanames: $races))
    (set: $validrace to (a:))
	(for: each _i, ..._racenames)[
		(if: $races's (_i)'s level <= 6)[
			(link: _i)[
					$char's baserace to (_i); 
					$charrace to (_i)
				(go-to: "Race Choice")
			](print: "  ")

I am making a script to help me to allow people to test campaigns I design before they purchase them.

The script above is supposed to set the race variables, and the info is displayed in a separate footer page.

$races is a datamap, declared at story start
$char is a datamap, declared at stat selection
$char's baserace is an entry in the datama
$charrace is a variable declared at stat selection, set to a default value

The script displays the options appropriatly as links, but when I click on them, it doesn't update the variables.

The go-to as the passage call to itself, which allows players to make a different selection if they made a mistake.

Any help is appreciated. Thank you


1 Answer

+1 vote
answered Apr 10 by idling (10,570 points)
selected Apr 10 by Valareos
Best answer

Take a look at the answer given by @greyelf here.

commented Apr 10 by Valareos (200 points)

Managed to get it working.

	(for: each _i, ...(datanames: $races))[
		(if: $races's (_i)'s level <= 6)[
				"(link: _i)[
						$char's baserace to '" + (text: _i) + "',
						$charrace to '" + (text: _i) + "'
					(go-to: 'Race Choice')
				]    "

For anyone else who has the same trouble, there are two main things to remember.

1: Encapsulate the entire link in a Print command using quotes around everything. This forces the program to consider it a single line. If you don't, the _i variable is actually not passed down passed the set portion. You will get an error that _i does not exist

2: After the initial (link: _i), any other references to _i need to be replaces by '" + (text: _i ) + "'
This forces the _i variable to be read at that instant and used, and the quotes and + will ensure that (test: _i) is ran properly

Welcome to Twine Q&A, where you can ask questions and receive answers from other members of the community.

You can also find hints and information on Twine on the official wiki and the old forums archive.

See a spam question? Flag it instead of downvoting. A question flagged enough times will automatically be hidden while moderators review it.