When can I override _init() with non-optional arguments?

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

I’ve discovered that overriding _init() with arguments, like _init(val), causes whatever scene this script is attached to to load without the script on runtime. This error also appears:

E 0:00:00:0202 Condition ’ r_error.error != Variant::CallError::CALL_OK ’ is true. returned: __null
modules/gdscript/gdscript.cpp:129 @ _create_instance()

If I make all arguments optional, like _init(val = 0), the scene loads with script and no errors.

I’d like to know when it’s possible to override _init() with non-optional arguments. I couldn’t find anything on this subject.

:bust_in_silhouette: Reply From: Zylann

If your script instance is going to be saved/loaded inside a scene, it must have a parameterless constructor, or a constructor with all arguments optional. The reason is, the “state” of the script instance comes from the scene file (variables with export or the STORAGE hint), it’s not really building a new one.
Otherwise it’s fine to have arguments in _init, for cases where you are calling it with new in your scripts.