RUBI Gamestate Stack

Showcase your libraries, tools and other projects that help your fellow love users.
mateusak
Prole
Posts: 10
Joined: Thu May 02, 2019 4:08 am

RUBI Gamestate Stack

Post by mateusak »

A gamestate stack is a way of handling game logic. Each gamestate receives the normal Love2D callbacks (update, draw, ...) and a few special stack related callbacks. The catch is that a gamestate can block these callbacks to gamestates under it (with blockDraw, blockUpdate and blockInput), which will make them receive weak callbacks (weakUpdate, weakKeypressed, ...). This is very useful for pausing and making menus.

The second catch is that gamestates keep their values as long as they're in the stack, which makes very easy to have persistency between gamestates. Think of Skyrim for example; you have your main menu gamestate, loading gamestate, world gamestate, whiterun gamestate and dragonreach gamestate.

This tool doesn't enforce any programming paradigm. Any table can be a gamestate.

https://github.com/mateusak/rubi-gamestate-stack
Last edited by mateusak on Wed Jun 05, 2019 10:58 pm, edited 1 time in total.
User avatar
dusoft
Party member
Posts: 492
Joined: Fri Nov 08, 2013 12:07 am
Location: Europe usually
Contact:

Re: Lovely Scene Stack

Post by dusoft »

Well, it's basically a state management / switcher.
mateusak
Prole
Posts: 10
Joined: Thu May 02, 2019 4:08 am

Re: Lovely Scene Stack

Post by mateusak »

dusoft wrote: Wed May 29, 2019 8:10 pm Well, it's basically a state management / switcher.
why do you hurt my feelings
User avatar
zorg
Party member
Posts: 3436
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: Lovely Scene Stack

Post by zorg »

logic is uncaring; that said, future prospects are extending your lib with other functions that may be useful; that way it won't be just another generic/simple state manager on the block! :3
Me and my stuff :3True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.
mateusak
Prole
Posts: 10
Joined: Thu May 02, 2019 4:08 am

Re: Lovely Scene Stack

Post by mateusak »

zorg wrote: Fri May 31, 2019 7:13 pm logic is uncaring; that said, future prospects are extending your lib with other functions that may be useful; that way it won't be just another generic/simple state manager on the block! :3
I've been in the game industry for many years and by far the most common way of managing game 'states' (which is a term I preferred not to use in this case due to it's allusion to state machines) is using some form of nested FSM. This is not a FSM, so I would hardly call it generic.

For the fun of theory and because I know someone will accuse it of being some sort of FSM, a FSM is defined as an "abstract machine that can be in exactly one of a finite number of states at any given time". Since the scene stack can be in multiple 'states' at the same time, it doesn't fit in the more traditional DFA, but I'll admit it can be considered an NFA (which honestly should be preferred over nested DFAs).

And although the concept and implementation of the scene stack is simple and it does have downsides, it's very time consuming to implement a FSM that can achieve the same level of flexibility, especially for games with few 'states' which is the focus of the scene stack.
User avatar
pgimeno
Party member
Posts: 3544
Joined: Sun Oct 18, 2015 2:58 pm

Re: Lovely Scene Stack

Post by pgimeno »

FWIW, conceptually I don't think it's a nondeterministic finite automaton, as NFAs have deterministic equivalents. I think it's a deterministic pushdown automaton. PDAs are automata with a state and a stack. I prefer to call them states (although a more accurate term could be screens), because the term scene can be confused with a scene tree like https://love2d.org/forums/viewtopic.php?f=5&t=85947.
mateusak
Prole
Posts: 10
Joined: Thu May 02, 2019 4:08 am

Re: Lovely Scene Stack

Post by mateusak »

pgimeno wrote: Wed Jun 05, 2019 1:17 pm FWIW, conceptually I don't think it's a nondeterministic finite automaton, as NFAs have deterministic equivalents. I think it's a deterministic pushdown automaton. PDAs are automata with a state and a stack. I prefer to call them states (although a more accurate term could be screens), because the term scene can be confused with a scene tree like https://love2d.org/forums/viewtopic.php?f=5&t=85947.
Very interesting. But as far as I understand, doesn't a deterministic pushdown automaton implies that the states beneath the top one are not active and have no effect? While you could certainly achieve that with my scene stack, you could also have multiple active states, which makes it a sort of hybrid between NPDA and DPDA.

Initiallly I called it a screen stack but I changed because in my project I had other plans for things named screens... Sometimes I feel there are too many things and too few names.
User avatar
pgimeno
Party member
Posts: 3544
Joined: Sun Oct 18, 2015 2:58 pm

Re: Lovely Scene Stack

Post by pgimeno »

mateusak wrote: Wed Jun 05, 2019 6:24 pm Very interesting. But as far as I understand, doesn't a deterministic pushdown automaton implies that the states beneath the top one are not active and have no effect? While you could certainly achieve that with my scene stack, you could also have multiple active states, which makes it a sort of hybrid between NPDA and DPDA.
From a state transition standpoint, I think it's mostly a DPDA. You really just have one active state, the one at the top of the stack. The fact that you invoke callbacks on the others doesn't have an influence on the structure of the state machine. I don't think it's nondeterministic. The term nondeterministic has a different meaning, related to whether one of the exponential ramifications of the state transitions leads to an acceptance state or not (and an acceptance state has little meaning in this case).

Anyway, the classification was brought up in relation to whether 'state' was an appropriate name or not. I think it is.
mateusak wrote: Wed Jun 05, 2019 6:24 pm Initiallly I called it a screen stack but I changed because in my project I had other plans for things named screens... Sometimes I feel there are too many things and too few names.
True. In these forums, the term I've seen most commonly used is 'gamestate library', if that helps.

FWIW, I read your link about Amethyst gamestates and found the idea interesting. I hadn't found any utility in stack-based gamestate libraries in past, but that was an eye-opener for me. IIRC forum user s-ol wrote a stack-based one too; I haven't checked it to compare.
mateusak
Prole
Posts: 10
Joined: Thu May 02, 2019 4:08 am

Re: Lovely Scene Stack

Post by mateusak »

pgimeno wrote: Wed Jun 05, 2019 7:10 pm From a state transition standpoint, I think it's mostly a DPDA. You really just have one active state, the one at the top of the stack. The fact that you invoke callbacks on the others doesn't have an influence on the structure of the state machine. I don't think it's nondeterministic. The term nondeterministic has a different meaning, related to whether one of the exponential ramifications of the state transitions leads to an acceptance state or not (and an acceptance state has little meaning in this case).

Anyway, the classification was brought up in relation to whether 'state' was an appropriate name or not. I think it is.
Alright then, I think it's clear by now that I'm not familiar with the definitions... :P
pgimeno wrote: Wed Jun 05, 2019 7:10 pm True. In these forums, the term I've seen most commonly used is 'gamestate library', if that helps.

FWIW, I read your link about Amethyst gamestates and found the idea interesting. I hadn't found any utility in stack-based gamestate libraries in past, but that was an eye-opener for me. IIRC forum user s-ol wrote a stack-based one too; I haven't checked it to compare.
If you say so :) Yeah the Amethyst one was so interesting I decided to copy. It works really well on small games. Didn't know someone had already written something similiar, tho.
User avatar
pgimeno
Party member
Posts: 3544
Joined: Sun Oct 18, 2015 2:58 pm

Re: Lovely Scene Stack

Post by pgimeno »

mateusak wrote: Wed Jun 05, 2019 11:00 pmIf you say so :) Yeah the Amethyst one was so interesting I decided to copy. It works really well on small games. Didn't know someone had already written something similiar, tho.
I've now taken a look at other similar libraries. Yours is the only one I see that has separate event handlers while running in the background, which seems interesting. However, I wonder if it wouldn't be better calling the same callbacks but letting them know whether they're on the background or not, and passing back whether the event was handled.

That's the approach taken by the one I was talking about: https://love2d.org/forums/viewtopic.php?f=5&t=79943. It has a 2D stack, i.e. a stack of stacks. The first level looks like an unnecessary complication to me. I don't like the idea of having the game screen stacked on top of the menu screen; I don't think the menu has to be preserved as if it was "paused" while you are playing and "resumed" when you finish. Also, if setting a pause state doesn't call the draw method of the previous stack, handling anything but a full-screen pause is going to be more complicated than adding the pause as another state in the current stack. So all things considered, I don't see the "primary" stack useful. I do like its approach to background execution, though.

Other libraries I've found that handle a state stack are:
ScreenManager by rm-code. Calls draw() for all states, but most other events are called only for the state at the top of the stack. Too many special cases to keep in mind for my taste.
stateful.lua by Kikito. Calls events only for the topmost state.
hump's gamestate by VRLD. Calls events only for the topmost state.
StateStack 0.1 by Plu - looks like it only handles a few events.
Post Reply

Who is online

Users browsing this forum: No registered users and 46 guests