Use and Create Godot Resources 4

Any outdoor game needs assets such as textures, sounds, and music to provide an enjoyable experience to its players. Godot treats these files as resources that you can use in your project.
Besides Godot’s built-in resources, you can also create your own to create powerful, modular systems. Resources make it easy to manage your project. You can use resources to track game progress and save information to disk.

In this tutorial, you will create a store where items will be available for sale. There’s a twist though: the store sells monsters! While working on the project, you will learn:

  • What are the resources 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
NOTES: This article assumes you are familiar with the basics of Godot and have an understanding of GDScript. To get started with both concepts quickly, check out Godot Getting Started and Teaching Writing.

dash

To get started, download the materials for this project by clicking on Download materials link at the top or bottom of this page. Unzip the files and open the MonsterShop project you can find on starter Godot folder.

Once the project is loaded, look at the FileSystem tab to see the included files.

project 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 that you build on. Open these views in the editor to familiarize yourself with them.

shop

the STORE scene is the main scene of the game. This is where you will spend most of your time as this is the only screen the player can see. It contains a moving background, some background music by a AudioStreamPlayer node and a CanvasLayers which holds the UI elements.

Store scenes

Press F5 to run the project and see the store view running.

The store scene is running

NOTES: If you want no music while running the game, you can change to disable PAUSE on BGM node.

Player Item Display

Next, open the player_item_display view.

Player item display nodes

This is a simple colored square that will display an item in the player’s inventory. More on that later!

Store Item Display

Finally, open the shop_item_display view.

Show items in the store

This is a card-like display that shows an item in the shop, along with a buy button.

Nodes to display items in the store

Now that you have an idea of ​​how the store works, it’s time to take a closer look at the resources.

What are the Resources?

Resources are data containers those nodes in your project are available. They can hold multiple properties, reference other resources and can contain functions to manipulate data.
Any file you can load and save to disk will become a resource after import. This includes scenes, textures, sounds, scripts, meshes and fonts.

Godot has a large library of supported resources, and you can also create your own custom resources tailored to your needs.

Importing and Using Resources

The starter folder contains a folder called animals with some sprites you need for the store.

Monster sprite files

To import resources, there are two options:

  • Copy the files to the project folder
  • Drag-and-drop files onto the FileSystem dock

Most developers go to the second option, so choose the sprites folder in the FileSystem dock and drag the animals folder from your file manager to the FileSystem dock.

Drag and drop the folder

That’s it, the sprites are now imported into the project as resources. You can find them at animals folder in the FileSystem dock.

Monster resources

Applying Sprite Textures

To test whether Godot is importing the sprites correctly, you can apply one of them as a texture to a node. To do this, open the shop_item_display view and select the SpriteTexture node. Now look in the Inspector and drag one of the imported sprites into the Text property.

Drag the sprite

You should now see the sprite appear in the circle above the item display.

Item display sprite

Next, take a closer look at the Text property and its Load Path. You can see that the path is not set to png file in animals folder, but in a ctex file in a .godot/imported folder. What gives?

Ctex path

Every time Godot imports a texture, it creates a ctex file in .godot/imported folder. A ctex file is a compressed texture file which Godot uses to store textures effectively. You can change the way Godot compresses textures via import parameters.

Import Parameters

View the import parameters by selecting a monster sprite in the FileSystem dock and opening the Import tab in the Scene dock.

Item of import

Each type of external resource has its own set of import parameters for you to modify. For image files, you can change how Godot uses the texture. For example, you can use the image as a cubemap or so fonts instead of a Text2D.

Texture import parameters

You can change the compression mode using the Mode property under compress category.

Compression mode

Usually, Godot saves the images as no loss GPU textures. This means that they are not compressed, and there is no loss of quality 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 this will compress the GPU texture. The downside is that it can introduce visible texture artifacts. As a rule of thumb, keep textures intended for 2D lossless, and use VRAM compressed textures for large 3D textures.

External vs Built-in Resources

Now turn your attention to Text property of SpriteTexture node again. There is one resources property there, click on it to expand its values.

Resource property

the resources The property has three values:

  • Local to the Scene: Generally, 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 the scenethe resource will only be shared by one view.
  • way: For this sprite, this points to the path to the resource file within the FileSystem dock.
  • name: An optional name for the resource.

You cannot edit these values ​​because the sprite is a external resource. This means that the resource is stored on disk as a file. As a result, it will always be shared with scenes and its path and name are predetermined.

Godot also allows you to use built-in resources which is saved with the scene file. In the case of textures, these are mostly gradients and noise textures that you can create right within Godot. As an example, right-click THE Text property of SpriteTexture node and select New NoiseTexture2D in the list.

New noise texture

It creates something new NoiseTexture2D resources. To generate noise, click on Noisy property and select New FastNoiseLite.

New fast noise lite

You can now see the noise texture visible in the scene view.

Noisy

The two NoiseTexture2D and the FastNoiseLite the resources are stored in the scene itself. This is why they are called built-in resources. You can save these resources to disk by right click any of them and choose Keep. For the purposes of this article, you don’t need it, but it’s good to keep in mind for your own projects.

Save the built resource

Built-in resources stored on disk can be reused in other scenes as external resources. This is useful when you want to create a specific resource only once and reuse it in multiple scenes.

Next, point to the Text return to a monster sprite by dragging one of the sprites from the FileSystem dock to the Text property of SpriteTexture node as you did before.
Now that you know the ins and outs of these resources, it’s time to see how to make your own.

Leave a comment