What's the logic behind being able to override _init() with argument?

Godot Version



Just today, I learned that you can make a custom Resource class, and put argument in _init() function, the compiler will even ask for the parameter when using new() function.

The person showed me that explained it’s just something other language has, so he assumed that’s possible with GDScript and found the solution; but for me, who only ever wrote GDScript, I cannot grasp the logic behind it.
For me, virtual method is just something empty you write in the parent class so you can decide functionality later. Being able to add parameters is like…I don’t know, _ready() doesn’t do that, _physics_process(delta) doesn’t do that, so why does _init() do that?
The doc kind of just assumed I already know that, so it didn’t help.

I know there’s something fundamental I’m missing here, can someone explain it?
Are there some implications I should be looking out, like duplicate() doesn’t work as the doc mentioned, or this post said save and load would fail?

Thanks for reading.

_init() is like the constructor in C-like languages. So when you have your own class MyClass with the _init function overridden with e.g. two parameters, you can create an object of type MyClass via

var myObject: MyClass = MyClass.new(a, b)

So _init() is THE only function that can be override with argument, correct?

Pretty much, yeah. It’s a very special function, as it’s also not called internally by Godot, but indirectly by the user via .new()

1 Like