How to manage scenes?

General discussion about LÖVE, Lua, game development, puns, and unicorns.
User avatar
ddabrahim
Party member
Posts: 182
Joined: Mon May 17, 2021 8:05 pm
Contact:

How to manage scenes?

Post 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.
User avatar
GVovkiv
Party member
Posts: 668
Joined: Fri Jan 15, 2021 7:29 am

Re: How to manage scenes?

Post 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
User avatar
darkfrei
Party member
Posts: 1168
Joined: Sat Feb 08, 2020 11:09 pm

Re: How to manage scenes?

Post 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
Last edited by darkfrei on Fri Oct 15, 2021 6:38 am, edited 1 time in total.
:awesome: in Lua we Löve
:awesome: Platformer Guide
:awesome: freebies
User avatar
ddabrahim
Party member
Posts: 182
Joined: Mon May 17, 2021 8:05 pm
Contact:

Re: How to manage scenes?

Post 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.
User avatar
GVovkiv
Party member
Posts: 668
Joined: Fri Jan 15, 2021 7:29 am

Re: How to manage scenes?

Post 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
User avatar
dusoft
Party member
Posts: 482
Joined: Fri Nov 08, 2013 12:07 am
Location: Europe usually
Contact:

Re: How to manage scenes?

Post by dusoft »

User avatar
ddabrahim
Party member
Posts: 182
Joined: Mon May 17, 2021 8:05 pm
Contact:

Re: How to manage scenes?

Post 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.
User avatar
BulbaMander
Citizen
Posts: 65
Joined: Sat Dec 15, 2012 7:00 pm

Re: How to manage scenes?

Post 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.
It takes an idiot to do cool things. Thats why they're cool. :emo:
RNavega
Party member
Posts: 235
Joined: Sun Aug 16, 2020 1:28 pm

Re: How to manage scenes?

Post 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.
User avatar
BulbaMander
Citizen
Posts: 65
Joined: Sat Dec 15, 2012 7:00 pm

Re: How to manage scenes?

Post 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.
It takes an idiot to do cool things. Thats why they're cool. :emo:
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot] and 16 guests