Position of the interactive object

Hi! I’m trying to create an interactive object for my game. I created a separate scene for the interactive area, which can be “attached” to any object I want to make interactive. This component sends a signal to the UI element responsible for displaying the prompt indicating what to press, for example, [E] to open.
However, I’m having trouble positioning the Label; I need to display it above the object the player is standing next to. Objects can vary in width and height, so I need to account for that as well.
Does anyone have any suggestions on how to do this?
I’ve attached the code and scene structure below. Thanks!

class_name InteractionArea extends Area2D


@export var interaction_action_name: String = '[E] to interact'

func _on_body_entered(body: Node2D) -> void:
	if body.is_in_group('player'):
		EventBus.interaction_body_entered.emit(interaction_action_name)

func _on_body_exited(body: Node2D) -> void:
	if body.is_in_group('player'):
		EventBus.interaction_body_exited.emit()
class_name InteractionActionUI extends Control


@onready var interaction_action_label: Label = $InteractionActionLabel

func _ready() -> void:
	EventBus.interaction_body_entered.connect(_on_interaction_body_entered)
	EventBus.interaction_body_exited.connect(_on_interaction_body_exited)

func _on_interaction_body_entered(action_name: String) -> void:
	interaction_action_label.text = action_name
	interaction_action_label.show()

func _on_interaction_body_exited() -> void:
	interaction_action_label.text = ''
	interaction_action_label.hide()

image

image

Have you tried adding an instance of the InteractionActionUI object as a child of the InteractionArea2D and just subtract like 100 from the y position and see what happens?

Also, this line is a Godot anti-pattern:

if body.is_in_group('player'):

If you add the player to its own physics layer and make the Area2D only detect the mask that matches that layer, you do not need this code.

You might also consider shortening your object names to eliminate the duplication of the word “Action”.

Yes, I tried, but it’s not working.
All I need to do is display a tooltip over an interactive object, but the problem is that I can’t just add a Label to it. I want to control a single Label and place it in the active zone closest to the player, but if I do this through UI nodes, I can’t convert it to a different coordinate system.
This seems like such a simple task, but I haven’t found any decent tutorials on this topic. Maybe you could suggest a more “proper” way to do this?

In what way is it not working?

Yes you can. Node2D nodes use the coordinates of the root node. Control nodes use the screen coordinates with (0,0) being in the top left of the screen.

You need to do a 2D transform.

I’m not sure this will work, but try something like this:

var interaction_area: InteractionArea  = InteractionArea.new()
var interaction_action_ui: InteractionActionUI = InteractionActionUI .new()

interaction_action_ui.position = interaction_action_ui.make_canvas_position_local(interaction_area.get_global_transform_with_canvas().origin)