How to statically type a library resource which you load in a node

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

I have created a new class “ScriptingEngine”, extending Reference.

class_name ScriptingEngine
extends Reference
# The card which owns this Scripting Engine.
var card_owner: Card
# Sets the owner of this Scripting Engine
func _init(owner) -> void:
	card_owner = owner

I have a node script with class_name Class. Within one function of that script, it creates a object based on ScriptingEngine

var sceng = ScriptingEngine.new(self)

As soon as I try to run my project, it fails with this error

Parser Error: The class "Card" was found in global scope, but its script couldn't be loaded.

This looks like it’s a cyclical dependency issue. If I remove the : Card static declaration from my var card_owner, it all works fine. Of course it means I don’t get the benefits of static typing

What is the way around this? Am I following a bad practice and if so, how should I be connecting these two objects?

:bust_in_silhouette: Reply From: db0

I asked in discord and the solution is to use the load() instead of a class reference. I’ve replaced my code like so

onready var scripting_engine = load("res://src/core/ScriptingEngine.gd").new(self)

These is the best answer!)

MiltonVines | 2020-11-27 14:54