Page 1 of 1
Run love.draw before love.update
Posted: Wed Oct 26, 2022 7:45 pm
by ddabrahim
Hi everyone!
I am trying to implement transition between levels in my project.
The concept is very simple. Each level has its own load, update and draw method.
The load methods are structured like so:
Code: Select all
loadLevel1()
--delete all resources
--load level 1 resources
gameState = "level1"
end
loadlLevel2()
--delete all resources
--load level2 resources
gameState = "level2"
end
And this is how update and draw looks like
Code: Select all
love.update
if gameState == "level1" then updateLevel1()
if gameState == "level2" then updateLevel2()
end
love.draw()
if gameState == "level1" then drawLevel1()
if gameState == "level2" then drawLevel2()
end
The problem is that however, each time I transition from one level to an other the screen get freezed for a few seconds and while it is freezed, update get executed and the game progresses in the background. By the time the screen draw the new level, it is already progressed.
I would like to somehow avoid this and delay the update until draw is executed. I tried using a boolean but it seems despite the fact my screen is freezed, love.draw continue to execute and set the boolean to true in the background.
Would anyone have any idea how to go about this?
Thank you.
Re: Run love.draw before love.update
Posted: Wed Oct 26, 2022 8:33 pm
by ddabrahim
I have just realised after posting this, it is not update running in the background but the delta time is accumulating while the screen is freezed and because I do a lot of calculations with delta, it is gives an initial boost to everything the moment the screen is drawn.
Not sure what can I do about this either. Would anyone have any idea?
Re: Run love.draw before love.update
Posted: Wed Oct 26, 2022 8:46 pm
by BrotSagtMist
You only need to call love.timer.step() after the level load to reset delta.
Re: Run love.draw before love.update
Posted: Wed Oct 26, 2022 9:00 pm
by darkfrei
Or if the dt is too huge set it to the not too huge.
if dt >0.05 then dt = 0.05 end
Maybe freeze by the slow computer, but 20 fps is always too slow.
Re: Run love.draw before love.update
Posted: Thu Oct 27, 2022 6:22 am
by ddabrahim
Thanks a lot for the suggestions, both has solved the problem with the boost after loading.
Regarding the freeze, I'm loading lots of large resources, hundreds of images and sounds. I suspect it maybe got something to do with it in case loading resources is not asynchronous in Love2D and my hard drive is slow (yes despite the fact I am on a 2019 Mac, its got a good old HDD). It is something I need to solve in the future but I am not too worried about it right now.
Re: Run love.draw before love.update
Posted: Thu Oct 27, 2022 10:10 am
by MrFariator
All functions that load data or assets from files in love2d are synchronous, meaning they will freeze main thread until the function finishes. As such, it's no surprise things might freeze up if you're loading a whole bunch of files at once within a single frame or two. In order to make them behave asynchronously, you'll need to use threads. There are some libraries like
lily that can help you along with that.
Re: Run love.draw before love.update
Posted: Thu Oct 27, 2022 2:15 pm
by milon
ddabrahim wrote: ↑Wed Oct 26, 2022 7:45 pm
Hi everyone!
I am trying to implement transition between levels in my project.
The concept is very simple. Each level has its own load, update and draw method.
The load methods are structured like so:
Code: Select all
loadLevel1()
--delete all resources
--load level 1 resources
gameState = "level1"
end
loadlLevel2()
--delete all resources
--load level2 resources
gameState = "level2"
end
And this is how update and draw looks like
Code: Select all
love.update
if gameState == "level1" then updateLevel1()
if gameState == "level2" then updateLevel2()
end
love.draw()
if gameState == "level1" then drawLevel1()
if gameState == "level2" then drawLevel2()
end
Setting aside the actual question, for the love of your sanity don't do it this way! That quickly becomes a nightmare to manage. A better way is to use "level" (or gameState) as a variable and make everything more dynamic. Here's one example:
Code: Select all
function love.load()
level = "level1" -- the default is to start at level 1, of course
end
function loadLevel(level)
--delete all resources
--load (level) resources
gameState = level
end
love.update()
updateLevel(level)
end
love.draw()
drawLevel(level)
end
You can even store the various update/draw functions in a table and index them by the level name and simply call the functions that way. That's the approach I used in
Wordie if you want to look at an example.
Re: Run love.draw before love.update
Posted: Thu Oct 27, 2022 5:29 pm
by ddabrahim
MrFariator wrote:In order to make them behave asynchronously, you'll need to use threads. There are some libraries like lily that can help you along with that.
I plan to look in to threads and asynchronous programming when I have some time to dedicate to this. The library looks simple to use, maybe I'll give it a try. Thank you.
milon wrote:That quickly becomes a nightmare to manage. A better way is to use "level" (or gameState) as a variable and make everything more dynamic.
Yes it is not great, I'll consider your recommendation. I always try to find ways to better organise my code but I'm afraid it is too late, I have lost my sanity long time ago