This series is a follow-up on the previous chapter.
I’ll take you from where we left off on the simulation builder and add a way to automatically manage our entities and blueprints, a grid-based inventory, picking up and dropping items, and the start of resource gathering.
This chapter is an extension of the foundation we already laid in the previous chapter. We need new systems and to extend existing ones to handle the new features, but the underlying principle we shape everything around does not change. We keep entities naive and have them controlled by higher-level systems.
In this chapter, we’ll cover:
As always, we welcome your feedback and questions, which you can leave directly under any lesson on the course’s platform.
You can find the fully completed project on the Godot 2D Builder. You can also find the starter project for the chapter inside the course’s downloads. It contains the result of the previous chapter if you need to skip ahead.
In this series, we’ll focus on items and the inventory.
We’ll design an inventory and a quick-bar where you can click to place or select items, attaching them to the mouse, swap items between inventory slots, and more.
We’ll also implement the ability to loot and stack items automatically, as seen in Factorio.
To implement this interface, we’ll use the following code structure.
We’ll create a GUI
node as the entry point for the rest of the game to access the inventory without holding fragile or complex connections to each part that makes up our user interface.
All they need to know is the GUI node and the functions and data it provides. We’ll keep the rest of the interface separated into a dedicated scene.
This setup allows us to make sweeping changes to the user interface without affecting the rest of the game, so long as we update the GUI
node to reflect the new interface. It’s useful because the UI is one aspect of a game that changes the most often!
Then, the GUI
node transfers signals and forwards messages from the rest of the game to the user interface, and vice versa.
The inventory window is the focus of this chapter. We build it out of a series of inventory panels that hold a reference to the BlueprintEntity
it’s holding onto. The panels give us the ability to split stacks, move item stacks from panel to panel, or take them into our hand.
The inventory window instances and manages those panels and gets its messages from the greater GUI node.
We’ll move the DragPreview
object out of EntityPlacer
and make it part of the user interface. This is because the DragPreview
node will be the mouse’s “inventory”, where a stack of items can live while the player is choosing where to place it or move it.
Finally, we’ll add a library node, an auto-loaded node that’ll make it so we don’t have to keep editing the code whenever we add a new entity. It will find all entities and blueprints and update itself whenever the game runs.
We have a lot to cover, so let’s get started. In the next lesson, we’ll brush over UI themes in Godot.