Godot:BackgroundLoading
lastmod 2022-12-23created 2022-12-214 edits
When switching the main scene (such as going to a new level), you might want to show a screen that indicates loading progress. The main load method (ResourceLoader#Load, or just the load function in GDScript) blocks the thread, so the game will appear frozen and unresponsive until the resource is loaded. This page instead discusses how to use the ResourceInteractiveLoader class to create a smoother loading screen.
ResourceInteractiveLoader
The ResourceInteractiveLoader class lets you load a resource in multiple stages. Each time the poll function is called, a new stage is loaded and control is returned to the caller. Each stage is generally a sub-resource loaded by the main resource. For example, if you load a scene that loads 10 images, each image will be one stage.
Usage
Usage is generally as follows
Obtaining a ResourceInteractiveLoader
You can use the ResourceInteractiveLoader returned by this function to manage the loading operation.
Polling
This function advances loading by one step. Each call to poll loads the next stage of your resource. Keep in mind that each stage is one "atomic" resource, such as a single image or a single mesh, so loading may take several frames.
OK is returned if there are no errors, and ERR_FILE_EOF is returned when loading is finished. Any other return value means an error occurred and loading has stopped.
Loading progress (optional)
To check the current loading progress, use the following functions:
int ResourceInteractiveLoader::get_stage_count() const;
int ResourceInteractiveLoader::get_stage() const;
- get_stage_count returns the total number of stages needed for loading.
- get_stage returns the stage currently being loaded.
Forcing completion (optional)
Use this function to load the entire resource in the current frame, without any further steps.
Obtaining the resource
If everything went well, you can obtain the loaded resource with the function below.
See also
Favorite site
- Background loading — Godot Engine (stable) documentation in English
- How to create a loading screen - Godot Engine - Q&A