2.2 KiB
Resources
Resources are essentially data containers. Much like nodes, they can hold functions, properties, and signals.
Resources do nothing on their own. They must be a property of a node for their data to be accessed.
The resoource only gets loaded into memory once. Any nodes referencing a node are accessing the SAME data. This behavior can be changed so that the resource is unique for each node referencing it.
Lessons:
- Create a script to use as a data object
- Create a resource based on the script for the player, and the enemy
- Learn to load resources
Autoload Singletons
The scene system in Godot is great for building multiple levels of a game and being able to run each one of them individually during development. However it does have a limitation.
When a scene is reloaded, or another scene is loaded in its place, all information is lost. This can be problematic for gameplay, because it means that variables storing information such as player health, number of enemies, etc. are lost when the scene is reloaded.
One answer to this is the use of an Autoload resource.
An autoload resource is one that can be specified in the project settings. It will be loaded ahead of all other resources, and can be given a name that can be referenced throughout the game by any other code. Data in this autoloaded resource will persist across scene changes.
An autoload script is a good place to put any persistent game logic and data, as well as any helper methods that may be useful to a wide variety of nodes in your game.
It is possible that an autoload script can get quite cluttered with information, so a good combination is to use an autoload script and data objects together.
Lessons:
- Create a script to be the game controller, set it to autoload
Relevance to UI
A good UI is essentially a graphical representation of the state of the game. Once that state is centralized in one place it becomes easier to make UI that accurately reflects all the data in the game (such as player health, coins collected, points, timers etc.)
Our goals for UI:
- Create a Canvas Layer to float the UI over the viewport
- Create a Control node to contain our UI nodes
- Create Container nodes (vbox and hbox)
- Use a MarginContainer node to wrap our UI nodes