0 votes
by (150 points)

I have a player object, $player, and a description array $strength. I am trying to return the player's strength description by putting in the player's strength score as below.

/* My Variables */
<<set $player.str = 2>>
<<set $strength = ["Frail", "Weak", "Average", "Strong"]>>

/* Line I Am Trying To Print */

However, the output I get is:

Frail, Weak, Average, Strong[2]

It appears that it can't parse the object $player.str correctly inside the array. I tried setting a temporary variable $temp = $player.str, and then when I called the strength array with the $temp variable it worked:

/* My Variables */
<<set $player.str = 2>>
<<set $strength = ["Frail", "Weak", "Average", "Strong"]>>

/* Using Temporary Variable */
<<set $temp = $player.str>>

My output was:


How would I go about printing my message with my $player.str variable?

1 Answer

+1 vote
by (44.1k points)

SugarCube will only print out fairly simple "naked" variables.  For any of the more complex variables you'll need to use the <<print>> macro (or one of its aliases).  Any of these should work:

<<print $strength[$player.str]>>
<<= $strength[$player.str]>>
<<- $strength[$player.str]>>

Note: There is a slight difference between "<<->>" and the other two macros, in that this macro also encodes HTML special characters in the output.

Hope that helps!  :-)

by (150 points)
Thank you! That was exactly what I needed.
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.