Change node at runtime

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Koroshiya

I make a function to change de node type between Button and EdLine:

func _on_Name_pressed(edit = null):
	var node
	var sig
	var old_node =  get_node("Name")
	get_node("Name").queue_free()
	if get_node("Name") is Button:
		node = LineEdit.new()
		sig = "text_entered"
	else:
		node = Button.new()
		sig = "pressed"
	get_node(".").add_child(node)
	node.set_name(old_node.name)
	if get_node("Name") is LineEdit:
		node.set_text(edit)
	else:
		node.set_text(old_node.text)
	get_node("Name").connect(sig, self, "_on_Name_pressed")
	get_node("Name").grab_focus()

I cant´t use “free()” and must use “queue_free()”, I guess the node is not deleted until the whole function does not finish.
The problem is in the last 2 lines when I do “get_node(“Name”)”.
I guess since the old node hasn’t been deleted yet, the lines conect the signal and grab_focus on the old node, no the new.

By the way, in the line i make “node.set_name(old_node.name)”, are there 2 nodes, which are called the same ?

I have discovered the function “get_instance_id()”, but I don’t know what to do with it ?

Any ideas, without changing the name of the node ?

Can’t you just hide and reveal two nodes that are present in a scene for whole time?

Reloecc | 2020-09-14 10:48

There is no fixed number of buttons, and the buttons are inside VBoxContainers, which complicates things.
The “remove_child()” option, it seems simpler.

Koroshiya | 2020-09-14 11:31

:bust_in_silhouette: Reply From: Reloecc

A) You may use remove_child() to solve “I have two same named nodes”.
B) You may connect signal to your variable called node instead of get_node("Name")