Page 1 of 2

How to manage scenes?

Posted: Wed Aug 25, 2021 9:56 am
by ddabrahim
Hi.

I would like to implement a way to switch between scenes and able to go from menu to scene1 and from scene1 back to the menu and basically delete all menu items from memory and load scene1 and then unload all scene1 items from memory and load menu.

Could anyone please help me and show me what direction to go with this, is there any tutorials, articles about this?
Or maybe any 3rd party library to help me but I would prefer to implement this my self but not sure where to start. Guess I could store each scene in a table and each scene has its own update and draw method that I can call in main.lua but I would appreciate any idea how to go about it.

Thanks.

Re: How to manage scenes?

Posted: Wed Aug 25, 2021 10:19 am
by GVovkiv
Guess I could store each scene in a table and each scene has its own update and draw method that I can call in main.lua
Well, you answered your own question

Re: How to manage scenes?

Posted: Wed Aug 25, 2021 10:36 am
by darkfrei
ddabrahim wrote: Wed Aug 25, 2021 9:56 am I would like to implement a way to switch between scenes and able to go from menu to scene1 and from scene1 back to the menu and basically delete all menu items from memory and load scene1 and then unload all scene1 items from memory and load menu.
Not sure that it's a good implementation, but it looks like you need the states:
https://love2d.org/forums/viewtopic.php ... 76#p243076

Re: How to manage scenes?

Posted: Wed Aug 25, 2021 10:50 am
by ddabrahim
you answered your own question
In most examples I find where the examples got some sort of scene management implemented, I can't figure out how they do it usually the examples have 0 comments and super complicated code.
It is makes me wondering if there is any more optimal, more sophisticated way to do it.
But in case what I have described is the common way to go about it, that's cool :)
it looks like you need the states:
Thanks, I am going to look in to that.

Re: How to manage scenes?

Posted: Wed Aug 25, 2021 2:55 pm
by GVovkiv
https://gitlab.com/V3X3D/love-libs/-/tr ... r/SceneMgr
Check this, it may be useful as example of how that can be done

Re: How to manage scenes?

Posted: Wed Aug 25, 2021 10:22 pm
by dusoft

Re: How to manage scenes?

Posted: Thu Aug 26, 2021 7:36 am
by ddabrahim
Thank you for all the links and libs, they are very useful. Appreciate it.
The Scene Manager lib at GitLab looks pretty clean and straight forward. It is similar to what I had in mind. I'll give it a try.

Thanks.

Re: How to manage scenes?

Posted: Tue Oct 12, 2021 4:47 pm
by BulbaMander
More recently, I've been organizing my "scenes" as "components", inspired by web frameworks like angular and react.

I've found it much simpler to have a tree of components, each component maintaining it's own state and knowing when and which of it's child components to draw. There's a root component, a menu component with several children that make up the main menu and it's non-game selections, as well as a game component which holds the rest of the components as children.

So far, the most troublesome source of complexity arising from this pattern in lua are "events" and event propagation. What I've done so far, is have the parent component register several callbacks with it's child component, which are called under certain circumstances, like a "back" button being pressed, or the player's health falling to zero. It's not a flawless approach but it's been working well enough.

Even withat that complexity, this way of doing it feels less complex, and more managable than the gamestate implementations I've written in the past.

Re: How to manage scenes?

Posted: Fri Oct 15, 2021 1:52 am
by RNavega
BulbaMander wrote: Tue Oct 12, 2021 4:47 pm I've found it much simpler to have a tree of components, each component maintaining it's own state and knowing when and which of it's child components to draw. There's a root component, a menu component with several children that make up the main menu and it's non-game selections, as well as a game component which holds the rest of the components as children.
@BulbaMander under that component abstraction, how would you handle this case: you click/press a menu button, and all other buttons and UI elements animate out of the screen + new elements appear, forming a new menu screen. Say, like going from a title screen to a settings screen but with a smooth animated UI instead of a dry cut.

Re: How to manage scenes?

Posted: Fri Nov 03, 2023 12:37 am
by BulbaMander
RNavega wrote: Fri Oct 15, 2021 1:52 am @BulbaMander under that component abstraction, how would you handle this case: you click/press a menu button, and all other buttons and UI elements animate out of the screen + new elements appear, forming a new menu screen. Say, like going from a title screen to a settings screen but with a smooth animated UI instead of a dry cut.
Simplest case (and how i've handled something like this before) each of the components is responsible for animating the components it owns. The first component animates it's buttons off screen, calls to it's parent with the user's selection, the parent starts the next component which animates it's buttons onto the screen.

It'd be more difficult to have "overlap" so that a subset of the buttons from the first component are being drawn alongside a subset of buttons from the second component. I've never had to do this. My first thought is to rethink whether or not they should be two separate components at all. I may be able to abstract the differences between the two menus in a layer beneath the component layer. For example a single component has several "menu" objects, and it handles it's own transitioning between menus that it owns. Another approach I might take would be to build the parent component so that it can draw several components at once. If that were the case, each component is still responsible for animating it's own children.