I have a base class with a custom _init(a, b)
function. I have a subclass in which I don't want to redefine _init
. Can I do this? The answer seems to be no:
# in base_class.gd
extends Node
def _init(a, b):
# etc etc
def some_behavior():
print("behave")
# in subclass.gd
extends "base_class.gd"
def some_behavior():
print("misbehave")
# in some other script
var Subclass = preload("subclass.gd")
var sub_instance = Subclass.new("a", "b")
# here I get "Invalid call to function 'new' in base 'GDScript'. Expected 0 arguments."
Edit to add:
To be completely clear about what I am trying to achieve, here is how you would accomplish this in Python:
class A(object):
def __init__(self, a, b):
self.a = a
self.b = b
def behave(self):
print("I am an A. I am " + str(self.a) + str(self.b))
class B(A):
def behave(self):
print("I am a B. I am not " + str(self.b) + str(self.a))
a, b = A(1, 2), B(3, 4)
a.behave() # prints: I am an A. I am 12
b.behave() # prints: I am a B. I am not 43
The point is that A and B have different behaviors in the behave
function, but the object initialization is the same, so there's no need to rewrite the __init__
function.