This site is currently in read-only mode during migration to a new platform.
You cannot post questions, answers or comments, as they would be lost during the migration otherwise.
+1 vote

So i'm currently trying to learn how to make games using godot. Right now i'm trying to create my own inventory system for my game. I have a function that adds an item to the inventory.
I want to add an item to the inventory every time player collides with a certain object (for example, if the player collides with a carrot, i want this carrot to be added to his inventory).

Here is my script for the item:

extends Node2D
export var my_res: Resource
onready var slots_script = get_node("/root/Slots")

func _on_Area2D_area_entered(area):
   slots_script.AddItemToInventory(my_res)

And here is the function that this item is triggering:

func AddItemToInventory(item):
print("adding")
for i in range(0, slots.size()):
    if slots[i].isFull == false: 
        slots[i].isFull = true
        slots[i].addItem(item)
        break

For some reason the only part of this function that is triggering right now is the first line that is printing a message in the console.
I tested this function before so there should be no problem with it. I think there is something wrong with my item script but i can't figure out what. I would really like to get some help with it
P.S I'm just a beginner so please don't judge my coding skills too much

Godot version v3.2.3.stable.official
in Engine by (15 points)

A side note, too, instead of referencing a node and calling a function on it, this is where you should be emitting an event that your inventory should be listening to

please be so kind and make some example-code. I'm interested, too

Emit a signal when it gets added to inventory:

extends Node2D
export var my_res: Resource
signal add_to_inventory(res)

func _on_Area2D_area_entered(area):
   emit_signal("add_to_inventory", res)

And in your Slots node programmatically or setup in the GUI:

func _ready():
   connect("add_to_inventory", self, "AddToInventory")

Mh, i'm not sure if i would do that with signals... i mean of course it highly depends on the inventory-system, but if the inventory can be full, i have to check first if the inventory has space so i can remove the item from the normal gameworld. So i can do something like this:

if Inventory.add_item(self): # true -> successfully added
    queue_free() # kill it (or maybe reparent the node to the inventory)
else: # false -> adding failed
    pass # stuff if the inventory is full (or whatever went wrong)

With signals that would be kind of a pingpong-game...

btw, i'm using signals way to rarely. maybe i'm getting something wrong here ;)
I'm always open to learn something new.

We're totally off-topic, but yeah I don't have many signals - I use a few autoloaded singletons and inventory should definitely be one. For the signals I do use I generally use an event bus for it: https://www.gdquest.com/docs/guidelines/best-practices/godot-gdscript/event-bus/

@misuki: I would also see how other people have implemented such systems, I know a quick google or github search will return a few results

Please log in or register to answer this question.

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.