Godot Version
Godot 4.5
Question
Hi! I am making my first plugin. The plugin works correctly after a project reload when the autoload is enabled at least once, but fails with parse and class resolution errors on initial activation in a fresh project.
Here is how my addon works:
Overview
The plugin follows this initialization flow:
- Plugin Activation (
_enable_plugin): Registers an autoload viaadd_autoload_singleton()and defers UI setup - Autoload Initialization (
_ready): Instantiates anARKitServerand connects signals - Menu UI: The editor panel communicates with the autoload via signals
Initial Errors
When first adding the plugin to a new project, two parse errors occur:
res://addons/godotarkit/arkit_server.gd:163 - Parse Error: Identifier "ARKitSingleton" not declared in the current scope.
res://addons/godotarkit/arkit_menu.gd:91 - Parse Error: Identifier "ARKitSingleton" not declared in the current scope.
After enabling the plugin, a runtime error appears:
res://addons/godotarkit/arkit_autoload.gd:52 - Invalid call. Nonexistent function 'new' in base 'GDScript'.
Error at: '_server = ARKitServer.new(11111)'
The class ARKitServer mysteriously resolves to the base GDScript type instead of the actual class.
Code Structure
Plugin Core (plugin.gd):
@tool
extends EditorPlugin
const ARKIT_AUTOLOAD_NAME: String = "ARKitSingleton"
const ARKIT_MENU: String = "uid://bmmmwgdvt6ymb"
const ArkitAutoload: String = "arkit_autoload.gd"
var control: Control
func _enable_plugin() -> void:
add_autoload_singleton(ARKIT_AUTOLOAD_NAME, ArkitAutoload)
call_deferred("_setup_ui")
func _setup_ui() -> void:
control = load(ARKIT_MENU).instantiate()
add_control_to_bottom_panel(control, "GodotARKit")
func _disable_plugin() -> void:
if control:
remove_control_from_bottom_panel(control)
control.queue_free()
remove_autoload_singleton(ARKIT_AUTOLOAD_NAME)
Autoload (arkit_autoload.gd):
@tool
extends Node
var _subjects: Dictionary[String, ARKitSubject] = {}
var _server # <- Missing type annotation
func _ready() -> void:
_server = ARKitServer.new(11111)
change_port.connect(_on_change_port)
start_server.connect(_server.start)
stop_server.connect(_server.stop)
func _exit_tree() -> void:
if is_instance_valid(_server):
_server.stop()
Menu UI (arkit_menu.gd):
@tool
extends HBoxContainer
func _ready() -> void:
_subjects_list.clear()
_hide_subject()
ARKitSingleton.show_error.connect(_on_error_shown)
ARKitSingleton.add_subject.connect(_on_add_subject)
ARKitSingleton.remove_subject.connect(_on_remove_subject)
Note on Classes: ARKitServer, ARKitSubject, and related classes are standard RefCounted classes without the @tool annotation.
ARKitServer is not a tool, it’s just a RefCounted, same for all other classes
Real question
I don’t understand why I have those errors only when first loading, and how to resolve them so that when a user download my addon, it does not create any error
- It can be that I don’t understand the behavior of @tool and that it cause some weird things
- It can be that I don’t fully understand the parser order or something related
- I also encountered a weird
Internal script error! Opcode: something (please report)by enabling/disabling the plugin in a new project, but it only happened once.
So I’m at a loss here, since it works… after a project reload. I don’t want to publish an addon that causes errors for the user, and I have no clue where the problem is.
Thanks for reading this!
Could you please help me ![]()