State Switching and the Script Stack

Ask and answer questions about making games and related topics. Unrelated topics go in that other forum.

Moderators: marionline, SDHawk

Post Reply
User avatar
kylekrack
Liquid Metal Slime
Posts: 1240
Joined: Mon Jun 16, 2014 8:58 am
Location: USA
Contact:

State Switching and the Script Stack

Post by kylekrack »

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.?
My pronouns are they/them
Ps. I love my wife
User avatar
Bob the Hamster
Lord of the Slimes
Posts: 7658
Joined: Tue Oct 16, 2007 2:34 pm
Location: Hamster Republic (Ontario Enclave)
Contact:

Re: State Switching and the Script Stack

Post by Bob the Hamster »

I think this is a great way to handle your state machine.

There might be other ways to get the behaviour you want, but I don't think any others will be cleaner or simpler (not without new engine features being added first)

If two or more different states somehow get triggered in the same tick, then only the last one will actually run on the next tick. For what you describe, that is probably okay, and even desired
User avatar
kylekrack
Liquid Metal Slime
Posts: 1240
Joined: Mon Jun 16, 2014 8:58 am
Location: USA
Contact:

Re: State Switching and the Script Stack

Post by kylekrack »

Thanks! That clears my head. I couldn't tell if I was being clever or naive. I shall push on!
My pronouns are they/them
Ps. I love my wife
User avatar
Spoonweaver
Liquid Metal King Slime
Posts: 6462
Joined: Mon Dec 08, 2008 7:07 am
Contact:

Re: State Switching and the Script Stack

Post by Spoonweaver »

I normally control this sort of thing like this

Code: Select all

global variable, begin
  1,  gameMode
  2,  gamePlaying
end

plotscript,mainGameLoop,begin

  while (gamePlaying==0) do, begin
    if (gameMode==0)then(introScript)
    if (gameMode==1)then(miniGameScriptA)
    if (gameMode==2)then(miniGameScriptB)
    if (gameMode==3)then(miniGameScriptC)
    if (gameMode==4)then(miniGameScriptD)
  end

gameover

end
Then you just have each mini-game script change the gamemode and exit itself when you're done with it.
User avatar
kylekrack
Liquid Metal Slime
Posts: 1240
Joined: Mon Jun 16, 2014 8:58 am
Location: USA
Contact:

Re: State Switching and the Script Stack

Post by kylekrack »

That's smart. Not worth rewriting everything at this point, but in the future I'll start with that model probably.
My pronouns are they/them
Ps. I love my wife
Post Reply