Call class creation in class itself

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

The basic task stymies me. Put it in code:

class Foo:
    func _init():
        pass

    func some_func():
        var foo = Foo.new()

Will not work, because Foo identifier is not visible in some_func and in Foo class itself.

What have I missed? Or how to overcome the trouble?

First way I found for myself is to move class in singleton script, so one can access class identifier through singleton object. But I don’t want to put my class into singleton for such a silly cause.

Second way is (probably) to create private function which returns Foo as load(“Foo path”), but this trick is a dirty one.

:bust_in_silhouette: Reply From: wyattb

Works for me.

class Foo:
	func _init():
		pass

	func some_func():
		var foo = Foo.new()
		pass
		
# Called when the node enters the scene tree for the first time.
func _ready():
	
	var f = Foo.new()
	f.some_func()

Hm, very strange. I’ve tested this example, and I got Parser Error: Identifier not found: Foo in the line with Foo.new(). Using v3.0.6.stable.official.8314054

GlaDos28 | 2018-09-18 18:39

My apologies. I am using 3.1 alpha. I guess it was fixed?

wyattb | 2018-09-18 18:55

Well, time will show. I’ll wait for the stable release though, but thanks for the reply, probably, it is so :slight_smile:

GlaDos28 | 2018-09-18 19:12