#Custom class in typed array leads to `Can't call non-static function` error

5 messages · Page 1 of 1 (latest)

rapid turret
#

Hey all, I'm having an issue that I'm hoping is just a misunderstanding of how to use something. I have a custom class, like so:

class TestClass:
    var num = 0

    func _init(numIn):
        num = numIn

    func update():
        print("hello")
        num += 1```


In a scene, I want to create multiple instances, and store them in an array:

var tests = [TestClass]

func _ready():
var new_test = Debug3d.TestClass.new(5)
tests.append(new_test)

    var new_test2 = Debug3d.TestClass.new(5)
tests.append(new_test2)

Then update them later:

func _process(delta):
for test in tests:
test.update()```

This all works. However, when var tests = [TestClass] uses a typed array like I've written here, every call to update results in:
Can't call non-static function 'update' in script, despite calling it just fine. However, if I do var tests = [], it works just the same, except with no error.

Is there something about gdscript 2.0's typed arrays that I'm misunderstanding here?

FWIW, this is the windows 64 v4.0.stable.official [92bee43ad] build

reef ice
#

What you're doing is putting the type itself in the array

hallow dawn
#

var tests = [TestClass] creates an array with one item, which is the class (not instanced). If you want to type the array you would do var tests: Array[TestClass] = []

reef ice
#

Whoops I used the wrong syntax too

rapid turret
#

Ahhh, got it