Simple, modular, reactive.
The App
class manages your app’s shared behaviors. It is effectively the root object of your app, and it works to manage some interactions with the system. Use your App
class to handle the following tasks:
- Load resources and modules of your application. (
Sync/Async
depending on your needs) - Responding to signals, such as loading process, completion of loading, and others.
-
Download AppDelegate, unzip and copy files to your Godot project's
res://addons/app_delegate
directory. -
You must add the
app.gd
script to theAutoLoad
(as in the example below). To do this, selectProject > Project Settings
and switch to theAutoLoad
tab. -
Create a
package.gd
file in theres://
directory following the example below and specify the path to the folder with your modules.
extends Reference
func package():
return {
"name": "APP_NAME",
"version": "0.0.0",
"modules_path": "res://modules/"
}
-
In the
modules
folder, create a folder with the name of the new module.For example, let's create the "menu" module.
assets
folder is used to store the various resources (image/audio, etc.) related to this module.menu.tscn
is the scene that hasCanvasLayer
as its root node.menu.gd
is a script linked to the root node.package.gd
is a script that contains important information/constants related to this module.
-
Extend
menu.gd
from theOKModule
class. -
Fill
package.gd
with the default initial information.
- Loading and caching modules and all their resources
# Asynchronous loading of 1 module
App.load_module("menu")
# Synchronous loading of 1 module
App.load_module("menu", false)
# Asynchronous loading of several modules
App.load_modules(["menu", "player", "game"])
# Synchronous loading of several modules
App.load_modules(["menu", "player", "game"], false)
- Loading with pending completion
# On the example of async loading 1 module
yield(App.load_module("menu"), "completed")
print("The menu module is loaded and ready.")
# On the example of async loading with several modules
yield(App.load_modules(["menu", "player", "game"]), "completed")
print("All modules is loaded and ready.")
- Loading with pending completion and a returned module
# On the example of async loading 1 module
var menu = yield(App.load_module("menu"), "completed")
menu.some_method()
# On the example of async loading with several modules
var modules = yield(App.load_modules(["menu", "player", "game"]), "completed")
modules.menu.some_method()
modules.player.some_method()
modules.game.some_method()
-
Waiting for module loaded anywhere in your code
- You can also use "await_module" instead of "load_module" and "await_modules" instead of "load_modules" to wait for loading to complete anywhere in your code. (For example, if at some point in your code, you need some module to be already loaded)
- If the module is already loaded at that point, your code will continue unchanged. And if the module doesn't exist and isn't currently in the loading process, it will start loading:
yield(App.await_module("menu"), "completed")
print("The menu module is loaded and ready.")
yield(App.await_modules(["menu", "player", "game"]), "completed")
print("All modules is loaded and ready.")
var module = yield(App.await_module("menu"), "completed")
print("The menu module is loaded and ready.")
var modules = yield(App.await_modules(["menu", "player", "game"]), "completed")
print("All modules is loaded and ready.")
If you try to unload a module when it is in the process of loading, the loading will be canceled and the module will be completely unloaded.
- Unloading modules and all their resources
# Unloading of 1 module
App.unload_module("menu")
# Unloading of several modules
App.unload_modules(["menu", "player", "game"])
- Unloading with pending completion
# Unloading of 1 module
yield(App.unload_module("menu"), "completed")
print("The menu module is completely unloaded.")
# Unloading of several modules
yield(App.unload_modules(["menu", "player", "game"]), "completed")
print("All modules is completely unloaded.")
func _ready():
App.connect("load_progress", self, "_on_load_progress")
func _on_load_progress(value: float):
print(value) # from 0.0 to 100.0
- Check if the module is in the process of loading
if App.is_module_loading("menu"):
print("Module in the process of loading")
- Check if the module is loaded
if App.has_module("menu"):
print("The module is loaded")
- Calling methods (Module must be loaded)
# Inside the module
some_method()
# From outside the module
App.module("menu").some_method()
- Getting data from
package.gd
(Module must be loaded)
# Inside the module
var version = package("version")
# From outside the module
var version = App.module("menu").package("version")
- Getting module resources (Module must be loaded)
# Inside the module
var img1: StreamTexture = asset("some_image.png") # assets/ folder
var img2: StreamTexture = asset("main/some_image.png") # assets/main/ folder
var array: Array = assets("main") # all resources in assets/main/ folder
# From outside the module
var img1: StreamTexture = App.module("menu").asset("some_image.png") # assets/ folder
var img2: StreamTexture = App.module("menu").asset("main/some_image.png") # assets/main/ folder
var array: Array = App.module("menu").assets("main") # all resources in assets/main/ folder