How to check for mouse click on TreeItem

Godot Version

Godot 3.5.3


I need to make sure that when I right-click on certain TreeItems, a menu will open, how can I do this?
I read the documentation but still don’t understand, I tried using get_selected() but it will only work for the left mouse button.

I’m guessing you’ll need to enable Tree.allow_rmb_select and listen to the signal Tree.item_rmb_selected You can get the TreeItem from the position with Tree.get_item_at_position()

It’s a little unclear, could you write a small code example?

extends Node

@onready var tree = $Tree

func _ready() -> void:
	tree.allow_rmb_select = true
	tree.connect("item_rmb_selected", self, "_on_tree_item_rmb_selected")
func _on_tree_item_rmb_selected(pos:Vector2) -> void:
	var item = tree.get_item_at_position(pos)
	# do whatever with the item

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