An autoload (short for auto-loaded node) is a node or scene that Godot automatically instantiates and adds as a direct child of the root node when your game starts. Autoloads are accessible from anywhere in your codebase without needing to load them beforehand.
Think of autoloads as global objects that hold data or methods you need throughout your game. They're perfect for things like creating a database of items, managing audio that should keep playing between maps like looping music, or input systems that many different parts of your game need to access.
For example, you might create an ItemDatabase
autoload to manage a collection of items throughout your game. In this example, each item has a unique ID (like "potion_red") mapped to its properties:
extends Node
var _items = {
"potion_red": {
"display_name": "Red Potion",
"type": "consumable",
"heal_amount": 50,
"price": 25,
"icon": preload("icons/health_potion.png")
},
"shield_wooden": {
"display_name": "Wooden Shield",
"type": "armor",
"defense": 5,
"price": 40,
"icon": preload("icons/wooden_shield.png")
}
}
func get_item(item_id: String) -> Dictionary:
return _items.get(item_id, null)
func get_random_item() -> Dictionary:
var random_item_id: String = _items.keys().pick_random()
return get_item(random_item_id)
Once registered as an autoload in the Project Settings, you can access it from any script. This is an example of a chest that gives the player a random item when opened:
func _on_chest_opened() -> void:
var item: Dictionary = ItemDB.get_random_item()
add_item_to_inventory(item)
Autoloads are not exactly singletonsWhile Autoloads are commonly called "singletons" in the Godot community, they're not exactly the same as the traditional Singleton design pattern.A singleton restricts the instantiation of a class to a single object, but autoloads are normal nodes with global access, not classes that restrict instantiation to a single object. Often, we use them for the same purpose as singletons in Godot, which is why people call them "Godot's singletons." External Links
Handpicked resources on the internet