Define variables from a parent class?

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

I have something like this

class ParentClass:
    var a : array
    func do_something():
        a.append(something)
        a.something_else()

class ChildClass inherits ParentClass:
    var a = [1,2,3]

I want to have a child class define the variables for the parent class that it extends, but gdscript throws an error saying that the parent class already has that member variable defined. I’m aware something could be done using init but I wanted to avoid having to use that if possible and just have my variables defined. Is there any other simple way to accomplish what I’m trying to do?

Have you tried not defining it in the child class, i.e. just put a = [1,2,3] (remove the var)

SteveSmith | 2022-10-10 19:40

:bust_in_silhouette: Reply From: Wakatta

There is no need to redefine your variables as they are hereditary and pass down to all sub classes and would be grammatically incorrect otherwise.

So your options are to assign your vars in the _ready(): func of each sub class or encapsulate them within setget functions