Connecting Coins to Inventory System

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By BadGamer

Trying to figure out how to best “connect” two parts of a game I am making. To illustrate just imagine one scene we call “Inventory” and another scene we call “Coin”. These scenes are entirely unrelated and are being used in a “World” scene. The “Coins” are being instanced.

The player wants to pick up a Coin. Before being able to do this the Inventory needs to be checked and only if there is enough room the Coin can be picked up.

The idea is that a Coin needs to emit a signal telling the Inventory; “Hey, I want to be picked up, is this alright?” and then the Inventory responds with yet another signal: “Yes, that is alright”.

However for this to work the inventory needs to connect to every single coin and this requires it to be aware (in some way) of all coins.

I don’t want the Inventory system to be aware of all Coins in the World.

How can I make those Coins communicate with the Inventory? And keep everything separated.

Do I need to add another layer, for example “Coin Manager” and let it handle the communication?

Basically I want to avoid code like : for each coin: each.connect(foobar) inside my inventory.

Or am I missing something obvious?

:bust_in_silhouette: Reply From: i_love_godot

I’m a noob… but I would use a global singleton as discussed here:

Singletons (AutoLoad)

That way, you could have a global “inventory” object that you can query from any scene that you like. As you move from level to level (scene to scene), the inventory is always available.

You wouldn’t need to connect signals either. When a player “collides” with the coin, you can directly query the global inventory object and see if the coin can be picked up. That would happen in your existing collision handler method.

Hope this helps :slight_smile:

Thanks. That is just what I need. At the end of day this whole “Inventory System” is just a dictionary and some methods to manipulate it.

BadGamer | 2019-01-15 22:24