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 */
$strength[$player.str]

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>>
$strength[$temp]

My output was:

Average

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

1 Answer

+1 vote
by (44.7k 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.
...