Methods to get the node (and path to it?) that launched a call.

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

Hey! To keep it short and simple, im working on a “shopkeeping” game, i have display furniture nodes in the game, and these nodes have “slots” where you can display sellable items.

You can click on an area on this slots to interact with them, this brings up an interface where you can select an item to display and sell there.

Problem is…
The selection interface is a “sibling” separate from the display (Not a child, not a parent) it’s easy to “call” from the display cases to the selection interface, but then Im having trouble with the interface knowing from which node and slot the interaction came from.

In the display case code, the lines related to the interaction are these:

func _on_Item0Area_input_event(camera, event, click_position, click_normal, shape_idx):
	if event is InputEventMouseButton:
		if event.button_index == BUTTON_LEFT and event.pressed == true:
			get_parent().get_node("HUD/Displayinteract").show()
			get_parent().get_node("HUD/Displayinteract").call("interactingwith", "0", $item0.global_transform)

“Interactingwith” is a func in the selection interface
“0” is the slot in the display case
and
“$item0.global_transform” is a Position3D where the chosen item will be displayed

But what happens when i have more than 1 display case around the shop? How can the interface know which display case “slot 0” is the one “interacting” with it.

Thanks in advance!

Can you minimize your description to only essential implementation details, because now it’s very unclear, what’s you concrete question about?

Normally, for a callee to know who’s called, caller should pass itself as a function argument (like self).
Another approach (care should be taken) - is to use something like global var last_clicked_caller.

sash-rc | 2021-07-03 20:40

I know, believe me i tried haha, but english is not my native language and even so sometimes it’s hard to express yourself clearly when talking about code.

To avoid repeating myself, check the answer i posted to this same question where i clear things up and solve the issue, at least in a way.

Still, i wont mark the question as “Solved”, because there may be better methods.

Tato64 | 2021-07-03 20:57

:bust_in_silhouette: Reply From: Tato64

I always end up solving my issues right after “giving up” and asking the community haha.

Anyways here’s how i did it…

On the display case’s side:

func _on_Item0Area_input_event(camera, event, click_position, click_normal, shape_idx):
	if event is InputEventMouseButton:
		if event.button_index == BUTTON_LEFT and event.pressed == true:
			get_parent().get_node("HUD/Displayinteract").show()
			get_parent().get_node("HUD/Displayinteract").call("interactingwith", get_path(), "0", $item0.global_transform)

Basically, when you click on the display slot 0 in the case, you make the selection interface visible, and call the function “Interactingwith” on it, with these variables:

get_path() = carries over the absolute node path to the display case
“0” is the slot in that display case
and
“$item0.global_transform” is the position of the Position3D node that tells where the item will appear.

The selection interface side is too long and irrelevant to the specific question, but so far this works.