Unload scene specific resources on demand #7381
Open
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This is an implementation of functionality for unloading all resources from the scene we are leaving. For example, we have several scenes (Preloader, postPreloader) that contain a significant number of resources such as images, audio, spine animations, atlases, JSON files, and so on. After these scenes have been loaded, once we transition to the so-called main scene, we want to remove (unload) all resources from the scenes that we do not expect to return to during the game’s runtime.
Plan for implementing the unloading of scene-specific resources on the GDevelop side:
An additional boolean parameter needs to be added to the "change scene" action. Enabling this parameter will indicate that all resources specific to the scene being exited should be unloaded. It is assumed that we will not return to this scene during the game’s runtime.
During the execution of the "change scene" action, the
_destroy
method is eventually called on the scene object (inruntimescene.ts
). At that point, the resource cleanup methoddisposeScene
—implemented in theResourceLoader
class—is invoked.The
disposeScene
method accepts the scene name as a parameter and creates aMap
where the keys are resource types (ResourceKind
) and the values are arrays ofResourceData
.In the
ResourceLoader
class, there is a field_resourceManagersMap
where the keys are resource types (ResourceKind
) and the values are resource managers. This makes it easy to correlate this map with the map of resources grouped by type since they share the same keys.In each resource manager class, a method
disposeByResourcesList
has been implemented, which takes an array of resources that need to be cleared. This method performs the cleanup of the resources in the provided list.