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

_init with parameters breaks when used with scene tree editor #75920

Closed
ishax-kos opened this issue Apr 11, 2023 · 4 comments
Closed

_init with parameters breaks when used with scene tree editor #75920

ishax-kos opened this issue Apr 11, 2023 · 4 comments

Comments

@ishax-kos
Copy link

Godot version

4.0.2

System information

Windows 10

Issue description

I was exploring whether I should instantiate a number of slot node from an exported count parameter, or collect the manually added nodes in said parameter. I had created an _init function to properly initialize these slots, when my static typing seemed to hit this error.

Steps to reproduce

The problem is demonstrated in the provided project with an assert. Note that the problem goes away if there are no parameters in _init for the test_class.gd script. Additionally, a cpp exception is emitted.

Minimal reproduction project

Bug_report_class_name.zip

@dalexeev
Copy link
Member

You should not create node classes with required constructor parameters, they cannot be instantiated from the scene editor. Also, when adding a node from code, MyNodeClass.new() will only create the root of the scene, not the entire scene. Instead, you can add a static method that creates a node or instantiates a scene.

extends Node
class_name MyNodeClass

var param: String

static func create(p_param: String) -> MyNodeClass:
    var packed_scene: PackedScene = load("res://my_node_class.tscn")
    var node: MyNodeClass = packed_scene.instantiate()
    node.param = p_param
    return node

Usage:

var node := MyNodeClass.create("test")
add_child(node)

Related:

I also remember that there was a proposal/comment about adding the ability to specify node/resource parameters when creating in the editor (but IMO this unnecessarily complicates things).

@ishax-kos
Copy link
Author

ishax-kos commented Apr 11, 2023

It's definitely not a great thing to do, but the engine, instead of having a sensible error, will proceed without informing the user what is happening. It's failing with errors that don't give the user the information they need. Not to mention having unusual side effects. The program should not run.

@Calinou
Copy link
Member

Calinou commented May 3, 2023

@Calinou Calinou closed this as not planned Won't fix, can't repro, duplicate, stale May 3, 2023
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

4 participants