How can I pass a EditorPlugin script pass a reference to itself to the Controls it added to the engine?

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

Hello :slight_smile:
I want to create a Plugin that adds a Control to the engine layout. This works fine. But I cannot pass a reference to EditorPlugin script that adds these Controls. I need to pass that reference because I want the Controls to be able to access functions that only a EditorPlugin can run ( get_editor_interface e.g.).

Minimal project to explain my problem:
The Control added by the plugin is a CenterContainer with a Button as a Child.
When the Button is clicked it is supposed to print the Plugin variable to the standardoutput.
The CenterContainer has the following script:

extends Control

var Plugin

func _ready() -> void:
    print("control ready")

func _on_button_button_up() -> void:
    print("plugin is: ",Plugin)

The Plugin has the following script:

extends EditorPlugin

@onready var PluginControl

func _enter_tree() -> void:
    PluginControl = load("res://addons/LevelEditor/PluginControl.tscn").instantiate()
    PluginControl.Plugin = self

func _exit_tree() -> void:

Neither does the ready func in Control print nor can the EditorPlugin assign self to the var Plugin in Control. It does not make a difference if it is a @onready var instead of var or if the EditorPlugin tries to assign it in its _ready or init or enter_tree (as shown above).

How do you people make the EditorPlugin script interact with its Controls?
I appreciate you help :slight_smile: Thanks!

:bust_in_silhouette: Reply From: klaas

i think the problem is that the control does not go into ready state because it isnt initialized by a sceneTree.
i think you want your control to be added to the editor!?
You have to add it via the EditorPlugin to the editor with a proper function call …

add_control_to_bottom_panel(panel,“the panels name”)

then there should be a ready event and the plugin should be accessable.

Thank you :slight_smile:
I solved the problem in a different way: I had forgotten the @tool at the top of the Control script, adding it solved everything!

For some reason using add-child(Control) inside EditorPlugin is possible…
You dont have to use add-control-to-bottompanel(). For me this is strange because there is no documentation out there telling you where the EditorPlugin script is actually positioned. Maybe calling add-child inside it makes the Control just a child of the overall Editor Control…

ChristophtTheSchmidt | 2022-09-26 15:12