Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow Parameters in instanciate Method of PackedScene in Godot #10118

Closed
DevSavvySerge opened this issue Jul 4, 2024 · 2 comments
Closed

Allow Parameters in instanciate Method of PackedScene in Godot #10118

DevSavvySerge opened this issue Jul 4, 2024 · 2 comments

Comments

@DevSavvySerge
Copy link

Describe the project you are working on

I'm developing a game in Godot where managing object instantiation and initialization is crucial for maintaining a clear and efficient codebase.

Describe the problem or limitation you are having in your project

Currently, when using PackedScene in Godot to instantiate objects, there is no direct way to pass parameters to the constructor (_init) of the instantiated scene. This limitation forces developers to use workarounds such as creating separate init methods without underscores, which deviates from standard practices and leads to confusion in the codebase.

Describe the feature / enhancement and how it helps to overcome the problem or limitation

I propose enhancing the instanciate method of PackedScene in Godot to allow passing parameters directly to the constructor (_init) of the instantiated scene. This enhancement would streamline object initialization by enabling developers to initialize scenes with specific parameters during instantiation, promoting cleaner and more intuitive code practices.

Describe how your proposal will work, with code, pseudo-code, mock-ups, and/or diagrams

Here's a basic outline of how the proposed enhancement could work:

Enhance the instanciate method to accept parameters:
# Example pseudo-code
var parameters = {
    "param1": value1,
    "param2": value2
}
var instance = packed_scene.instanciate(parameters)

Allow passing parameters to the _init constructor of the instantiated scene:

# Inside the scene script
func _init(param1, param2):
    # Initialize the scene with the provided parameters
    self.param1 = param1
    self.param2 = param2

Benefits of allowing parameters in instanciate:

Enables cleaner and more maintainable code by adhering to standard initialization practices.
Reduces the need for separate initialization methods (init without underscores), minimizing confusion in the codebase.
Facilitates clearer understanding of scene initialization logic at the point of instantiation.

If this enhancement will not be used often, can it be worked around with a few lines of script?

Currently, developers can work around this limitation by manually setting properties after instantiation or by using separate initialization methods. However, these workarounds can lead to less efficient code and introduce unnecessary complexity, especially in projects with complex object hierarchies.

Is there a reason why this should be core and not an add-on in the asset library?

The ability to pass parameters to the _init constructor of scenes directly during instantiation is a fundamental feature that aligns with best practices in object-oriented programming. Implementing it as a core feature in Godot would improve developer productivity, maintainability, and code clarity across projects. It would also ensure consistent behavior and integration with the existing scene management system within the Godot engine.

@AThousandShips AThousandShips changed the title Enhancement Request: Allow Parameters in instanciate Method of PackedScene in Godot Allow Parameters in instanciate Method of PackedScene in Godot Jul 4, 2024
@AThousandShips
Copy link
Member

Thank you for your proposal, consolidating in:

Please search for open proposals before making new proposals

@AThousandShips AThousandShips closed this as not planned Won't fix, can't repro, duplicate, stale Jul 4, 2024
@DevSavvySerge
Copy link
Author

Thank you for your proposal, consolidating in:

* [Make it possible to pass arguments to `PackedScene.instance()` #1513](https://github.com/godotengine/godot-proposals/issues/1513)

Please search for open proposals before making new proposals

I did, it's not easy to guess how others could have phrased the same kind of proposal.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants