Cannot change name of instanced scene

Godot Version

Godot 4.3

Question

Hello, would appreciate any help with my issue:

Am trying to make it so that two functions work by freeing a scene and creating a new instance of the scene (ball for pong game). However, the new instance is giving me some trouble as it always instances with its default CharacterBody2D id as its name, which I found out by printing the list of root node’s children before and after. I am not quite sure how to fix or circumvent this, I’ve tried researching a bit about it as I could. Here’s the code in question:

function in script of parent (root scene):

func _reset_ball_instance():
	if $Ball and $Ball.has_method("free_instance"):
		$Ball.free_instance()
		
		# spawn new ball after timeout (1.3 second)
		await $Ball.on_cd_after_free_timeout
		var ball_scene = preload("res://Scenes/ball.tscn")
		var ball = ball_scene.instantiate()
		# ball.name = "Ball"
		
		call_deferred("add_child", ball)

$Ball.free_instance() (from ball scene script) waits a short while before freeing current instance and emitting a signal to calling function:

func free_instance():
	process_frozen = true
	$cd_after_free.start()
	
	await $cd_after_free.timeout
	on_cd_after_free_timeout.emit()
	queue_free()

So far, only been able to find the supposed solution of changing the name thru the instance variable but same bug occurs. Would greatly appreciate any help to figure out what’s wrong. Thank you!

Add “true” to your add_child arguments or it will never keep a “readable” name

add_child.call_deferred(ball, true)

But you should try to avoid getting dynamically created nodes by name if you can.

1 Like

Managed to make it work with that, thank you! I should’ve read the documentation further. Also took up your advice and remade it to use reference to the actual instance instead, its a lot reliable now, appreciate it!

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.