Any game needs assets like textures, sounds, and music to provide a fun experience to players. In Godot, these files are treated as resources that can be used throughout the project. Custom resources can be created to build powerful, modular systems, making it easier to manage the project. Resources can also be used to track the game’s progress and store information on disk. In this tutorial, a shop will be created where monsters are sold. The tutorial will cover the basics of resources, how to use them, how Godot handles their loading, how to create custom resources, and how to save and load the state of resources. It is assumed that the reader is familiar with the basics of Godot and GDScript. To quickly get started with these concepts, check out the Godot Getting Started and Scripting tutorials. To begin, download the materials for the project and unzip them. Open the MonsterShop project in Godot. Take a look at the FileSystem tab to see the included files, including textures, sound files, and scenes. The scenes folder contains three scenes that will be used in the project. Open these scenes in the editor to familiarize yourself with them. The “Shop” scene is the main scene of the game, where the player will spend most of their time. It contains a moving background, background music, and a CanvasLayer with UI elements. Press F5 to run the project and see the shop scene in action. The “Player Item Display” scene is a simple colored square that will show a single item in the player’s inventory. The “Shop Item Display” scene is a card-like display that will show a single item in the shop, along with a buy button. Now that the basic structure of the shop is understood, let’s take a closer look at resources. Resources are data containers that nodes in the project can use. They can hold properties, reference other resources, and contain functions to manipulate the data. Any file that can be loaded from and saved to disk becomes a resource after importing. This includes scenes, textures, sounds, scripts, meshes, and fonts. Godot comes with a library of supported resources, and custom resources can be created to suit specific needs. To import resources, there are two options: copying the files into the project folder or dragging and dropping the files into the FileSystem dock. Most developers prefer the second option. Select the sprites folder in the FileSystem dock and drag the monsters folder from the file manager into the FileSystem dock. The sprites are now imported as resources and can be found in the monsters folder in the FileSystem dock. To test if the sprites were imported correctly, apply one of them as a texture to a node. Open the “Shop Item Display” scene and select the SpriteTexture node. In the Inspector, drag one of the imported sprites onto the Texture property. The sprite should now appear in the item display. Take a closer look at the Texture property and its Load Path. The path points to a ctex file in the .godot/imported folder, rather than the png file in the monsters folder. This is because Godot creates a compressed texture file (ctex) in the .godot/imported folder when importing a texture. The import parameters can be adjusted to change how Godot compresses textures. The import parameters can be found in the Import tab in the Scene dock. For image files, the compression mode can be changed. By default, images are stored as lossless textures on the GPU, meaning there is no loss of quality compared to the original image. To reduce memory usage and load times, the compression mode can be changed to VRAM Compressed, but this may introduce visible artifacts. It is generally recommended to keep 2D textures lossless and use VRAM compressed textures for large 3D textures. The Texture property of the SpriteTexture node has a Resource property that can be expanded to show its values. The Resource property has three values: Local to Scene, Path, and Name. By default, resources are shared between scenes, but can be set to only be shared within a single scene. The Path points to the resource file in the FileSystem dock, and the Name is an optional name for the resource. External resources are stored as files on disk and are shared across scenes. Godot also allows the use of built-in resources, which are stored with the scene file. These can be generated within Godot, such as gradients and noise textures. Built-in resources can be saved to disk and reused in other scenes as external resources. To create custom resources, further exploration is required.
Source link