Reset a child node without restarting the whole scene

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

How can I make it so I can reset a specific child without having to restart the whole scene

I tried this

var playerScreen=preload("res://playerScreen.tscn")
var t = playerScreen.instance()

  func _input(ev):
	if Input.is_key_pressed(KEY_R):
		remove_child(t) 
		add_child(t)

But it just ends up canceling out, I would really appreciate if somebody could help me out

It isn’t really cancelling out though is it?
You remove ‘t’ from the scene tree but ‘t’ still exists exactly as it was when removed.
So then you immediately add it back to the scene tree and you see the same ‘t’ that you just removed.

There is another way to reset your scene (other than the one jgodfrey suggested below).
You can add a reset() function inside your child scene node. This function can then zero out that what needs zeroing and reset that what needs reseting.

func _input(ev):
    if Input.is_key_pressed(KEY_R):
        t.reset()

LeslieS | 2023-01-06 01:59

:bust_in_silhouette: Reply From: jgodfrey

If by “reset” you mean to simply start like the node was just created, then… I might suggest that you do just that - just queue_free() the existing node and instance() a new one.

So, something like:

var playerScreen=preload("res://playerScreen.tscn")
var t = playerScreen.instance()

func _input(ev):
    if Input.is_key_pressed(KEY_R):
        if t: queue_free(t)
        t = playerScreen.instance()
        add_child(t)

Just season to taste…