Connecting signal via code

Godot Version

Godot 4.4.1 Stable

Question

Hello everyone. I tried to connect signal via code and got myself stuck in dead end, even when using godot docs or forums with similar question (you can guess that i'm novice).

Main tree

So, here’s the thing: I have an Item (Area2D), that emmits a signal, when body (player) enters it.

Part of “Item Area2D.gd”

func _on_player_entered(body):
	print("player took an item")
	queue_free()
	take_item.emit(item)

My problem to connect that signal via code to “inv_panel”, that located in HUD. I can’t imagine what i suppose to do now: get proper reference to "Item (Area2D) in “inv_panel” or HUD and then connect signal?

Hud Tree

inv_panel.gd

extends Panel

@onready var inventory: Inventory = preload("res://resources/inventory.tres")
@onready var slots: Array = $HSplitContainer.get_children()



func update():
	for i in range(min(inventory.items.size(), slots.size())):
		slots[i].update(inventory.items[i])

func _ready():
	update()
	#Global.connect("on_item_pick_up", _on_item_area_2d_take_item) <- Didn't make it
	
func _update_item(index: int, newItem : InventoryItem):
	inventory.items[index] = newItem
	slots[index].update(newItem)


func _on_item_area_2d_take_item(item):
	for i in range(inventory.items.size()):
		if !inventory.items[i]:
			_update_item(i, item)
			break

Tried many different things, even using global script, but got mixed up with different solutions (and different errors), so now i decided go back at the start. Can you give me some advice to get this resolved (any suggestion, really, i’ll try it all)

If you need more details on something, i’ll try my best to give it to you

Thanks in advance

Considering your items are probably going to be dynamic (i.e. spawned at run-time, not configurable in the editor) you will have to find the HUD each time the item requires it, or on spawn. Using a Group for the inventory may be best, instead of signals.

func _on_player_entered(body):
	var inventory = get_tree().get_first_node_in_group("Inventory")
	inventory.add_item(item)
	queue_free()
2 Likes

@gertkeno 's answer was what I was going to suggest. The other possibility is you can make the HUD an autoload. Then it’s easy to find.

func _on_player_entered(body):
	HUD.add_item(item)
	queue_free()
2 Likes

How are items generated? If this is done dynamically using code it would be best to have said code also connect the necessary signals. An approach I would take is to have a node responsible for the spawning of items. Part of that process would involve connecting the necessary signals. Said node could have references to all of the nodes that need to be connected.

I should clarify that I have the most experience using Godot 3.6, so take this suggestion with a grain of salt.

1 Like

So, i have a frontend java friend of mine, and yesterday i asked him to help me with that problem. And solution was to add my “Item (Area2d)” in “items” group
Then, in inv_panel.gd:

func _ready():
	for item_node in get_tree().get_nodes_in_group("items"):
		item_node.take_item.connect(_on_item_area_2d_take_item)

I was thinking that way about month ago, but i messed up in some details. Had to look in other project to see, that i’ve already done that in the past. Simple as that. Thanks everyone for the help and tips tho, i appriciate that, guys! (godot community is the best)
I’ll look to this topic in case i need to rewrite my code with suggestions of yours
P.S. - yes, should have asked my friend earlier, so sorry for that, but maybe all of solutions in this thread will help others :slight_smile:

2 Likes

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.