How to name a class?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By EAngeles02
:warning: Old Version Published before Godot 3 was released.

What I want to accomplish here is extends a class without using the class file, going through the documentation I found this:

“By default, the body of a script file is an unnamed class and it can only be referenced externally as a resource or file.”

How can I set the class name?

:bust_in_silhouette: Reply From: Calinou

It’s unnamed, so you don’t choose its name directly. You can access it indirectly by accessing the node which contains the script using get_node(), like this:

get_node("/root/Level/Player").my_function(2, 5)
get_node("../Apple").count = 6
:bust_in_silhouette: Reply From: Bojidar Marinov

File-level classes in godot are unnamed by design. You can access them via preload/load, in which case the name of the class would be the name of the variable you assign it to. You can also access them via get_node, as @Calinou pointed out. After you have access to the node you can also use get_script on it, and you would again have access to that class.

Subclasses are a bit different. They do have a name, by which they are referenced in the parent class.

Here is a code sample, summarizing all I wrote above:

extends Node2D
# It doesn't actually have a name, but we give it one
const MyClass = preload("res://utils/my_class.gd") 

# To other scripts, subclasses behave like static constants
const MySubClass = preload("res://utils/subclasses.gd").MySubClass 

# Subclasses, on the other hand, have a name
class SubClass extends MyClass:
    var i = 0

var classes = []
func _ready():
    for i in range(4):
        # Load when it is not a constant expression
        classes.push_back(load(str("res://utils/classes/", i, ".gd")))
    
    # Just call functions on scripts attached to other nodes
    get_node("../another_node").do_stuff()
    # ... or change properties
    get_node("../yet_another_node").a += 1

    print(SubClass.new().i) # == 0
    # Get the script, and create a new instance of it
    var yan_script = get_node("../yet_another_node").get_script()
    print(yan_script.new().a)
    

Thank you for this

Robster | 2017-02-02 14:03

:bust_in_silhouette: Reply From: Jeremi360

I never try it, but i see some time a go, in build in keywords in gdscript package for atom editor, keyword “class” - maybe you just must do some think like this:
class class_name extends Node

Unfortunately, that would create a subclass instead.

Bojidar Marinov | 2016-03-15 09:41

:bust_in_silhouette: Reply From: Javier Alfonso

Other answers are correct, partially.

Classes are anonymous by default but you can name them with class_name so they became a first-class citizen of Godot.

For example:

extends Object

class_name MyPlayer

export var something: String

func doNothing():
  pass

Exactly what I needed for my project, thanks!

BCS | 2023-07-07 15:18