Godot Version
v4.2.1.stable.mono.official [b09f793f5]
Question
I’m trying to write a plugin that has a few different classes. I have my project GDScript settings configured to warn and error on typing issues, because this is extremely helpful for spotting mistakes early. I do not consider turning off or suppressing typing errors outright to be a productive solution.
Here’s an example where I’m trying to refer to a class defined in another file from my plugin.gd
, and the typing errors I’m getting:
When defined like this (type inferred):
const Mt3DEditorManager := preload("res://addons/MonsterTerrain3D/editor/Mt3DEditorManager.gd")
And later referenced like this in the plugin’s _ready
function:
var manager: Mt3DEditorManager = Mt3DEditorManager.new()
I get this odd error:
res://addons/MonsterTerrain3D/plugin.gd:28 - Parse Error: Value of type "res://addons/MonsterTerrain3D/editor/Mt3DEditorManager.gd" cannot be assigned to a variable of type "res://addons/MonsterTerrain3D/./editor/Mt3DEditorManager.gd".
I’ve also tried defining it like this (typed as Script
):
const Mt3DEditorManager: Script = preload("res://addons/MonsterTerrain3D/editor/Mt3DEditorManager.gd")
In which case I get this different warning (configured to give an error):
res://addons/MonsterTerrain3D/plugin.gd:28 - Parse Error: The method "new()" is not present on the inferred type "Script" (but may be present on a subtype). (Warning treated as error.)
One thing I have been able to do is change the variable assignment like this, and then it works without errors. The problem is that I then seemingly have no valid way to refer to the type of this variable, if for example I needed to define a function that accepts this type as an argument:
var manager := Mt3DEditorManager.new()
How can I fix this?
(Related: How do I "Include" a Script Into Another Script? - #2 by system)