Cyrille
Cyrille

Reputation: 14533

Godot/GdScript How to instantiate a class from a static function?

I have 2 scripts:

A.gd

class_name A    

var v = 0    

func _init(v_):
    v = v_

B.gd

class_name B

var A = preload("res://A.gd")

static func Add(a1:A, a2:A):
    return A.new(a1.v + a2.v)

I don't understand why I have this error while I'm typing:

res://B.gd:6 - Parse Error: Can't access member variable ("A") from a static function.

Apparently, I can't instantiate A from a static function. If I remove static, there are no more errors. What am I doing wrong? How can I instantiate A from a static function?

Upvotes: 2

Views: 2960

Answers (1)

Theraot
Theraot

Reputation: 40220

There are no static variables in Godot. Thus that var A is not a static variable. And thus it is not available from a static function.

On the other hand, if you gave a name to your class with class_name - which you did - then that class name exist everywhere. Remove var A.

Upvotes: 2

Related Questions