0 votes

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.


in Engine by (78 points)

1 Answer

+2 votes
  • 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'
by (4,163 points)
edited by

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!

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

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

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.

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())

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.

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.