Using and Creating Resources in Godot 4
- Getting Started
- Player Item Display
- Shop Item Display
- What are Resources?
- Importing and Using Resources
- Applying a Sprite Texture
- Import Parameters
- External vs Built-in Resources
- Creating Custom Resources
- Your First Custom Resource
- An Inventory of Items
- Using Custom Resources
- The Shop Inventory
- Creating a Shop Display
- Showing the Shop Items
- Making Smart Resources
- Inventory Logic
- Player Script
- Displaying Player Items
- Saving and Loading Custom Resources
- Creating a Save Manager
- Connecting the Save and Load Buttons
- Where to Go From Here?
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. You can even use resources to keep track of the game’s progress and store the information on disk.
In this tutorial, you’ll create a shop where items are available for sale. There’s a twist though: the shop sells monsters! While working on the project, you’ll learn:
- What resources are and how to use them
- How Godot handles the loading of resources
- Ways to create your own resources
- How to save and load the state of resources
To start off, download the materials for this project by clicking the Download materials link at the top or bottom of this page. Unzip the files and open the MonsterShop project you’ll find in the starter folder in Godot.
Once the project has loaded, take a look at the FileSystem tab to see the included files.
Most of these are textures and sound files to make the project feel more alive. The scenes folder is especially important, it has three scenes in it you’ll be building upon. Open these scenes in the editor to familiarize yourself with them.
The shop scene is the main scene of the game. This is where you’ll be spending most of your time as it’s the only screen the player will see. It contains a moving background, some background music via an AudioStreamPlayer node, and a CanvasLayer that hold the UI elements.
Press F5 to run the project and see the shop scene running.
Player Item Display
Next, open the player_item_display scene.
This is a simple colored square which will show a single item in the player’s inventory. More on that later on!
Shop Item Display
Finally, open the shop_item_display scene.
This is a card-like display that will show a single item in the shop, along with a buy button.
Now that you have an idea of how the shop works, it’s time to take a closer look at resources.
What are Resources?
Resources are data containers that nodes in your project can use. They can hold many properties, reference other resources and can contain functions to manipulate the data.
Any file that you can load from and save to disk becomes a resource after importing. This includes scenes, textures, sounds, scripts, meshes and fonts.
Godot comes with a huge library of supported resources, and you can also create your own custom resource tailored to your needs.
Importing and Using Resources
The starter folder contains a folder called monsters with some sprites in them you’ll need for the shop.
To import resources, you have two options:
- Copy the files into the project folder
- Drag-and-drop the files into the FileSystem dock
Most developers go with the second option, so select the sprites folder in the FileSystem dock and drag the monsters folder from your file manager into the FileSystem dock.
That’s it, the sprites are now imported into the project as resources. You can find them in the monsters folder in the FileSystem dock.
Applying a Sprite Texture
To test if Godot imported the sprites correctly, you can apply one of them as a texture to a node. To do so, open the shop_item_display scene and select the SpriteTexture node. Now take a look at the Inspector and drag one of the imported sprites onto the Texture property.
You should now see the sprite appear in the circle at the top of the item display.
Next, take a closer look at the Texture property and its Load Path. You’ll find that the path isn’t set to the png file in the monsters folder, but rather to a ctex file in a .godot/imported folder. What gives?
Whenever Godot imports a texture, it creates a ctex file in the .godot/imported folder. A ctex file is a compressed texture file that Godot uses to store textures efficiently. You can change the way Godot compresses textures via the import parameters.
Take a look at the import parameters by selecting one the monster sprites in the FileSystem dock and opening the Import tab in the Scene dock.
Every external resource type has its own set of import parameters for you to change. For image files, you can change how Godot uses the texture. For example, you could use the image as a cubemap or as font instead of a Texture2D.
You can change the compression mode with the Mode property under the Compress category.
By default, Godot stores images as lossless textures on the GPU. This means they’re uncompressed, and there won’t be any loss of quality when compared to the original image. If you want the texture to have a smaller memory footprint and load faster, you can change the compression mode to VRAM Compressed. Doing so will compress the texture on the GPU. The downside is that this might introduce visible artifacts in the texture. As a rule of thumb, keep textures intended for 2D lossless, and use VRAM compressed textures for huge 3D textures.
External vs Built-in Resources
Now focus your attention on the Texture property of the SpriteTexture node again. There’s a Resource property there, click on it to expand its values.
The Resource property has three values:
- Local to Scene: By default, Godot shares resources between scenes. This means that any changes you make in one scene will be reflected in all other scenes. By enabling local to scene, the resource will only be shared in a single scene.
- Path: For this sprite, this points to the path to the resource file inside the FileSystem dock.
- Name: An optional name for the resource.
You can’t edit these values as the sprite is an external resource. This means the resource is stored on the disk as a file. As a result, it will always be shared across scenes and both its path and name are pre-defined.
Godot also allows you to use built-in resources that are stored with the scene file. In the case of textures, these are often gradients and noise textures that you can generate within Godot itself. As an example, right-click the Texture property of the SpriteTexture node and select New NoiseTexture2D in the list.
This will create a new NoiseTexture2D resource. To generate some noise, click the Noise property and select New FastNoiseLite.
You’ll now see the noise texture appear in the scene view.
Both the NoiseTexture2D and the FastNoiseLite resources are stored in the scene itself. This is why they’re called built-in resources. You can save these resources to disk by right-clicking any of them and selecting Save. For the purposes of this article, you won’t need them, but it’s good to keep in mind for your own projects.
Built-in resources that are saved to disk can be reused in other scenes as external resources. This is useful when you want to generate a specific resource only once and reuse it in multiple scenes.
Next, point the Texture back to a monster sprite by dragging one of the sprites from the FileSystem dock to the Texture property of the SpriteTexture node like you did before.
Now that you know the ins and outs of resources, it’s time to take a look at how to create your own.