How to use _init()? It always throws an error for me.

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

As the title says, basically how do you use _init()? As far as I can tell, this is meant to act as a constructor and that’s what I’d like to use it for. In my case I’m just trying to extend a button, need to pass a few things through on creation.

Even something basic like

func _init():
pass

Will always throw the folowing error: E 0:00:00.735 _create_instance: Condition “r_error.error != Variant::CallError::CALL_OK” is true. Returned: __null
<C++ Source> modules/gdscript/gdscript.cpp:121 @ _create_instance()

Do I just have a bad install or something?

Function _init() is more like a function to call a class when it’s created. It’s similar to _ready(), but to my knowledge _init() is used when creating your own class.

In this documentation, it explains _init() and the basics of GDScript: GDScript reference — Godot Engine (stable) documentation in English

Here’s a previous question that might help you: https://forum.godotengine.org/86028/functions-_init-or-ready?show=86028#q86028

I’m not sure about your error code, but I’m pretty sure it has something to do with the fact that you are not calling _init() in your own custom class. With all that’s said though, I’m not sure what’s going on.
I hope you solve your problem.

TheJokingJack | 2021-06-01 20:13

Yeah I should’ve been more clear. I’m not trying to call _init() directly, I’m just trying to use it in terms of myObj.new(args). Anything with _init() defined though just immediatly throws that error on new. My current workaround has been removing the _ so that I can call myObj.new().init(args), which is just a bit janky.

TomMurtha | 2021-06-01 20:34

Could you share a bit of your code?
And have you tried the code in the documentation?

TheJokingJack | 2021-06-02 13:29