Godot: Allow passing _init() arguments into instance()

Created on 9 Apr 2019  路  10Comments  路  Source: godotengine/godot

I have a script with some parameters in it's _init(..). While trivial to pass in args if creating objects via .new(), I have not found a way to pass something in when instancing a scene. A workaround is to instead have an init(...) and call it right after instance. However, would be nice to be able to just .instance(args)

archived feature proposal core

Most helpful comment

I was about to report a similar issue for C# and found this. I can't say anything about the difficulty of implementing this feature, but I think it'd be quite useful, conceptually at least.

It's always better to provide all required dependencies of a class in its constructor, so that they remain unchanged throughout the lifecycle of the instance.

All 10 comments

Comments made on discord:

A:
instancing a packed scene doesn't allow passing parameters like init
from what I see

B:
because it does not construct a new class object, but recreates a saved one
once it's been recreated, feel free to call a secondary initializer

C:
Well, it clones a whole tree of one to many nodes. passing args to that for all nodes would be odd
although I think a construct could be made to forward to root node

I was about to report a similar issue for C# and found this. I can't say anything about the difficulty of implementing this feature, but I think it'd be quite useful, conceptually at least.

It's always better to provide all required dependencies of a class in its constructor, so that they remain unchanged throughout the lifecycle of the instance.

It would be quite useful, I have to remember to use arbitrarily different conventions when I instantiate a single-node object vs a scene, which are often functionally similar objects in my game. Passing parameters to the root node's _init() function seems like a pretty straightforward pattern.

I was about to post this suggestion, after searching, there are already several similar issues here.

28712

After google, a suggested way to pass parameters through scenes is using a global singleton,
However, this way makes all the scenes require a singleton to work, make the scenes not easy for reusing,

I'm having this issue right now, when i load and nodes on the fly i need to pass some data from the parent and it would be very weird if the child have to ask it for the parent. It would be clearer the parent giving it. If the child _init have any argument: _init(arg), when calling instance() it throws an C++ error on the gdscript.cpp, _create_instance().

My suggestion: change .instance() to .new() on loaded scenes (PackedScene) and remove instance() (or keep it working for compatibility). Allow passing arguments to new instances through .new(arg) -> _init(arg).
It also works as a reminder what kind of data that Node/Class need to work.

I'm also going through this problem right now. I think the only way for now is to just create a pseudo-constructor for your class.

I'm here to lend my support because I too would like this feature. If a scene has a gdscript attached to it, and the script initialises with the node, why can't the .instance() method take parameters like .new() does.

This is particularly relevant for scenes where you only ever instance them from code.

I'm now having to instance my scene then call an initialise function.

I'd also love to see a way to pass parameters when instancing

Feature and improvement proposals for the Godot Engine are now being discussed and reviewed in a dedicated Godot Improvement Proposals (GIP) (godotengine/godot-proposals) issue tracker. The GIP tracker has a detailed issue template designed so that proposals include all the relevant information to start a productive discussion and help the community assess the validity of the proposal for the engine.

The main (godotengine/godot) tracker is now solely dedicated to bug reports and Pull Requests, enabling contributors to have a better focus on bug fixing work. Therefore, we are now closing all older feature proposals on the main issue tracker.

If you are interested in this feature proposal, please open a new proposal on the GIP tracker following the given issue template (after checking that it doesn't exist already). Be sure to reference this closed issue if it includes any relevant discussion (which you are also encouraged to summarize in the new proposal). Thanks in advance!

Was this page helpful?
0 / 5 - 0 ratings