Page 1 of 1

[Guide] State Manager

Posted: Wed Dec 26, 2018 9:53 am
by janglee
Hello developers,
I have created guide for making scene manager in Love2d. Generally I do not write a tutorial. its not my profession and this is first tutorial I have written. So take a look and give feedback.

https://github.com/Janglee123/Love2dGui ... eneManager

Re: [Guide] Scene Manager

Posted: Wed Dec 26, 2018 11:46 am
by pgimeno
It's good, though the choice of the name is a bit unconventional. These are typically called states, rather than scenes, because the name 'scene' is typically used in the context of scene trees using for rendering, where nodes are normally connected in such way that if you e.g. rotate a node, then all of its children rotate with it. See e.g. viewtopic.php?f=5&t=85947

There are several libraries to handle game states, but I like how you expose it as something natural and straightforward to implement. It's uncommon to need a pre-made library in order to handle game states. There are some libraries that handle nested states, but I have never had a need for those, therefore what you explain should be good for most people. So good job on that.

The method you call 'reset', I call 'activate' because in my case, some states also need an event called 'deactivate', for example to stop sounds that may be still running. It would be a good idea to add an event that fires when exiting a state, for this reason.

Finally, you assume that no state uses any other love callbacks. Depending on the game, some of them will use other events (like 'textinput' if there is any text input, for example), and it's a good idea in general to check if the state function is defined before calling it.

Re: [Guide] Scene Manager

Posted: Thu Dec 27, 2018 12:46 pm
by janglee
pgimeno wrote: Wed Dec 26, 2018 11:46 am It's good, though the choice of the name is a bit unconventional. These are typically called states, rather than scenes, because the name 'scene' is typically used in the context of scene trees using for rendering, where nodes are normally connected in such way that if you e.g. rotate a node, then all of its children rotate with it. See e.g. viewtopic.php?f=5&t=85947
I hear you.
pgimeno wrote: Wed Dec 26, 2018 11:46 am The method you call 'reset', I call 'activate' because in my case, some states also need an event called 'deactivate', for example to stop sounds that may be still running. It would be a good idea to add an event that fires when exiting a state, for this reason.
Thanks for suggestion. Naming is what I do worse than worse.
pgimeno wrote: Wed Dec 26, 2018 11:46 am Finally, you assume that no state uses any other love callbacks. Depending on the game, some of them will use other events (like 'textinput' if there is any text input, for example), and it's a good idea in general to check if the state function is defined before calling it.
Sorry, but I am not getting you. can you give me one more example ?

Re: [Guide] State Manager

Posted: Thu Dec 27, 2018 1:29 pm
by grump
What you're attempting to build here is a state machine (not a scene or state 'manager'). The reason why you'd want to use this to 'manage your scenes' is because you need a mechanism to switch between different parts of your game: the main menu, the options menu, the actual game, etc, right?

LÖVE has only a single global state (one love.load, one love.draw, etc.), which why a more flexible construct is desirable. pgimeno's point (I presume) is that your implementation is a minimal building block for that, but it is lacking in several ways: It only covers load, update, and draw, but there is more to a global game state that needs multiplexing: updating, drawing, loading, key presses, text input, mouse movement and mouse buttons, etc. are all equally important events, but you're ignoring most of them.

It'd also come in handy to have well defined enter/exit points (transitions) between states: when going from game to settings, I don't want to reset the entire game; but I probably want to reset the game state when the game is over and the player goes back to the main menu. Only having 'resetState' is lacking flexibility.

Since the repo is called 'Love2dGuides', it's only fair to assume you only want to show a general direction on how it could be done and leave the rest up to the reader: if you aim to provide a useful framework to be used by anyone, this is not enough though.

Re: [Guide] Scene Manager

Posted: Thu Dec 27, 2018 2:28 pm
by pgimeno
janglee wrote: Thu Dec 27, 2018 12:46 pm
pgimeno wrote: Wed Dec 26, 2018 11:46 am Finally, you assume that no state uses any other love callbacks. Depending on the game, some of them will use other events (like 'textinput' if there is any text input, for example), and it's a good idea in general to check if the state function is defined before calling it.
Sorry, but I am not getting you. can you give me one more example ?
For example, if the love.textinput event is necessary in several states/scenes, this could be added:

Code: Select all

function love.textinput(text)
    if scenes[ currentScene ].textinput then
        return scenes[ currentScene ].textinput(text)
    end
end
If other events are necessary, this scheme is one way for all states to have access to them, so that those states that don't need a certain event, don't have to define it at all.

Re: [Guide] State Manager

Posted: Sun Dec 30, 2018 1:04 am
by dusoft
See also: https://github.com/love2d-community/awe ... -libraries
(state helpers / switchers)

Re: [Guide] State Manager

Posted: Tue Jan 01, 2019 2:04 pm
by janglee
Thank you to all for suggestions. I will update guide with suggested thing.

Re: [Guide] State Manager

Posted: Tue Jan 01, 2019 2:22 pm
by janglee
grump wrote: Thu Dec 27, 2018 1:29 pm What you're attempting to build here is a state machine (not a scene or state 'manager'). The reason why you'd want to use this to 'manage your scenes' is because you need a mechanism to switch between different parts of your game: the main menu, the options menu, the actual game, etc, right?

LÖVE has only a single global state (one love.load, one love.draw, etc.), which why a more flexible construct is desirable. pgimeno's point (I presume) is that your implementation is a minimal building block for that, but it is lacking in several ways: It only covers load, update, and draw, but there is more to a global game state that needs multiplexing: updating, drawing, loading, key presses, text input, mouse movement and mouse buttons, etc. are all equally important events, but you're ignoring most of them.

It'd also come in handy to have well defined enter/exit points (transitions) between states: when going from game to settings, I don't want to reset the entire game; but I probably want to reset the game state when the game is over and the player goes back to the main menu. Only having 'resetState' is lacking flexibility.

Since the repo is called 'Love2dGuides', it's only fair to assume you only want to show a general direction on how it could be done and leave the rest up to the reader: if you aim to provide a useful framework to be used by anyone, this is not enough though.
Yeah, I wrote this guide for beginners and I am only working to provide on guide not a module. Guide does not contains other functions because i want it simplest but I would like to add some of you mentioned as they are most use full and lacking in guide. Mostly beginners do state management in if-else statements. Another thing I used is love2d's file system to read directory because I saw in other game an array containing state name is used to load states. That is not good way to do because when we delete or add state we also have to edit it in array manually. But the approach show in guide do it itself. That is my two main point to create guide.