+1 vote

I have a scene who's root node takes some arguments at instantiation time (that is, in _init(arg1, arg2). Unfortunately, I don't see a way to pass arguments to said node when instance()'ing the scene containing it.

If I just blindly call instance() anyway, it seems like the node is simply ignored. Is there a way to do this?

in Engine by (27 points)

I had the same problem and solved it with a very ugly hack

1 Answer

+2 votes
Best answer

There is currently no built-in way to pass arguments to a scene like that. You have to call a function of yours probably, like this:

var scene = preload("your_scene.tscn").instance()
scene.initialize(params) # init root node

There might be only one case where _init will be called, is when you do this:

var node = preload("your_script.gd").new(params)

But for scenes, you have to go the other way.

by (28,735 points)
selected by

Thanks, I did a preload("script.gd").new(arg1, arg2), where script.gd instantiates its child nodes in _ready(), and that indeed worked.

Is there any particular reason to use scenes instead of a script like this then? I suppose if you have custom collision shapes or some node which really wants to be set up with the editor, that might be a good reason to use a scene. Is that the only reason?

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.