Skip to reading
GDScript Programming & Godot

46. Resources, load and preload

Edition 0.86.4.1 HF1

0.86.4.1 Hotfix 1 edition. Earlier lessons and screenshots retain their original version context; revised references and supplements describe the newer behavior.

Download PDFView illustrations
Code example
const ADVENTURE_BASE = preload("res://scripts/runtime/script_base.gd")
Code example
func run() -> bool:
    var script_resource: Resource = load("res://scripts/runtime/script_base.gd")
    return script_resource == ADVENTURE_BASE

preload requires a constant path and loads while the script is prepared. load/ResourceLoader.load can use a runtime path. A .gd resource is a Script; call new to make its instance. A PackedScene uses instantiate instead.

res:// points to the resource pack/project of the application currently running. In PCA’s editor that is the PCA application; in a built game it is the runner and mounted game content. A script record ID is not a file at res://scripts/your_id.gd.

ResourceLoader.exists can check a resource path before load. After loading, check the actual type. For raw packaged text/data, include the file through the export workflow; imported resources may have a converted format.

Figure 15. Imported asset metadata and Godot resource paths are different from the visible asset name.

Page 55 of 378Permalink
Go to page