16. October 2023
Godot Resources in a nutshell
Godot has a lot cool features built in that make game development a joyful ride. One of this awesome features are Custom Resources.
Technically all the parts of Godot that save some data extend the Resource class. They are data containers that can contain all sort of data: translations, audio, textures and much more. This are all built-in Resources, but you can create also you own Custom Resources, that help you defining data structures and bring a lot of other features.
I learned about Custom Resources quite late, since I was able to all my data related problems with Dictionaries and JSON files. But now I work on bigger projects, like Futsal Manager where I need a lot of data. In such complex projects Custom Resources are a big game changer and make the development easier, with less errors and faster development.
Custom Resources
You can create your own Custom Resources simply by creating an empty Script file, giving it a class_name and extending it with the Resources class. Then you can define some variables, enums, constants and methods like in this example:
class_name MyResource
extends Resource
signal such_signal
enum SuchEnum { Hello, Custom, Resources }
@export var name:String
@export var id:int = 0
@export var values:Array[int]
@export var enumValue:SuchEnum
func get_sum() -> int:
var sum:int = 0
for value in values:
sum += value
return sum
func special_effect() -> void:
if randi() % 10 == 1:
such_signal.emit()
Now you can create a variable of the type MyResource and you get the functions and variables name with the editors autocomplete. So you are sure that you always use the correct variable name. With Dictionaries instead, you have to remember the names of the keys.
By using the @export annotation you can modify all your resources directly in the editor.
Save and load
Another cool feature is the built-in load and save to disk functionality. So you can replace all your JSON files with built-in Custom Resources.
Note: Only variables with @export annotation get sa