[GDscript] How i can't "instanciate class A" in "class A" code with gdcript?

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

i am trying in my gscript file “name.gd” to do:

class A:
	func _init():
		pass
		
	func a_function():
		var new_A = A.new() # <= Here

it’s not working: Parser Error: Identifier not found: A

Looks like a bug/unsupported feature in GDScript. Recursive classes should be possible. I bet there is an issue on Github about this but didn’t searched yet.

Zylann | 2016-12-03 15:04

Doing this:

class A:
    func _init():
        pass

class A:
    func _init():
        pass

    func a_function():
        var new_A = A.new() #uses the second A

Should work, but yes, looks like a bug…
There’s the option to use a separate file and loading it inside the same file for recursive access.

eons | 2016-12-04 10:11

:bust_in_silhouette: Reply From: eons

Found the strange way it works:

You need to use get_script to get the class (???)

class A:
    func _init():
        pass

    func a_function():
        var new_A = get_script().new()