Godot-proposals: Make it possible to pass arguments to `PackedScene.instance()`

Created on 15 Sep 2020  路  5Comments  路  Source: godotengine/godot-proposals

Describe the project you are working on:
Any Godot project.

Describe the problem or limitation you are having in your project:
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 using said script.

Describe the feature / enhancement and how it helps to overcome the problem or limitation:
It would be nice to be able to have scene instancing accept args to configure the object.

Describe how your proposal will work, with code, pseudocode, mockups, and/or diagrams:
Like: .instance(args), where those would then be passed into the scripts __init(...).

If this enhancement will not be used often, can it be worked around with a few lines of script?:
A workaround is to instead have an additional init(...) function and call it right after instancing the scene.

Is there a reason why this should be core and not an add-on in the asset library?:
It is not something that can be implemented as a (simple) add-on. Additionally, this would make scenes/scripts work more consistently.

Previous Discussion
https://github.com/godotengine/godot/issues/27834
https://github.com/godotengine/godot/issues/28712

core

Most helpful comment

@aaronfranke: That's fine when you have one or two properties you need to set. When you have ten...

All 5 comments

What's wrong with just doing this?

var instance = some_scene.instance()
instance.some_property = some_value

@aaronfranke: That's fine when you have one or two properties you need to set. When you have ten...

Seems like an obvious and easy thing to do. If you need to keep the edit_state argument (which I admittedly do not understand), we can just pass the arguments in an array.

The biggest reason this is so useful in my mind is that it allows a cleaner architecture. We can have a singular _init() function that does what we want an initialization function to do, have it run when a node is initializing, and have an interface to it when we're initializing a Node. Makes sense to me.

@aaronfranke Setting variables after creating an instance is error-prone. Consider this:

  • You or your team-mate can forget to set a variable

  • If you want to change your initialization logic, you'll need to go to every place where you create an instance (i.e its not DRY)

One more thing to consider is that a lot of newcomers _expect_ to be able to use constructors since the chances are their old language supports them.

@AnEnigmaticBug Right now every time you instance an object you are repeating the same pattern to create the instance, assign a transform (for example) and add it to the scene.

If you want to change your initialization logic, you'll need to go to every place where you create an instance

That happen in any lenguage. What we can have instead is a instance method that acceps basic and optional parameters like the parent scene, the transform, etc

I would like to have a init method to instance. If the firm defines what the object needs to exist. If it changes it should change everywere. To have it DRY we can have a factory object like in every lenguage

Was this page helpful?
0 / 5 - 0 ratings

Related issues

aaronfranke picture aaronfranke  路  3Comments

SpyrexDE picture SpyrexDE  路  3Comments

arkology picture arkology  路  3Comments

rainlizard picture rainlizard  路  3Comments

wijat picture wijat  路  3Comments