So in our current project, I'm programming most of the mechanics and systems from scratch. It also switches between several modes of gameplay, so I have a state manager script that initializes each state correctly. A potential issue I encountered with doing this was that none of the scripts ever actually returned. This made me worry that the call stack would get too high and crash eventually.
Each state is its own script with its own game loop, eg. "minigame1()", "minigame2()", etc. The state manager script would evaluate which state it was currently in, then call the script for the correct next minigame. That left the call stack looking like "titlescreen > statemanager > minigame1 > statemanager > minigame2 > statemanage > ...".
To get around this, instead of running the scripts directly from state manager, the state manager returns a script ID, which each minigame calls with a timer. The state manager will have something like "exitReturning(@minigame1)" and the end of each minigame script will call this line:
Code: Select all
setTimer(timer:nextState, 0, 1, stateManager())
This way, both the state manager and each minigame script have a chance to return before the next state begins. It appears to work fine, but I have a few questions about it. First of all, this feels like I'm overcomplicating things. Is there any easier way to avoid call stacks getting too high when you're always running a script at any given time? Are there any potential issues I may run into here with scripts running out of order, etc.?