$variable.property doesn't work

Twine Version: 2.7.0
Sugarcube 2.36.1

I am puzzled. The sugarcube documentation says if I write in this format $variable.property and as I saw in other games that they have this structure in StoryInit:

<<set $CampJ to {
	TransMast: false,
	Farm: false,

Therefore if I write
<<if $CampJ.TransMast == true>>[img[Transmission Mast|assets/items/antenna_CJ_rs.png]] <</if>>,
it gives me following error:

Error: <<if>>: bad conditional expression in <<if>> clause: Cannot read properties of undefined (reading 'TransMast')
<<if $CampJ.TransMast == true>>[img[Transmission Mast|assets/items/antenna_CJ_rs.png]] <</if>>

Do I need a predefined Javascript code snippet?

You don’t need to put anything in the JavaScript section for object properties to work, and when I paste your code into a fresh project as-is, with that first block in StoryInit and the second in a regular passage, it works fine. (I put in some dummy text for the conditional instead of the image, and confirmed that it worked when I set the value to “true”, rather than just not throwing an error while also not doing what it was intended to do.) My guess would be that you have something somewhere else in your project that’s interfering.

Your syntax is a little off—you don’t need the comma after the second “false” or the semicolon—but in my otherwise empty project, that doesn’t seem to be affecting the execution of the markup.

1 Like

Yup, that should be what’s probably causing it. Or if there is a similarly coded object above this one, that could have affected how $CampJ is set.

<<set $CampJ to {
	TransMast: false,
	Farm: false <- no comma here
}>> <- no semi-colon
1 Like

Thanks much(!) on you both. With your offers for solution and a new fresh mind, I found out that it seems to indeed interfering WHILE I was constructing a passage. Because as I test-played anew (after finishing the passage), I do not have the errors anymore (with either comma, semicolon or false/true). A bit weird and random to me though is that if I reload an old save, the errors come back; the new save does not.

Thanks again.

Generally, values do not change when loading a save, even buggy values. If saves allowed values to randomly change without direct action by the end user (that’s you), they wouldn’t be very good or useful.

For example. If you created a save while the value of $CampJ was undefined, then after loading such a save the value will be undefined, because that’s what’s recorded within the save.

Hm, ok.
Then I must have not completed my variable list as well as my passage. Or mistyped.