0 votes

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?

in Engine by (12 points)

1 Answer

+1 vote

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 :)

by (378 points)

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.

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.