A quick guide on how to load scenes in the background and show a loading screen in Godot 4! #godot #gameengine #tutorial #gamedeveloper #gamedev #loading #scene
Quick and short, well edited, has all the info I needed to know. Just perfect! Thank you so much and I hope you can upload more often because your videos are really amazing
Found one of your videos yesterday and have started to binge watch your channel. I like your aesthetic very much, and the short explanatory video format is very good for when you need fast instructions.
Thank you so much, I was really confused on how to get this process started. My code was close but wasn't working, this helped me adjust it so it worked :)
In this example you are using get_tree().change_scene_to_packed(newScene) which automatically unloads every other scene. Is there a way to make it work with get_tree().root.add_child(newScene)? (That one throws an error, saying it cannot handle a PackedScene. Consider "sceneName" holding a path like in your example.) In my case it is not desirable to have everything unloaded... or is it a better approach to push everything which is supposed to be available (almost) permanently into global/autoload?
I think I figured it out: loaded_scene = ResourceLoader.load_threaded_get(saved_path) var scene = loaded_scene.instantiate() get_tree().root.add_child(scene) Please correct me if I am wrong!
@@DemKartal You are absolutely correct! - A packedScene is an object that represents a scene. We can think of it as an object that contains all the information needed to instantiate/duplicate the nodes in this scene. So a packedScene is like a tree template, and we can invoke instantiate() to clone this structure. - When we invoke the instantiate() method on a packedScene, we get a new scene instance, and the instantiate method returns the root node of this structure. - add_child expects a node as an argument, so this is why it didn't work at first. I hope this helps!
Excellent video. Do you know if background loading is possible with single thread mode? Now that the web export has this single thread option many people like me will use it to avoid problems with the browsers but I can't find a way to load scenes without freezes.
The documentation of 4.3 for background loading is: docs.godotengine.org/en/stable/tutorials/io/background_loading.html#background-loading And it doesn't specifically mention your use case. If you want to do background loading on the main thread without freezes, I think you will have to interleave the execution of the game and loading the resource. You can manually read the file in small chunks during each frame (e.g., in the _process() function). This will reduce the risk of blocking the main thread. Consider using `get_buffer()`. docs.godotengine.org/en/stable/classes/class_fileaccess.html#class-fileaccess-method-get-buffer You can adjust the length as needed. After that you will have the file content, and you can load them as a resource. Hopefully, this will work!
You can use a Timer node, and use the timeout signal. Then, you can set the wait_time, and start the timer. docs.godotengine.org/en/stable/classes/class_timer.html
Very useful, but I have one question. If I wanted to use this function but make the loading wait for a function in the next scene to be run how would i do that?
If you want to run a function before switching to the new scene, you can run this function before the line `get_tree().change_scene_to_packed(newScene)`.
Hola, no se mucho ingles pero quisiera que me asesoren. Cuando testeo mi juego en mi PC todo funciona, las instancias, las escenas, todo, el problema es cuando oo exporto a android. Algumas veces no entra al juego, otras veces se queda atascado en el menu principal y ahora que aplique este tutorial el resource loader me lanza un 2, que siignifica que hubo un error. No se que es exactamente soy principiante en esto y no quiero abandonarlo por un bug confuso
Hello! Some possible reasons to consider: - Make sure that you have the correct path to the resource. For example, ensure that the paths use `/` as the separator instead of `\`. - Mobile devices usually have less memory available (compared to PC). If your game uses a lot of resources, you might run out of memory, causing resources to fail to load. Maybe try to reduce the size of the textures, models, or other resources, if they are large.
What determines a scene being finished loading? i have a scene with procedurally generated a world, but is that scene immediately "loaded", while the generation still continues to happen?
When we talk about a scene being loaded, we are generally referring to the process of taking a scene file (like .tscn) from the disk and bringing it into memory. This way we can interact and instantiate the scene. If you have scripts attached to a node in your scene that perform procedural generation when the scene starts up, that logic will execute after the scene is instantiated from the loaded data.
@@JumboGamedevthe mistake was that i compensated the process with a while true loop! a mistake on my part, i solved it by adding "await get_tree().process_frame". thank you for the tutorial!
Hello! I just saw this and was able to implement it into my game. However, one question: I notice that my progress number goes to 50% then 0% but everything loads just fine. Hiding the number (replacing with just Loading...) makes it look good and still works correctly, but I am curious as to why the progress stopped at 50% and then went to 0% before loading the finished scene?
Hello! I think the behavior of load_threaded_get_status depends on the scene being loaded, so it is hard to exactly know why this may be happening. It is an interesting scenario; my guess is that at some point through the loading process, more dependencies were discovered (or needed to be loaded), and these dependencies were too many & enough to make the amount that was already loaded negligible. You may find these resources helpful, if you want to go into more detail ( github.com/godotengine/godot/blob/4.2/core/io/resource_loader.cpp#L509 ), ( github.com/godotengine/godot/issues/56882#issuecomment-1377612987 ) Side note: I think this behavior may change depending on the version of Godot or the OS you are using.
@@JumboGamedev Thanks for those links, was an interesting read and glad to know I'm not (yet) going crazy. After fooling with it some more, I noticed that the progress goes rapidly to 49% - kinda sits there, goes to 50% and then the scene switches successfully. I think the "save sanity" way is for me to just treat 50% as "100%" and then it will look decent to a player while still showing "hey the game isn't frozen". Or go with some other effect and drop the number (a sprite animating, etc). The adventures of game dev - when things you didn't think would be a quirk show all their quirks :D
@@VRNocturne You are very welcome. I agree, I would also stretch/redistribute the first 50% to cover a substantial part of the 100%. You may want to use a non-linear function that will take the progress value, and it will return the stretched value. For instance, you may use a power function. I found that y = x^0.2 (where x is the progress value) gives a decent distribution. Indeed, game dev is really interesting!
It seems that you have `progress` as a string and you are trying to get a character that is out of bound. I think you need to make sure that progress is an array and you are accessing the value in index 0.
Thanks for the feedback! I will try to work on the audio more. It would be helpful if you let me know what aspect of the sound needs to be adjusted? does it need to be louder?
I would use a different sound effect because it sounds just like a chat message. I have that same issue on all of your videos, but I appreciate the content.@@JumboGamedev
Okay, I'd just thought I'd drop another bit of knowledge I ended up getting from the Godot contributors chat. I was getting some exceptionally nasty errors and scene corruption issues awhile after with error messages like 'circular dependency'. If you are ever getting these errors, check the referenced scene and script, go into the script and change any 'preload' scenes you have to 'load' this should then fix the problem, the way they've gone about it is cancer though so I'm going to see if they can post up either some kind of documentation or update their error to explain this better because it was the first time I'd encountered this and it's terrifying when you first get it. Edit: Not doing this can cause huge problems even when you're just in the editor because of the way preloading works it seems even I even found myself having problems even after I removed the loading scripts.
"Scene file [scene_name] appears to be invalid/corrupt"... This thing has been giving me a headache for a few days, I'll try your fix and see if it works. Edit: it works
The aesthetics and color scheme are gorgeous
Thank you very much!
Great tutorial. Thank you. I only wish example code were a bit bigger. :) Specially when on second part of the screen is just 0% most of the time
Thank you for the feedback, much appreciated!
I will try to do better for future tutorials : )
Quick and short, well edited, has all the info I needed to know. Just perfect! Thank you so much and I hope you can upload more often because your videos are really amazing
Thank you very much! I am planning to upload more.
This was very useful and packed with a lot of info in under 3 minutes.
Glad to hear! Thanks!
Short and to the point. Love it and subscribed! :)
Thank you very much : )
This tutorial is genuinely one of the best if not the best I've ever experienced. Yeah the code viewport should have been larger. But Very nice.
Thank you very much!
The most efficient tutorial I have ever watched. Subscribed and Bell on.
Thank you, much appreciated!
Found one of your videos yesterday and have started to binge watch your channel. I like your aesthetic very much, and the short explanatory video format is very good for when you need fast instructions.
Thank you very much!
Thank you so much! I'm so glad to finally be able to have loading screens, I'm definitely saving this code!
You are very welcome!
So simple, worked first try. Thank you!
You are very welcome!
Clean, quick, concise! Great video! Just the short tip I needed to add loading screen! One for pause menu was useful as well!
Thank you very much!
Extremely useful. I will definitely add this to my game, too.
Thank you very much!
Such an outstandingly made video. Great job and thank you for explaining it so clearly!
Thank you very much!
Thank you! Great tutorial with a very clear and accurate delivery
Thank you very much!
Thanks so much! I was wondering how loading would work.
You are very welcome!
Thank you so much, I was really confused on how to get this process started. My code was close but wasn't working, this helped me adjust it so it worked :)
You are very welcome!
short, clear, and clean. Thanks man
You are very welcome!
One of the Best explainations how it works. Keept it up!
Thank you very much!
The video is very useful and your video editing is very good. Keep up the good work.
Thank you very much!
Great tutorial brother!
Love from India ❤
Thank you! ❤
Very clearly explained. Gonna wrap this into some UI components for my game this afternoon.
Thanks! Great idea.
Great tutorial. Keep up the good work.
Thank you!🙂
welcome back
Thank you : )
Awesome, keep em coming!
Thank you!
Excellent explained, thanks!
Thank you very much!
Great tutorial. Thank you.
Thank you. You are welcome : )
Damn you sound like a calm Jon Zherka
Interesting analogy!
subbed and liked, thanks
Thank you very much!
best channel ever
Thank you very much!
Thank you, works very well! For some reason, it jumps from 0.4 to 1.0, but maybe it's just the scene i'm loading...
You are very welcome!
Hey, good to see new video from u. Is it new thing background loading implemented in last versions? or it was here long time?
Hey, thank you!
Yes, it is a new way to do background loading added in Godot 4 ( github.com/godotengine/godot/pull/36640 )
Nice tuts!! thanks!
Thank you 😁
Yoooo look who's back
It is me 😁
In this example you are using get_tree().change_scene_to_packed(newScene) which automatically unloads every other scene. Is there a way to make it work with get_tree().root.add_child(newScene)? (That one throws an error, saying it cannot handle a PackedScene. Consider "sceneName" holding a path like in your example.)
In my case it is not desirable to have everything unloaded... or is it a better approach to push everything which is supposed to be available (almost) permanently into global/autoload?
I think I figured it out:
loaded_scene = ResourceLoader.load_threaded_get(saved_path)
var scene = loaded_scene.instantiate()
get_tree().root.add_child(scene)
Please correct me if I am wrong!
@@DemKartal You are absolutely correct!
- A packedScene is an object that represents a scene. We can think of it as an object that contains all the information needed to instantiate/duplicate the nodes in this scene. So a packedScene is like a tree template, and we can invoke instantiate() to clone this structure.
- When we invoke the instantiate() method on a packedScene, we get a new scene instance, and the instantiate method returns the root node of this structure.
- add_child expects a node as an argument, so this is why it didn't work at first.
I hope this helps!
@@JumboGamedev Thank you for confirmation and the detailed explanation!
@@DemKartal You are very welcome!
Excellent video. Do you know if background loading is possible with single thread mode? Now that the web export has this single thread option many people like me will use it to avoid problems with the browsers but I can't find a way to load scenes without freezes.
The documentation of 4.3 for background loading is: docs.godotengine.org/en/stable/tutorials/io/background_loading.html#background-loading
And it doesn't specifically mention your use case.
If you want to do background loading on the main thread without freezes, I think you will have to interleave the execution of the game and loading the resource. You can manually read the file in small chunks during each frame (e.g., in the _process() function). This will reduce the risk of blocking the main thread. Consider using `get_buffer()`. docs.godotengine.org/en/stable/classes/class_fileaccess.html#class-fileaccess-method-get-buffer
You can adjust the length as needed. After that you will have the file content, and you can load them as a resource. Hopefully, this will work!
@@JumboGamedev Thank you, I will try that!
Goated
Thank you very much!
Perfect! + sub
Thank you!
How do I increase the loading time by, say, 5 seconds after the scene has finished loading?
You can use a Timer node, and use the timeout signal. Then, you can set the wait_time, and start the timer. docs.godotengine.org/en/stable/classes/class_timer.html
Very useful, but I have one question. If I wanted to use this function but make the loading wait for a function in the next scene to be run how would i do that?
If you want to run a function before switching to the new scene, you can run this function before the line `get_tree().change_scene_to_packed(newScene)`.
Hola, no se mucho ingles pero quisiera que me asesoren. Cuando testeo mi juego en mi PC todo funciona, las instancias, las escenas, todo, el problema es cuando oo exporto a android. Algumas veces no entra al juego, otras veces se queda atascado en el menu principal y ahora que aplique este tutorial el resource loader me lanza un 2, que siignifica que hubo un error. No se que es exactamente soy principiante en esto y no quiero abandonarlo por un bug confuso
Hello!
Some possible reasons to consider:
- Make sure that you have the correct path to the resource. For example, ensure that the paths use `/` as the separator instead of `\`.
- Mobile devices usually have less memory available (compared to PC). If your game uses a lot of resources, you might run out of memory, causing resources to fail to load. Maybe try to reduce the size of the textures, models, or other resources, if they are large.
What determines a scene being finished loading? i have a scene with procedurally generated a world, but is that scene immediately "loaded", while the generation still continues to happen?
When we talk about a scene being loaded, we are generally referring to the process of taking a scene file (like .tscn) from the disk and bringing it into memory. This way we can interact and instantiate the scene. If you have scripts attached to a node in your scene that perform procedural generation when the scene starts up, that logic will execute after the scene is instantiated from the loaded data.
@@JumboGamedev Thank you! That's what I thought. Do you know would I add a loading bar in my case?
@@MrGold-zn1xo Yes, I think it is a good idea to add a loading bar.
my game still freezes when this process happens? it may have something to do with the fact that im using an autoloaded object but im not sure
If your game freezes at some point, this likely means that there is some operation that blocks the main loop. It doesn't have to be the autoload.
@@JumboGamedevthe mistake was that i compensated the process with a while true loop! a mistake on my part, i solved it by adding "await get_tree().process_frame". thank you for the tutorial!
Hello!
I just saw this and was able to implement it into my game.
However, one question: I notice that my progress number goes to 50% then 0% but everything loads just fine.
Hiding the number (replacing with just Loading...) makes it look good and still works correctly, but I am curious as to why the progress stopped at 50% and then went to 0% before loading the finished scene?
Hello!
I think the behavior of load_threaded_get_status depends on the scene being loaded, so it is hard to exactly know why this may be happening. It is an interesting scenario; my guess is that at some point through the loading process, more dependencies were discovered (or needed to be loaded), and these dependencies were too many & enough to make the amount that was already loaded negligible. You may find these resources helpful, if you want to go into more detail ( github.com/godotengine/godot/blob/4.2/core/io/resource_loader.cpp#L509 ), ( github.com/godotengine/godot/issues/56882#issuecomment-1377612987 )
Side note: I think this behavior may change depending on the version of Godot or the OS you are using.
@@JumboGamedev Thanks for those links, was an interesting read and glad to know I'm not (yet) going crazy.
After fooling with it some more, I noticed that the progress goes rapidly to 49% - kinda sits there, goes to 50% and then the scene switches successfully. I think the "save sanity" way is for me to just treat 50% as "100%" and then it will look decent to a player while still showing "hey the game isn't frozen".
Or go with some other effect and drop the number (a sprite animating, etc).
The adventures of game dev - when things you didn't think would be a quirk show all their quirks :D
@@VRNocturne You are very welcome. I agree, I would also stretch/redistribute the first 50% to cover a substantial part of the 100%. You may want to use a non-linear function that will take the progress value, and it will return the stretched value. For instance, you may use a power function. I found that y = x^0.2 (where x is the progress value) gives a decent distribution.
Indeed, game dev is really interesting!
Why does the error 'Invalid get Index "progress" (in base string)' appear?
It seems that you have `progress` as a string and you are trying to get a character that is out of bound. I think you need to make sure that progress is an array and you are accessing the value in index 0.
@@JumboGamedev Thanks
@@velixgd9312 You are welcome!
nice video! thanks. but...your chatting sound in the video keeps making me check my gogole chat~ aha
Thanks for the feedback! I will try to work on the audio more. It would be helpful if you let me know what aspect of the sound needs to be adjusted? does it need to be louder?
I would use a different sound effect because it sounds just like a chat message. I have that same issue on all of your videos, but I appreciate the content.@@JumboGamedev
Okay, I'd just thought I'd drop another bit of knowledge I ended up getting from the Godot contributors chat. I was getting some exceptionally nasty errors and scene corruption issues awhile after with error messages like 'circular dependency'. If you are ever getting these errors, check the referenced scene and script, go into the script and change any 'preload' scenes you have to 'load' this should then fix the problem, the way they've gone about it is cancer though so I'm going to see if they can post up either some kind of documentation or update their error to explain this better because it was the first time I'd encountered this and it's terrifying when you first get it.
Edit: Not doing this can cause huge problems even when you're just in the editor because of the way preloading works it seems even I even found myself having problems even after I removed the loading scripts.
Thank you for sharing!
"Scene file [scene_name] appears to be invalid/corrupt"... This thing has been giving me a headache for a few days, I'll try your fix and see if it works.
Edit: it works
In case anyone is wondering how to do a progress bar for loading.
loadingProgressBar.value = floor(progress[0] * 100)
Thanks for sharing!