Strange behavior setting variables in sugarcube

Twine Version: 2.9.2.0

EDIT: I was being thwarted by caching of the StoryInit file. Hitting the “restart” button rather then refreshing the page fixed it.


I’m having a problem setting a variable, which feels like a deranged problem to have but here we are.

I’m trying to write some widgets that my story will need. The first one went fine. The second one kept telling me that a state variable wasn’t set, so I tried moving it, renaming it, changing it’s value, and printing it outside of the widget, none of which appears to have set it. The variable set two lines down (or two lines above, before I moved it) is working exactly as expected.

My code is as follows:

The StoryInit passage:

...
<<set $what to "what?">>

<<set $map_sizes to {"start": [-3,1],"end": [0,-2]}>>

<<set $characters to {
"bumblesheep":{"avatar":"AvatarPlaceholder","name":"Bumblesheepskin"},
"map":{"avatar":"Map","name":"The Map"}}>>
...

The calling code:

<<print $what>>
<<print $characters>>
<<print $map_sizes>>
What????
<<make_map map>>
...

The widget:

<<widget "make_map" >>
    <<for _i to $map_sizes.start[1]; _i gte $map_sizes.end[1]; _i-->>
        <<for _j to $map_sizes.start[0]; _j gte $map_sizes.end[0]; _j++>>
            <<set $target to _args[0] + $j + '_' + $i>>
            [[img "courier-resources/" + $target + ".png"][$target]]
        <</for>>
    <</for>>
<</widget>>

what that prints is:

[undefined]
[object Object]
[undefined]
What???

and then an error message:

Error: <<make_map>>: error within widget code (Error: <>: bad init expression: State.variables.map_sizes is undefined)

I originally renamed $map_sizes to $what and changed its value, I recreated $map_sizes just now so the error message makes more sense in my explanation.

Both $map_sizes and $what are returning undefined. I’ll admit I don’t find sugarcube’s custom language thing intuitive compared to real javascript, but I don’t think I’m misunderstanding it that badly, and second pair of eyes that can find what I’m doing wrong would be very much appreciated.

1 Like