Engine.restart() not resetting variables?

So, my understanding was that the only difference between UI.restart() and Engine.restart() was that UI.restart() calls a dialog box (and then runs Engine.restart). However, in my game, while UI.restart() works perfectly, Engine.restart() seems to… not be running StoryInit, I think? If a variable is set to “true” over the course of the game it seems to remain true after running Engine.restart(), even though StoryInit should set it to false.

Additionally, I have a bunch of randomized text in the game that’s stored in arrays, but after using Engine.restart(), all the places the text is supposed to be just has “Error: cannot read property of undefined” messages (everybody’s favorite vague and unhelpful JS error!). So the arrays aren’t being set up properly somehow (and are not even retaining their values from the previous playthrough like other variables do).

It’s a short game that’s meant for multiple quick playthroughs and has no UI bar, so all the people who tested it for me were kind of thrown off by the “unsaved progress will be lost” dialog box, and I would really love to be able to reset the game “silently,” so I was wondering – does anyone have any insight into what’s going on with Engine.restart() and how to fix it, or have a workaround for it?

I did a quick test here with SugarCube v2.33.2 and wasn’t able to replicate that problem. (You should download and install the update if you’re using an older version of SugarCube.)

Here’s the code I used in my test (written in Twee notation):

:: StoryInit
<<set $test = false>>

:: Start
$test
<<set $test = true>>

[[Next]]

:: Next
$test

<<button "Restart">>
	<<run Engine.restart()>>
<</button>>

If what you were saying were true, I should see “true” at the top of the screen after hitting the “Restart” button. However, I’m correctly seeing “false”, as you’d expect.

Are you possibly running any other code after running Engine.restart() which could be causing the problem? Including triggered events? If so, you should make sure that running Engine.restart() is the last line of code that runs when you call that.

Check that out and let us know what you discover.

Hope that helps! :grinning:

I was using a link rather than a button, and I think that must have been what the problem was, that the link ran Engine.restart() and then forwarded to a page? Changing it to a button seems to have worked. Thanks!

Yeah, if you restarted during a link that triggered a passage transition, then that’s why you got confusing results. It would have restarted and tried to do the passage transition at about the same time.

Thanks! The documentation seemed to be saying that the link macro would run whatever script was attached to it before forwarding to the indicated page, so I thought it was kosher.

It does, the problem is that, if you have the link trying to go to another passage, then it will still try to do that after the Engine.reset(), which is what causes the engine to get so mixed up.