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

[Solved]A way to instance node with parameter #28712

Closed
yikescloud opened this issue May 5, 2019 · 9 comments
Closed

[Solved]A way to instance node with parameter #28712

yikescloud opened this issue May 5, 2019 · 9 comments

Comments

@yikescloud
Copy link

<Godot 3.1>

Is there a way to pass some parameter to a node when instancing it? I mean the same time, not define a custom init function and then call it later, that will cause some async problem. Or can a node can know who instance itself, so we can use get_parent() to get param. I think if there is a way like than, code will be more simple. Or just because that will cause some performance issue?

@yikescloud
Copy link
Author

If a packedScene's child node need get param who instance it, that will be a little problem, I should call every child in packed scene to change the var. The only way I think better is use the singleton to pass the param, but I think that's not naturally

@KoBeWi
Copy link
Member

KoBeWi commented May 6, 2019

But you can set any properties after instancing and _ready() is called after adding to scene. Not sure why would you need some instancing parameters. Like

var node = preload("res://somenode.tscn").instance()
node.some_variable = some_value
node.creator = self
add_child(node) #here the _ready() is called

There aren't any "async problems" with that.

@groud
Copy link
Member

groud commented May 7, 2019

There's absolutely no "async problems" in setting a node's variable just after it's instantiation.

@yikescloud
Copy link
Author

Thank you guys, I see that a node wont instance child before adding to the tree

@yikescloud
Copy link
Author

I think if godot give a virtual function order, then everyone know how does it work. Just like the"ready", "enter tree", "draw", "input", "unhandled input", sometimes I feel confuse to the order they run

@yikescloud yikescloud changed the title A way to instance node with parameter [Solved]A way to instance node with parameter May 7, 2019
@KoBeWi
Copy link
Member

KoBeWi commented May 23, 2019

I think if godot give a virtual function order, then everyone know how does it work.

Eh, is there any tutorial about that or any reference point in docs? If not, this could be changed to documentation issue.

@Anutrix
Copy link
Contributor

Anutrix commented Sep 1, 2019

This would useful in scene testing. Because we don't know if pseudo-constructor is already called or not.
Let me give an example. I have a init() function as an alternative to scene constructor with optional parameters. When instancing in another running scene(Main.tscn) I can call it with parameters. But, I have no way to call it when I run/play(F6) just the scene(Parts.tscn).

Let this be Main.tscn's main_game.gd:

export(int) var No_Of_Levels=0
var bd=load("res://Parts.tscn")

func _ready():
	var x=bd.instance()
	x.init(No_Of_Levels) #pseudo-constructor call
	add_child(x)

Let this be Parts.tscn's Parts.gd:

var curr_player
var part_id
var level

func _ready():
	#init() #This can't be un-commented because then init() would be called twice when instance in another scene
	pass

func init(lvl=0,p_id="0",startplayer="X"): #pseudo-constructor
	#some action like follows
	level=lvl
	part_id=p_id
	curr_player = startplayer
	reset_board()
	var a = connect('board_done', get_parent(), 'set_cell')
	if a==0:
		print("Level ", level, " Board ", board_id," is connected.")
	else:
		print("Level ", level, " Board ", board_id," failed to connect.")
	#some action like above

Having a instance with parameters would solve it because then I could just rename my init to _ready; And instance with bd.instance(1,1,"Y").

@alexzheng
Copy link

alexzheng commented Jan 3, 2020

I can't agree more.
The _init() is called automatically when new a node.
And if call new with parameters, like Scene.new("a", "b"), the func _init(a, b) would be called as a constructor. That's good.
However, in most situations, PackedScene.instance() would be used to instance scene. then the only _init() without any parameters would be called. It's not possible to pass parameters to the scene.
If the instance can be declared as
Node instance( GenEditState edit_state=0, ... ) , the _init constructor would be really useful.

Also, this can be used for the change_scene as change_scene(String path, ...)

@KoBeWi
Copy link
Member

KoBeWi commented May 27, 2020

Feature and improvement proposals for the Godot Engine are now being discussed and reviewed in a dedicated Godot Improvement Proposals (GIP) (godotengine/godot-proposals) issue tracker. The GIP tracker has a detailed issue template designed so that proposals include all the relevant information to start a productive discussion and help the community assess the validity of the proposal for the engine.

The main (godotengine/godot) tracker is now solely dedicated to bug reports and Pull Requests, enabling contributors to have a better focus on bug fixing work. Therefore, we are now closing all older feature proposals on the main issue tracker.

If you are interested in this feature proposal, please open a new proposal on the GIP tracker following the given issue template (after checking that it doesn't exist already). Be sure to reference this closed issue if it includes any relevant discussion (which you are also encouraged to summarize in the new proposal). Thanks in advance!

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

6 participants