0 votes

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?

in Engine by (271 points)

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

1 Answer

+1 vote
Best answer

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

by (6,264 points)
selected by
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.