~/home/blogs/building-an-interaction-system-in-godot-an-update.md

Building an Interaction System in Godot: An Update

I've been working on a co-op game in Godot. Here's an update on the object interaction system!

- Raul G.
2023-02-20

Hey everyone, it's me again, the nerdy developer working on a random co-op game. It's been a minute since my last blog post, but I finally got some time this weekend to add some more features to this new project. So far, we've got a main menu and a character controller as well as the spawn room. It's now time to start adding some gameplay, so I need to build the interactive systems.

As I mentioned in my last post, I've been getting some help with 3D art; I'm collaborating with a Redditor from Spain who's doing an amazing job creating some cool assets for the game. But for the interaction system, it's just been me and Godot.

Interaction System


Now I'm planning the interaction system for this game, and I wanted to share my progress with you all šŸ˜.

I've been thinking a lot about how to create a flexible interaction system that will work will with my future plans for players and the game world. That's why I'm building an interaction system that will allow players to walk up to objects and interact with them using (A) or [SPACE], both with tap and hold interactions. I love that godot is lightweight and flexible with it's script systems.

Of course, with great flexibility comes great responsibility šŸ˜“. Sometimes, it can be exhausting to design a system from scratch. At the same time, it's a blessing to have the freedom to craft the interaction system exactly as I need it. I think I've designed it well enough to be extendable in the future, but I'm curious to see how this system evolves and how it contributes to the overall gameplay experience.

I've been working on making the interaction system generic enough so that we can build any kind of interactable object. Players will be able to walk up to an object to interact with it, or potentially interact with it from range in the case of the mage role. Some objects might require a tap interaction while others may require a hold interaction. I've designed it so that we can build doors, levers, rocks, and anything else we can think of that a player may need to interact with. Plus, the system is co-op friendly, so sometimes one player can interact with an object, but another player cannot.

So, How?

The System


Art generated by Stable Diffusion

The system is designed to leverage Godot's signals and duck-typing, so we can easily add new interactable objects and handle complex interactions. For example, we can use the has_method() function to check if an object has the interact_start(node), interact_end(node), or selected_by(node) functions. That will let us know that the object is interactable. "If it quacks, it's a duck!", lol.

Some objects will only be interactive when approached by certain roles or items. It's been a challenge to design the system to be extendable for future development, but the way that we handle this is to give interactive objects a selected_by(node) function that returns true or false. When a player tries to highlight an object, the player will call object.selected_by(self) and if the result is true, then the object can be interacted with by this player.

If the player is allowed to highlight the object, then the player will select the object and store it in the player's selected variable. When the player interacts with the object by pressing the A button, the object's interact_start(node) function will be triggered. When the button is released, we will call on interact_end(node). This will allow every object to determine whether it should do something on keydown, or keyup. I also thought about how to handle situations where multiple players are trying to interact with the same object at the same time. To handle this, we've added an array called players_selecting to each interactable object. When a player calls object.selected_by(self), the object will store the player in the array and subscribe to the player's selection_changed signal. If the player's selection changes to another object, the object will remove the player from the array. If the array still has at least one element, then we highlight the object so that players know they can interact with it.

Right now, I'm still in the planning stages and haven't yet implemented the system in Godot. But I'm making progress, and I'm hoping that it all goes according to plan. Once it's implemented, I'll be testing and refining it to make sure it feels natural and intuitive for players.

That's it for this update, nerds!! Stay tuned for more updates on game development. And if you have any questions or suggestions, feel free to drop them in the comments. Thanks for following along!

Share this post