0 votes

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?

in Engine by (115 points)

1 Answer

+1 vote
Best answer

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)
by (115 points)
selected by

These is the best answer!)

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.