Access the property of an instantiated scene in a plugin

Godot Version

4.2.1

Question

I’m trying to make a plugin that displays a context menu on click but I’m stuck here, calling a method from an instantiated scene.

[plugin.gd]

@tool
extends EditorPlugin
var dock:Context

func _init() -> void:
	main_screen_changed.connect(screen_changed)

func _enter_tree() -> void:
	# Initialization of the plugin goes here.	
	dock  = preload("res://addons/context_menu/context.tscn").instantiate()
	add_control_to_dock(DOCK_SLOT_RIGHT_BL,dock)


func _exit_tree() -> void:
	remove_control_from_docks(dock)
	dock.free()
				
func screen_changed(screen)->void:
	if dock:
		if dock.has_method("setCurrentScreen"):
			dock.setCurrentScreen(screen)
		

[context.gd - attached to context.tscn]

extends Control
class_name Context
var currentScreen:String

func _input(event: InputEvent) -> void:
	if event is InputEventMouseButton: 
		if event.button_index == MOUSE_BUTTON_LEFT: 
			if event.pressed:
				if currentScreen != "3D": return
				$cm.show()
				$cm.position = get_viewport().get_mouse_position()

func setCurrentScreen(val:String)->void:
	currentScreen = val

Error:
res://addons/context_menu/plugin.gd:22 - Invalid call. Nonexistent function ‘setCurrentScreen’ in base ‘Control (Context)’.

how is it possible that “has_method” evaluates to true and at the same time sends me the error that the function does not exist?

I had to use the @tool keyword also in my instantiated scene script. That’s why it wasn’t working :expressionless:

@tool
extends Control
class_name Context
1 Like