Okay, so here's the wait code for the Specs threader:
SetUpdateScript('Threads.updateAll();');
SetRenderScript('Threads.renderAll();');
Threads.waitFor = function(threadID)
{
while (Threads.isRunning(threadID)) {
RenderMap();
FlipScreen();
UpdateMapEngine();
}
};
Now suppose I start a battle, here's some code to illustrate the issue (essentially pseudocode, my actual battle engine is more complex than this):
Battle.prototype.go = function()
{
// Prepare state variables in preparation for fight...
this.$mainThread = Threads.createEntityThread(this); // An "entity" in the Specs engine is any object with an .update method.
Threads.waitFor(this.$mainThread);
};
// ...
Battle.prototype.update = function()
{
// ...
if (turnIsUp) {
var menuThread = new MoveMenu(actingUnit);
Threads.waitFor(menuThread);
}
};
All's well and good, the battle starts running, but the minute the menu is hit everything freezes up. See, the battle engine is already running inside of a waitFor() loop itself, which means Battle.update()--and therefore waitFor(menuThread) is being called as part of the update script. This means the updatescript enters a wait loop, waiting until menuThread terminates. However, menuThread never terminates because the updatescript is caught in a loop and Sphere won't run another instance, so no threads get updated ever again. And so, the whole thing deadlocks.