<<print $variable>> Not working?

0 votes
asked Nov 8 by Macer200 (120 points)
Sugarcube 2.1.3

This is all code that should be affecting the problem:

First passage:

<<set $u = {
soldier: 100,
moral: 80,
hunger: 30,
food: 250,
fight: 0,
recruit: 0
}>>

<<set $R = {
soldier: 0
}>>

Passage 2

Your soldier count <<print $u.soldier>>.
Your soldier's moral <<print $u.moral>>%.
Your soldier's hunger <<print $u.hunger>>%.
Your amount of food <<print $u.food>>.
<<set $R.soldier = Math.round($soldier / 3)>>
How many fights you have been in <<print $u.fight>>.
How many days it has been since you last recruited from a village or town <<print $u.recruit>>.

Your options:
[[Celebrate]] - <<print $R.soldier>>

<<if $u.recruit <= 0>>
[[Look for towns nearby]]
<<endif>>

 

For some reason the code <<print $R.soldier>> doesn't print anything. And in test mode it doesn't show a '<<print>> #' at all. and there is no red errors anywhere. Did I mess up on something?

1 Answer

0 votes
answered Nov 8 by greyelf (36,930 points)

Your Math.round($soldier / 3) expression is referencing a $soldier variable which hasn't been initialised (at least not in the example you gave), this will cause that expression to result in a NaN (Not a Number) error.

This means that the $R.soldier property now equals a value of NaN, and it appears the <<print>> macro doesn't output that value.

If you assign a value to the $soldier numerical variable before the Math.round() function is called then the <<print>> macro will be able to display the value of the $R.soldier property.

<<set $soldier to 1>>

 

...