Problems using parent constructor

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Daniel Mircea
:warning: Old Version Published before Godot 3 was released.

I have the following code structure:

# base-player.gd
func _init(arg1, arg2):
  pass

# human.gd
extends "base-player.gd"

This works okay:

const PLAYER = preload("res://base-player.gd")

func do_stuff():
  PLAYER.new("a", "b")

However the next snippet does not:

const HUMAN_PLAYER = preload("res://human.gd")

func do_stuff():
  HUMAN_PLAYER.new("a", "b")

It raises Invalid call to function 'new' in base 'GDScript'. Expected 0 arguments..
But if I were to do HUMAN_PLAYER.new() it raises Invalid call to function '_init'. Expected 2 arguments..

Why is this happening? What’s the proper way of doing inheritance of basic scripts?

:bust_in_silhouette: Reply From: Daniel Mircea

I found this post https://forum.godotengine.org/6665/how-do-i-subclass-without-changing-the-constructor.

To sum it up, you can’t inherit the constructor method.

Quite odd, given that other scripting languages such as ruby and python allow it.

:bust_in_silhouette: Reply From: Zodman

In 3.1 I believe you can do this:

# human.gd
extends "base-player.gd"
func _init(arg1, arg2).(arg1, arg2):
    pass

You declare an init with 2 args (the first set of params). Then pass them through to the parent (the second set of params).