+1 vote

I declare a class in a .gd file like this

class_name C_CASELLA
extends Object

Then i discovered that even if i don't istance it, it generate a leak when i quit the application.
My supposition is that "classname" does allocate something.
Seen that the documentation it's not clear about that,can somebody explain please what exactly class
name keyword do?
Thank you in advance

in Engine by (32 points)

Are you using the class type inside the same script it's defined? If so, it's a known issue that should be fixed in the gdscript refactor of Godot 4.0: https://github.com/godotengine/godot/issues/31378

Or you could be having a circular reference issue (CCASELLA uses type OTHERTYPE, and OTHERTYPE uses type CCASELLA), also a known issue: https://github.com/godotengine/godot/issues/27136

Also, the docs examples show to declare the class after the extends statement. Not sure if that changes anything though.

HI Bernard and thanks.
I have read the issue just now and yes it is.
I am using the type everywhere in the project,because i found that using class_ name the type become global but probably is a wrong way to do so.
I use extends Object:

    class_name C_CASELLA
    extends Object
    var id: int                         
    var proxid: int                     

Basically what i want to do is to have a class file like a class in c++, so the type C_CASELLA i am using everywhere in my project like this:

var test1: C_CASELLA
test1 = C_CASELLA.new()
[..]
test1.free()

Waiting for the fix,how can i do the same thing without using autoload or some weird stuff? Indeed what i need its very simple: to have a global custom type like a struct or class in c++.

C++ can get around cyclic reference issues with it's forward declarations. Can't do that in gdscript for now, unfortunately that leaves us Waiting for Godot 4.0.

As a hacky workaround, if you want to make sure the node is of the correct type, but using that type would introduce a cyclic reference, you could try this:

#Type_A.gd
extends Node
class_name Type_A

var my_type_b: Type_B

func _ready():
    my_type_b = $"path/to/node"

#Type_B.gd
extends Node
class_name Type_B

var my_type_a: Node # can't use static typing with Type_A, otherwise cyclic dependency
var type_a_script: Resource

func _ready():
    my_type_a = $"path/to/node"
    type_a_script = load("res://Type_A.gd") # using preload would also introduce a cyclic dependency
    if my_type_a is type_a_script:
        print("I have a Type_A prop")
    else:
        print("ERROR: I don't have a Type_A prop")

The Type_B script won't have access to the autocomplete for its my_type_a variable, but at least it allows you to validate the type of the node without causing a cyclic dependency.

However, since gdscript uses duck typing, you don't need to worry about the correct type. It's a different mindset from C++, I suggest you read this if you don't know what duck typing means: https://devopedia.org/duck-typing

edit: simplified code in Type_B.gd

Thank you Bernard, very useful i am trying it.
I will post my final solution

Please log in or register to answer this question.

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 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 webmaster@godotengine.org with your username.