Table of Contents
Stop using JSON and use Custom Resources
I love JSON just as much as any other developer, but I see it overused so often: Someone wants to create a quick save implementation, or a store item data for an inventory, or keep track of a few variations of data in their game... the list goes on.
Custom Resources to the rescue!
What is a Custom Resource?
A Custom Resource extends Godot's base Resource class and has many benefits. First of all, you get built in Editor support via the Inspector, just like all other Godot native Resource types. You can save resources to the filesystem and reuse them as much as you need. You can pass them as inputs to other parts of code.
My personal favorite is the ability to dynamically save data from a game and reload them as needed using the ResourceSaver class .
Thankfully since Godot 4.0 we can now treat Custom Resources as a first class citizen now that we can export and use them as variable types, which makes filtering the files in your project much easier from the inspector.
How do we create a Custom Resource?
To create a Custom Resource, create a new script that inherits from Resource
.
We'll call ours character_stats.gd
.
To improve usability for our Custom Resource, let's give it a class_name
of CharacterStats
and we'll export our data fields that we want to modify as part of this Resource.
Once we have our CharacterStats
CustomResource defined, we can create a new file that inherits from our CharacterStats
Resource and modify it in the inspector panel.
Let's create our stats for a Gandolf character.
We could also create set of character stats using custom script to be more dynamic, then save them off to our filesystem using the ResourceSaver class .
Experiment
Try it out for yourself by experimenting with Custom Resources and the ways you can leverage them instead of reaching for JSON.
The docs are a great place to better understand Godot's base classes and some of their inner workings.