Making a progression bar

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.
User avatar
Gunroar:Cannon()
Party member
Posts: 1088
Joined: Thu Dec 10, 2020 1:57 am

Making a progression bar

Post by Gunroar:Cannon() »

How do I draw a progression bar that draws the progress of something that takes long to do(like loading a map) so that a player won't think the game has frozen? Do I use coroutines or what? I don't know so pls help...
The risk I took was calculated,
but man, am I bad at math.

-How to be saved and born again :huh:
User avatar
darkfrei
Party member
Posts: 1172
Joined: Sat Feb 08, 2020 11:09 pm

Re: Making a progression bar

Post by darkfrei »

Do you load all data in one load/update tick?
:awesome: in Lua we Löve
:awesome: Platformer Guide
:awesome: freebies
User avatar
Gunroar:Cannon()
Party member
Posts: 1088
Joined: Thu Dec 10, 2020 1:57 am

Re: Making a progression bar

Post by Gunroar:Cannon() »

yes
The risk I took was calculated,
but man, am I bad at math.

-How to be saved and born again :huh:
User avatar
darkfrei
Party member
Posts: 1172
Joined: Sat Feb 08, 2020 11:09 pm

Re: Making a progression bar

Post by darkfrei »

Gunroar:Cannon() wrote: Thu Jan 07, 2021 9:53 am
darkfrei wrote: Thu Jan 07, 2021 9:11 am Do you load all data in one load/update tick?
yes
So, you are need to load in multiple ticks.
:awesome: in Lua we Löve
:awesome: Platformer Guide
:awesome: freebies
User avatar
Gunroar:Cannon()
Party member
Posts: 1088
Joined: Thu Dec 10, 2020 1:57 am

Re: Making a progression bar

Post by Gunroar:Cannon() »

Hmm, so draw as I go along? But can't I use coroutines or something?
PS: I noticed you added an avatar, nice.
The risk I took was calculated,
but man, am I bad at math.

-How to be saved and born again :huh:
dezoitodemaio
Prole
Posts: 15
Joined: Mon Oct 26, 2020 2:02 pm

Re: Making a progression bar

Post by dezoitodemaio »

Just load a few objects at every update() instead of loading the whole thing.

Or create a corountine that loads the map and do a resume()/yield() after loading some amount of objects, do that untill everything is loaded.
RNavega
Party member
Posts: 244
Joined: Sun Aug 16, 2020 1:28 pm

Re: Making a progression bar

Post by RNavega »

I think you can do this using Data objects, more specifically FileData, created by reading a file from disk with love.filesystem.newFileData.

FileData is a subclass of Data, so you can know the amount of bytes that the loaded file data represents. You can also calculate the total bytes from all assets that you're trying to load at a given moment while debugging your game (just remove that code for the release version, obviously). Then you know how many bytes you're trying to load at that moment, and how much each loaded asset represents of that total = a percentage for a progress bar.

You can create images from a FileData (FileData > ImageData > love.graphics.newImage overload that takes ImageData), and also audio (FileData > love.audio.newSource overload that takes a FileData), possibly others.

The only thing is how to asynchronously (i.e non-blockingly) load the FileData objects from files on disk. Lua coroutines are blocking (the ones that say they aren't, just yield() control back when there's no work to do). The only way is using the love.thread module, loading FileData objects in a thread, pushing them into a Channel and then popping from that Channel back on the main thread so that you can retrieve the FileData and use them to update the progress bar and read them into other LÖVE objects (images, audio etc).
So you can keep drawing animated graphics and updating the program while loading files in the background, on another thread.
User avatar
pgimeno
Party member
Posts: 3548
Joined: Sun Oct 18, 2015 2:58 pm

Re: Making a progression bar

Post by pgimeno »

Just make sure you don't load any GL images in another thread! You can load them into ImageData in the thread, then in the main thread, convert them all to Image at once.
User avatar
Gunroar:Cannon()
Party member
Posts: 1088
Joined: Thu Dec 10, 2020 1:57 am

Re: Making a progression bar

Post by Gunroar:Cannon() »

Can't I call the loading method in a coroutine and have value for progression of the loading that love.draw uses to draw the bar?
The risk I took was calculated,
but man, am I bad at math.

-How to be saved and born again :huh:
User avatar
pgimeno
Party member
Posts: 3548
Joined: Sun Oct 18, 2015 2:58 pm

Re: Making a progression bar

Post by pgimeno »

Yes you can do that; you'd then need to yield after every asset is loaded.

You can also update the screen with love.graphics.present(), without using the update and draw callbacks. In that case you need to be careful to always call love.graphics.clear() to clear the screen before you draw the progress bar, though.
Post Reply

Who is online

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