Godot tech bits #1: the Scenes autoload
Reasoning behind the Scenes autoload
The Scenes autoload is just that: an autoload with a lot of exported PackedScene variables, that I use to keep track of all the scenes that need to be instantiated from code.
I started to include it in every project because I tend to move stuff around a lot during development, thus breaking most of the load("res://path/to/my_resource.tres")
statements.
The only problem I found with this approach is that I end up with a huge list of scenes which is hard to navigate.
Thankfully Godot 4 introduced separators for exported variables, so this shouldn’t be an issue in the future.
Detecting Unassigned Variables
This should have been it, but I found out that I often forget to assign a scene to the newly created exported variable, and only realize it later on while running the game.
To solve that issue I added this nifty bit in the _ready function to crash the game instantly if there is an unassigned member variable of type PackedScene:
func _ready():
for p in get_property_list():
if p["class_name"] == "PackedScene":
assert(self[p["name"]], "Scene << %s >> was not set!" % p["name"])
Leave a comment
Log in with itch.io to leave a comment.