Godot version:
3.1 Alpha
OS/device including version:
Windows 10
Issue description:
Every class I export with "class_name" gives me the specified error when I attempt to reference them in other scripts.
extends Node2D
class_name Space
var row:int
var col:int
var piece:Piece setget SetPiece
func SetPiece(piece:Piece):
self.piece = piece
add_child(piece)
piece.position = Vector2(0, 0)
piece.space = self
func _ready():
pass
EDIT: I did some testing and it looks like that class_name breaks when you have references to two different exported classes in your code. In other words, you can only have one exported class in your whole project.
The error says there's an error or a cyclic dependency (I actually need to change this error, because not only inheritance can cause this). I need a sample project with a complete reproduction to see if this is not an issue in the code, because the sample code is not complete (I don't know what Piece
is).
It's very easy to make cycles when using class_name
as types, and with is
and as
too.
@vnen
BeardHeroes.zip
Why is referencing each other class_types cause inheritance issues? It's not inheritance after all... Inheritance comes from the extends keyword, unless I misunderstand.
I try all the time to use static typing between classes that reference each other and get the mentioned error.
@PLyczkowski as I said, I will change the error message because not only inheritance can cause this. The cause is cyclic _dependency_.
Can't the dependency _be_ cyclic, or rather mutual? Meaning object A having a var of type B, and object B having a var of type A? I know that would be impossible with inheritance, but I thought types are a different thing.
The problem is the way GDScript is wired: it needs the file fully compiled in order to use it in other scripts. And to fully compile the script, it needs first to fully compile the dependencies. So if any dependency also depend on the current script (that is not yet fully compiled), it won't be able to resolve the types.
This was _always_ a problem. It's just exacerbated now with typing because there are many more ways to create cycles.
Note that other languages have similar issues. C++ solves this by using definitions in headers (and even with that it can create unresolvable cycles).
In order to fix this we need to change how GDScript works, and it's not the time to do it now.
I see, and thanks for explaining.
@vnen In the mean time, what do you recommend we do to have two-way references? Because this is a very common requirement, like in my case where a board game space and a game piece need references to each other.
In the meantime I will just be using C#, which doesn’t have such issues.
Will with be fixed?
This is significant limitation of using class_name.
It is kinda half - unusable now.
You can access scripts by class_name but only in scripts that is not declared with class name..
This is looks like a duplicate of #21461. And it's not really a bug, just that GDScript wasn't designed to handle circular references, since it never had class names and static typing until now.
Closing as duplicate of #21461.
@vnen In the mean time, what do you recommend we do to have two-way references? Because this is a very common requirement, like in my case where a board game space and a game piece need references to each other.
In the meantime I will just be using C#, which doesn’t have such issues.
Simply don't use statically typed variables then.
@PLyczkowski as I said, I will change the error message because not only inheritance can cause this. The cause is cyclic _dependency_.
It's been 20 months and you didn't. Great job.
Also, I'm getting this message without any cyclic dependencies, just two classes extending same parent.
UPD: Solved by restarting an editor. Bravo.
@badunius We could do without your passive aggressiveness, thanks.
@akien-mga I know, I know. And I could go without being misled. I can't help but picture an "American Chopper" meme in my head: —You have an error! — I don't! — You have an error! — I checked twice, there's no error!
And then I google this.
P.S. I'm not passive aggressive. I'm just trying to stay polite while being aggressive due to frustration.
This is still a valid issue I believe. I have two scripts and can only reference one in a script, if I try to reference both, this error comes up.
Just faced this error. I have 3 scripts. One of them is just a bunch of constants to be used in other scripts. The two other scripts do use these constants and I started to see the error. After lots of fighting, closing the editor and then reopening it got me rid of the infamous message,
I'm having this same issue too, can't use class_names in the way they're meant to, lol
Most helpful comment
I'm having this same issue too, can't use class_names in the way they're meant to, lol