Get_item_at_position() function

Godot Version

Godot 4.0 stable

Question

I wanted to see what instance of the node the script is attached to so I tried to use get_tree().get_item_at_position() but it says “Invalid call. Nonexistent function ‘get_item_at_position’ in base ‘SceneTree’.” Does the function not exist or what?

This is my code:

extends Area2D

var Bakery_path : String = ""

func _on_area_entered(area):
	if(area.is_in_group("Player")):
		
		var Node_instance = get_tree().get_item_at_position(position_x_y.position_x)

You are thinking about the GUI node Tree, the get_tree method returns SceneTree, which is different

Can u give me the function then?

Under the assumption that your scene tree looks like this:

  • Area2D
    • Tree

then you can use the following in order to get the tree item within the tree node:

extends Area2D

var Bakery_path : String = ""

func _on_area_entered(area):
	if(area.is_in_group("Player")):
		var tree_node: Tree = $Tree
		var Node_instance = tree_node.get_item_at_position(Vector2(10, 10))
2 Likes

There is no function to find a node at a position in the scene, generally, it depends on what you are actually looking for

It says node not found when i give the right node

Please provide more info about how your scene tree is set up and what you want to achieve.

2 Likes

I want to name of the node instance i need to work with.

Scene tree is:

  • Door

    • Sprite2D

    • Area2D(This is the area that houses my script)

      • CollisionShape2D2
    • CollisionShape2D

    • AnimationPlayer

    • AnimationTree

Then that doesn’t work at all, this method is specific to Tree, there’s no general method for this

1 Like

U said “: Tree” how would that work if it were say CharecterBody2D or Staticbody2D. What I mean is what node would have a type of “Tree”.

It won’t work? It only works with Tree, not with those types

You can’t do what you are wanting to do with this, there is no way to find a node at a position like this

Tysm
For letting me know.
Ill try to find a work around.

1 Like