Run love.draw before love.update

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
User avatar
ddabrahim
Party member
Posts: 201
Joined: Mon May 17, 2021 8:05 pm
Contact:

Run love.draw before love.update

Post 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.
User avatar
ddabrahim
Party member
Posts: 201
Joined: Mon May 17, 2021 8:05 pm
Contact:

Re: Run love.draw before love.update

Post 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?
User avatar
BrotSagtMist
Party member
Posts: 665
Joined: Fri Aug 06, 2021 10:30 pm

Re: Run love.draw before love.update

Post by BrotSagtMist »

You only need to call love.timer.step() after the level load to reset delta.
obey
User avatar
darkfrei
Party member
Posts: 1216
Joined: Sat Feb 08, 2020 11:09 pm

Re: Run love.draw before love.update

Post 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.
:awesome: in Lua we Löve
:awesome: Platformer Guide
:awesome: freebies
User avatar
ddabrahim
Party member
Posts: 201
Joined: Mon May 17, 2021 8:05 pm
Contact:

Re: Run love.draw before love.update

Post 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.
MrFariator
Party member
Posts: 563
Joined: Wed Oct 05, 2016 11:53 am

Re: Run love.draw before love.update

Post 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.
User avatar
milon
Party member
Posts: 472
Joined: Thu Jan 18, 2018 9:14 pm

Re: Run love.draw before love.update

Post 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.
Any code samples/ideas by me should be considered Public Domain (no attribution needed) license unless otherwise stated.
User avatar
ddabrahim
Party member
Posts: 201
Joined: Mon May 17, 2021 8:05 pm
Contact:

Re: Run love.draw before love.update

Post 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 :awesome:
Post Reply

Who is online

Users browsing this forum: Google [Bot], Semrush [Bot] and 5 guests