Why is the name of the instanced scene looks wierd?

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

Im making this incremental game which i want to make infinite buttons for, and for that im using a for loop to make a lot of buttons at once, but the name i set for the button (which matters in my code for determining the price and stuff) turns all wierd, for example i give it a name of ‘1’ because its the first button, godot produces me ‘@1@2’ and that screws up all my code.
Can anyone help please?

Instancing code:

for item in range(len(copy_dict['default_costs']['Multiplier'])):
	var new_button = multi_button_scene.instance()
	new_button.get_node('Give').text = "+" + get_notation(copy_dict['default_costs']['Multiplier'][str(item + 1)][1]) + " Multiplier"
	new_button.get_node('Cost').text = "$" + get_notation(copy_dict['default_costs']['Multiplier'][str(item + 1)][1])
	new_button.name = str(item + 1)

Here is the dictionary that is uses (i dont know if you need it):

var default_costs = {
'Multiplier': {
	'1': [Big.new(0.1, 0), Big.new(10, 0), Big.new(0, 0)],
	'2': [Big.new(0.3, 0), Big.new(60, 0), Big.new(0, 0)],
	'3': [Big.new(0.7, 0), Big.new(550, 0), Big.new(0, 0)],
	'4': [Big.new(1.5, 0), Big.new(1750, 0), Big.new(0, 0)],
	'5': [Big.new(2.75, 0), Big.new(5500, 0), Big.new(0, 0)]

It generates a unique name when it instances a class.

SteveSmith | 2023-01-03 22:16

:bust_in_silhouette: Reply From: jgodfrey

Names must be unique within a common parent. So, you can’t name multiple nodes 1 if they have the same parent. Well, you can, but (as you see) Godot will append some additional info to ensure their uniqueness.

A few tips…

  • When you call add_child(), you can pass in true as a second argument to cause the generation of a more legible name (essentially, with no @ signs).
  • If you pass that 2nd arg, and provide truly unique names within a parent, I think Godot will keep them as-is.

Another option would be to not care about the assigned name and instead just store it (whatever it is) along with other interesting node info for future reference…

1 Like