0 votes
by (420 points)

I'm implementing a combat system that needs to display a list of abilities to the player.  Currently, I'm doing this as follows:

/* abilities is an object map field of an object called Character defined in story javascript; _currentTurnOwner is the Character whose turn it is */
abilities = {
		"attack" : new State.variables.Ability("Attack"),
		"defend" : new State.variables.Ability("Defend"),
		"run" : new State.variables.Ability("Run") 
}
...
<<for _abilityName,_ability range _currentTurnOwner.abilities>>
  <<link [[_abilityName->Combat]]>>
    <<run console.log("setting current selected abl to the value of  temp var ability, which points to "+_ability.name);>>
    <<set $combatArena.currentSelectedAbility = _ability>>
  <</link>>
<</for>>//end ability listing loop

The links show the correctly enumerated abilities Attack, Defend, and Run.  However, the problem I'm having is that the temporary variable value is not evaluated until the link is clicked, and at that point it holds whatever was last set within the enclosing for loop.  Hence, Attack and Defend both log to the console that Run is the current selected ability.   What's the best way to handle this situation where code within <<link>> macros generated in a <<for [range]>> loop macro needs to refer back to the relevant object from the range iteration wherein the <<link>> was defined?  Is there a way to access the parent <<link>> macro from within nested child macros, perhaps?

1 Answer

+1 vote
by (159k points)
selected by
 
Best answer

Please use the Question Tags to state the name and full version number of the story format you are using, adding that information to the Question Title just makes it longer than necessary.

Based on the information you have supplied I believe you need to use the <<capture>> macro to capture the current value of the temporary _ability variable for each of the <<for>> loop's iterations.

The resulting <<for>> macro should look something like the following.

<<for _abilityName, _ability range _currentTurnOwner.abilities>>
	<<capture _ability>>
		<<link [[_abilityName->Combat]]>>
			<<run console.log("setting current selected abl to the value of  temp var ability, which points to " + _ability.name);>>
			<<set $combatArena.currentSelectedAbility = _ability>>
  		<</link>>
	<</capture>>
<</for>>

warning: The above example hasn't been tested, this is due to the fact that I didn't have enough information and would of needed to make too many assumptions to create a working test project.

...