Page 1 of 1
[SOLVED]How to implement new levels correctly
Posted: Fri May 05, 2017 10:35 am
by Markku
I can make a game level with load, update, draw and everything works (which is a miracle, thanks devs!).
However, if my character then should go to a new level with new graphics, music etc., how do I can start anew? I would need to save the progress for the character and then load new resources. Also, should I release memory etc. from previous level resources? Is there a recommended structure or tutorial for this...?
I tried to find this in Wiki and Google, but no luck...
Re: How to implement new levels correctly
Posted: Fri May 05, 2017 12:55 pm
by RaycatRakittra
Markku wrote: ↑Fri May 05, 2017 10:35 am
[...] However, if my character then should go to a new level with new graphics, music etc., how do I can start anew? [...] Also, should I release memory etc. from previous level resources? Is there a recommended structure or tutorial for this...?
It's your game. You decide what 'structure' you want to use.
An example of a simple implementation would keep track of the level number and just draw different things on the screen but, this is during gameplay; it's not a level select. A better implementation would be creating a sort of class for Levels. They would have their own load(), update(), and draw() functions that you would call in the main game loop. You could even include a cleanup() function for when you switch levels (though I don't think that memory should ever be an issue). An ever better implementation treats the levels as objects and registers them with a higher-order function that tells the game what level should be rendering. However, the last one is an opinion; you have to strike a balance with abstracting what would be tedious for you - the programmer - and what would essentially be a rocket launcher for an anthill.
Markku wrote: ↑Fri May 05, 2017 10:35 am
I would need to save the progress for the character and then load new resources.
Again, you decide what structure you want to use. Your character's position and state should be saved somewhere. If you want to implement some type of hot-swapping solution, it shouldn't be too difficult to load a new level around the player.
Re: How to implement new levels correctly
Posted: Fri May 05, 2017 1:02 pm
by MasterLee
There is no need to release memory simple secure that there is no more reference to the objects.
On the other hand there is no real recommended structure for changing levels.
I use
but that is specific i program my stuff
Re: How to implement new levels correctly
Posted: Fri May 05, 2017 1:17 pm
by OnACoffeeBreak
I haven't used it myself yet, but wouldn't
HUMP gamestate library be useful here?
Re: How to implement new levels correctly
Posted: Fri May 05, 2017 3:09 pm
by Markku
Ok, thanks, I try to see if I can use those suggestions. Another way I see I could do this is that I make my own resource load function and in the beginning Löve calls it from love.load(), then I do it myself, when a level is complete. Let's see how this works...
Thanks!
Re: How to implement new levels correctly
Posted: Fri May 05, 2017 3:33 pm
by Markku
...and after quick testing, one resource loading function seems to work. I just make a love.audio.stop() and then reload stuff to same variables, so when love.draw() works, it has new content to work with.
This is solved then...