How to get name of node of instanced scene?

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

I know there is the function get_basename() on type String but if I call it on name, I get printed @Test@99. So the node’s name of my instanced scene be “Test”, how to get it? I want to do that because sometimes another Object with the same script is added.

I know this question is tagged under gdscript, but I wanted the same thing in c# and your question helped me figure out the way to do it is using the Name property of a node object (ex: Node x; GD.Print(x.Name);)

wooshuwu | 2022-02-04 19:13

:bust_in_silhouette: Reply From: Inces

I had that problem also and I didn’t find any option to call name or even custom class name. Filename worked for me, it is unique just like name and means resource path of your scene. It should do if You only need to identify nodes.

:bust_in_silhouette: Reply From: Wakatta
func node_name():
	return str(name.replace("@", "").replace(str(int(name)), ""))

This should be a setget method of node. I’m still only starting out with Godot, and it is the second project that I need this. Does anyone know if this was already suggested to the Godot team?
Or maybe I am not organizing my projects correctly to have to rely on something like this…

SomeMedtnerGuy | 2022-10-02 21:08

Actually the way Godot handles multiple instance names is rather intelligent and it is up to you the user to regulate it for your use case if the default proves problematic.


func _ready():
    for i in range(10):
        var scene = test_scene.instance() = + str(I)
        # or = + str(scene.get_instance_id())

Wakatta | 2022-10-02 21:59