I have some very basic misunderstanding about Godot so please bear with me. I have a scene that I'd like to instance with some initial values. With the instance() method I can't pass values. I tried with an _init but I get:

"Nonexistent function 'new' in base ''packed scene'"

I understand basic OOP but the way to use nodes and classes combined is confusing me in some point. How do I instance a scene with initial values? (needed for the _ready method)

am I doing things in a non Godot way?

I'll be honest, I have never figured out the _init thing in Godot. I just work around it by making a function called _setup with the arguments I need, and then I call _setup before calling add_child, so the data is there before _ready is called.

That said, if anyone knows how _init works, I am all ears! I'm sure there is a trick/technique that makes it work, I just have not stumbled upon it.

This is an old answer but judging by the comments its still a valid answer :

passing arguments to an instanced node constructor is not supported, because it would make it impossible to instance via the editor (which won't ask you to give the required arguments values).

So using a custom init() or setup() function is the simplest solution to achieve this behaviour.

https://godotengine.org/qa/4786/how-to-pass-parameters-when-instantiating-a-node

@llHate said: This is an old answer but judging by the comments its still a valid answer :

passing arguments to an instanced node constructor is not supported, because it would make it impossible to instance via the editor (which won't ask you to give the required arguments values).

So using a custom init() or setup() function is the simplest solution to achieve this behaviour.

https://godotengine.org/qa/4786/how-to-pass-parameters-when-instantiating-a-node

Thank you. I did as the last person in your link recommends, but the error is:

"Nonexistent function 'new' in base ''packed scene'"

@llHate said: This is an old answer but judging by the comments its still a valid answer :

passing arguments to an instanced node constructor is not supported, because it would make it impossible to instance via the editor (which won't ask you to give the required arguments values).

So using a custom init() or setup() function is the simplest solution to achieve this behaviour.

https://godotengine.org/qa/4786/how-to-pass-parameters-when-instantiating-a-node

Oh, wait. I was loading the scene, not the script. nevermind. Thank you, now works

@AnthropomorphicBeing said: "Nonexistent function 'new' in base ''packed scene'"

I just tested and it works fine for me. You didn't load the packed scene

Script1.gd var item = preload("res://Instances/Item.tscn").instance().setup("hello")

Item.gd

func setup(value)
  variable = value
  return self
2 years later