Did something change in Node set_name() and get_name() ?

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

In a board game propotype I have a main scene in which I create many instance of a scene (a tile). I then use set_name() to give a specific name to each instance in order to precisely retrieve every istance when needed. Names are something like 1_1, 1_2, 1_3... 2_1, 2_2, 2_3… etc.

In Godot v.3.0.6 the game used to work since get_name() returned exaclty what was expected, while in the new 3.1.1 version it does not work: get_name() returns something like @1_2 followed by another @ and a number (ie. @1_2@33 or @2_4@45 etc.).

I could manage it by trimming the first @ and getting the following 3 chars, but I would like to know if there is a reason behind this behaviour or if I am doing something wrong.

Thanks
Dave

:bust_in_silhouette: Reply From: Dlean Jeans
  • void add_child ( Node node, bool legible_unique_name=false )

Pass true when you add_child:

add_child(node, true)

Also, use the name property, it’s shorter:

node.name = '1'
print(node.name)

Dlean Jeans,
thank you for you suggestion.
Unfortunately it does not work.

Using either true or false (the default value), or 1 or 0 as the second parameter of the add_child() method still gives me nodes whith name like @1_8@85 and consequently I get the error Attempt to call function 'get_node()' in base 'null instance' on a null instance. when I try to programmatically use get_node("1_8").

I found this https://github.com/godotengine/godot/issues/2103 and I realize I was in the same situation.
Then I thougth I could randomly change the name of every Node before to call_deferred("queue_free") on them.

It works!

DaveMS | 2019-07-16 20:22

You gotta change the name before add_child(node, true):

var node = Node.new()
node.name = '1'
add_child(node)

node = Node.new()
add_child(node, true)
node.name = '1' # doesn't work

node = Node.new()
node.name = '1' # works
add_child(node, true)

Click here to run it.

Dlean Jeans | 2019-07-17 03:21

I tried to set the Node.name either before or after the add_child().
It does not work.

After reading the issue at https://github.com/godotengine/godot/issues/2103 realized that the error occurs only if a Node with the same name was present in the previous game’s board, meaning the engine did not remove it from memory.

So I patched the function that clear the scene before to move to the following one:

tiles = get_tree().get_nodes_in_group("Tiles")
	for tile in tiles:
		tile.name = str(randi())
		tile.call_deferred("queue_free")

In this case, the old tiles are all assigned a random name which it does not conflicts with the names of the new tiles in the following game’s board, and the engine can remove them later on.

DaveMS | 2019-07-17 14:13