What class_name exactly do?

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

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 “class_name” 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

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: Memory leak if define class name and use it inside the same script · Issue #31378 · godotengine/godot · GitHub

Or you could be having a circular reference issue (C_CASELLA uses type OTHER_TYPE, and OTHER_TYPE uses type C_CASELLA), also a known issue: Classes recursive references lead to "script could not be loaded" · Issue #27136 · godotengine/godot · GitHub

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

Bernard Cloutier | 2020-10-09 14:13

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()

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++.

StefanoGrossiNature | 2020-10-09 14:32

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:

extends Node
class_name Type_A

var my_type_b: Type_B

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

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")
        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: Duck Typing

edit: simplified code in Type_B.gd

Bernard Cloutier | 2020-10-09 15:38

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

StefanoGrossiNature | 2020-10-10 10:35