0 votes
by (130 points)

With the following code, I am trying to create the variable "dmg" within the object, $player, and to make it equal $player.str * 2 but seem to be unable to. Does anyone have any suggestions? I have highlighted the variable in question.

Cheers.

<<set $player =  {
  "name" : "Lupen", 
  "hp" : 50,
  "ap" : 15,
  "str" : 5,
  "dmg" : "$player.str" * 2,
  "dex" : 6,
  "int" : 10,
  "cha" : 10,
  "description" : "Itsa me, Lupen."
 }>>
 
 <<print $player.dmg>>

1 Answer

+1 vote
by (68.6k points)
edited by

You cannot access the properties of an object while you're in the middle of defining it with an object literal.  If you want to base the value of one property on another, then you must either do it after or use an intermediate value.

 

For example, to do so after:

<<set $player = {
	"name" : "Lupen", 
	"hp"   : 50,
	"ap"   : 15,
	"str"  : 5,
	"dmg"  : 0,
	"dex"  : 6,
	"int"  : 10,
	"cha"  : 10,
	"desc" : "Itsa me, Lupen."
}>>
<<set $player.dmg = $player.str * 2>>

 

Alternatively.  Here's an example of one way to use an use an intermediate value (using a temporary variable):

<<set _str = 5>>
<<set $player = {
	"name" : "Lupen", 
	"hp"   : 50,
	"ap"   : 15,
	"str"  : _str,
	"dmg"  : _str * 2,
	"dex"  : 6,
	"int"  : 10,
	"cha"  : 10,
	"desc" : "Itsa me, Lupen."
}>>

 

by (130 points)
Thanks for the explanation and the great examples.
...