Using and Creating Resources in Godot 4

Any game out there needs assets like textures, sounds, and music to provide a fun experience to its players. Godot treats these files as resources you can use throughout your project. Besides Godot’s built-in resources, you can also create your own to build powerful, modular systems. Custom resources make it easier to manage your project. […] By Eric Van de Kerckhove.

Leave a rating/review
Download materials
Save for later
Share
You are currently viewing page 2 of 5 of this article. Click here to view the first page.

Creating Custom Resources

Custom resources are data containers that can store and manage data in a reusable way. Besides data, they can also hold logic to manipulate the data or to signal changes. They’re useful to organize sets of data throughout your project. You can use them in place of JSON or CSV files to keep properties grouped together. Your custom resources can also be saved to disk and slotted into your scripts, just like regular resources. If you used other game engines, they’re like Scriptable Objects in Unity and Data Assets in Unreal Engine.

Here are some example of how you can use custom resources:

  • Game settings: Store the game’s settings like the music volume, resolution and difficulty level.
  • Level data: The tiles, spawn locations and item placements for a level.
  • Dialogues: The text and choices for a dialogue, along with sprites and sounds.
  • Stat blocks: A character’s stats, like health, attack and defense.
  • Achievements: Achievement names, along with the conditions and rewards.
  • Color palettes: A list of color palettes the player can choose from.

Your First Custom Resource

Creating custom resources is as easy as creating a script and a regular resource. For your first custom resource you’ll be creating an item resource that will hold information about a monster, including its name, sprite and price.

To start, create a new folder in the scripts folder and name it resources. This keeps your files nice and organized.

Resources folder

Now create a new script by right-clicking the resources folder and selecting Create New ▸ Script…. Replace Node with Resource in the Inherits property, this will make the script inherit from the Resource class. Next, name the new script item.gd and click Create.

Item resource script

Double-click the item.gd script to open it in the code editor and replace its contents with the following:

extends Resource # 1
class_name Item # 2
# 3
@export var item_name : String = "Unknown"
@export var sprite : Texture2D
@export var price : int = 1
# 4
@export var hp : int = 10
@export var attack : int = 10
@export var defense : int = 10

This script holds information about an item, which will be displayed as a monster in the game. If you ever wrote a script in Godot, this will look familiar:

  1. This script inherits from the Resource class, the base class for serializable objects.
  2. An Item class name to make the script easier to type and make auto-completion work.
  3. A set of exported variables that will be editable in the Inspector. The first three variables are the item’s name, texture and price.
  4. Exported variables to store the item’s stats.

Now save the Item script. From now on, you can make resources based on it.
To give the items a nice place to store, create a new folder named items in the root of the project.

Items folder

Next, right-click the items folder and select Create New ▸ Resource…. In the Create New Resource window, search for Item and click Create.

Select item resource

Name this new resource bat.tres and click Save to add the new resource to the project. Congrats, you just created your first custom resource!

The next step is to fill in the details of the bat item. Double-click the bat.tres resource to open it in the Inspector.

Bat resource

As you can see, all the properties you defined in the script are here, ready for you to fill them in. Change the Item Name to Bat and set the sprite by dragging the bat sprite from the FileSystem dock to the Sprite property. You can fill in the other properties as you see fit.

Bat resource filled in

To create the other items, duplicate the bat.tres resource by selecting the file and pressing CTRL/CMD-D in the FileSystem dock. Name the new resources as follows:

  • bird.tres
  • ghost.tres
  • mushroom.tres
  • slime.tres

Duplicate items

For each of these item resources, fill in the details as you did for the bat.
By defining items like this, you can add more items to the game by filling in the properties. The items by themselves won’t do much, so that’s where the inventory comes in to hold a collection of critters to work with.

An Inventory of Items

The inventory will be another resource, one that holds a collection of items. You can imagine how this can be useful in a game with an item shop, a blacksmith and other merchants. Each of these with their own inventory of items.

Like with the item resource, create a new script in the scripts / resources folder. Make sure it inherits from the Resource class and name it inventory.gd. Edit the inventory.gd script and add these two lines below extends Resource:

class_name Inventory

@export var items : Array[Item]

This script allows you to make inventories that each hold an array of items. To use it, create an inventory folder in the root of the project and create a new Inventory resource, the same way you created an item. Name this new resource shop_inventory.tres. As its name suggests, it will hold the shop’s inventory.

Shop inventory resource

Double-click shop_inventory.tres to open it in the Inspector. As expected, there’s a single array of items in the items property.

Shop inventory

Now you need to add items to this inventory. Due to a bug in Godot, you can’t select all items and drag them to the Items property like you’d expect. Instead, click the Array property to reveal its Size property. Next, set its value to 5. This will create 5 empty items slots in the inventory.

Empty items

Finally, drag the items from the FileSystem dock to the items slots in the inventory, one by one.

Dragging items to inventory

Great! With the inventory ready to go, it’s time to use it in a script.

Using Custom Resources

To use the inventory and the items in it, you’ll be creating a shop script. This script will accept an inventory and display its contents so a player can buy items.

The Shop Inventory

The first step is to create the shop script. Open the shop scene and select the Shop node at its root. Next, click the new script button open the Attach Node Script window.

Attach script button

Make sure this script is placed in the scripts folder and name it shop.gd. Now click the Create button to create the script.

Save shop script

The shop script will open in the code editor automatically. Add the following code below the extends line and save the script:

class_name Shop

@export var shop_inventory : Inventory

This gives the script a class name and adds an export variable that accepts an inventory. This by itself won’t do much, so add or edit the _ready function below:

func _ready(): # 1
    for item in shop_inventory.items: # 2
        print(item.item_name) # 3

Here’s what this does:

  1. The _ready function is called when the script is loaded
  2. For each item in the shop’s inventory…
  3. Print the item’s name

Now save the script and make sure the Shop node is selected. Next, drag shop_inventory.tres from the FileSystem dock to the Shop Inventory property of the Shop node.

Drag inventory

Next, run the project and take a look at the output window. It should print out all the items in the inventory.

Items in output window

Contributors

Over 300 content creators. Join our team.