Hello again,
I have run into a bug with my internal game loop, and while I've already come up with a couple of ways I could fix it, I'd still like some advice on the cleanest way to go about it.
For the curious, a WIP of the engine mechanics I'm working on can be seen here: http://rokiyo.net/games/cetacea/Story%20Engine.html
@TheMadExile, I would really appreciate your thoughts on the below, though I also welcome any advice anyone else has to offer too.
Issue summary
To start an internal game loop at around 30 ticks per second, I run the following code within StoryInit (because it seems to be one of the last things executed during story loading):
<<run window.tickTimer = window.setInterval(window.gametick, 33)>>
When calling UI.restart(), the restart is performed without a page load, which means the above timer is not cleared. This causes two primary issues:
Potential solutions
I can think of several ways to address some of these issues (though none of them strike me as ideal):
Conclusion
So yeah, there are a few ways to go about it, but all of them strike me as a little bit "hacky". I'd really appreciate a second opinion on a better approach to this.
The other thought on my mind is... If intervals aren't being cleared during Engine.restart(), does that mean all the other stuff I'm attaching to window isn't getting cleaned up either? I mean, I guess it all gets overwritten when the story JavaScript gets rerun, but I'm wondering if it would be cleaner to have some kind of shutdown task object that only gets called during a restart?
I have run into a bug with my internal game loop, and while I've already come up with a couple of ways I could fix it, I'd still like some advice on the cleanest way to go about it.
For the curious, a WIP of the engine mechanics I'm working on can be seen here: http://rokiyo.net/games/cetacea/Story%20Engine.html
@TheMadExile, I would really appreciate your thoughts on the below, though I also welcome any advice anyone else has to offer too.
Issue summary
To start an internal game loop at around 30 ticks per second, I run the following code within StoryInit (because it seems to be one of the last things executed during story loading):
<<run window.tickTimer = window.setInterval(window.gametick, 33)>>
When calling UI.restart(), the restart is performed without a page load, which means the above timer is not cleared. This causes two primary issues:
- Multiple timers running at once, all of them calling window.gametick more often than expected.
- window.gametick getting called during story initialization, before specific story variables and DOM elements have started existing.
Potential solutions
I can think of several ways to address some of these issues (though none of them strike me as ideal):
- A singleton-style approach: <<run window.tickTimer = window.tickTimer || window.setInterval(window.gametick, 33)>>
- Hook into an Engine.restart() to clear the interval, possibly by rebuilding a custom UI.restart() dialog via the API to call some cleanup code if the user confirms the restart (e.g. clearInterval(window.tickTimer)).
- Wrap window.gametick in a try/catch block to silently discard any errors that occur from it running on load.
- Something involving the options parameter on UI.restart()? I don't understand the documentation within Dialog.addClickHandler() enough to know if I've missed something obvious here.
Conclusion
So yeah, there are a few ways to go about it, but all of them strike me as a little bit "hacky". I'd really appreciate a second opinion on a better approach to this.
The other thought on my mind is... If intervals aren't being cleared during Engine.restart(), does that mean all the other stuff I'm attaching to window isn't getting cleaned up either? I mean, I guess it all gets overwritten when the story JavaScript gets rerun, but I'm wondering if it would be cleaner to have some kind of shutdown task object that only gets called during a restart?