+1 vote

I'm not sure if this is possible but can I name instances? I want to have 3 sets of 3 enemies on the screen and give them orders (like position and scale or queue_free), depending on what the user does.
Basically something like 3 snakes, 3 boars and 3 crows would be on instanced on screen and when the user would interact with an Area2d the first snake would find the player and attack him. I have the AStar path finding working so I can make them go to the player's position. But not sure how to differentiate between instanced nodes.

I have a very simple instancing code at _ready()

for i in range(3):
    var s= snake_sprite.instance()
    add_child(s)
for i in range(3):
    var s = boar_sprite.instance()
    add_child(s)
for i in range(3):
    var s = crow_sprite.instance()
    add_child(s)

Hope my query makes sense.

in Engine by (59 points)
edited by

2 Answers

0 votes

Try this:

for i in range(3):
    var s= snakesprite.instance()
    # set the names to snakesprite_1, snakesprite_2, snakesprite_3
    s.set_name("snakesprite_" + str(i))
    addchild(s)
by (308 points)
+4 votes

you can name it as @Michael Paul said.
but it's not safe to get a reference of objects by name.

it's better to save the reference somewhere and use it.

var list_snake = []
var list_boar = []
var list_crow = []

func _ready():
    for i in range(3):
        var s= snake_sprite.instance()
        add_child(s)
        list_snake.push_back(s)
    for i in range(3):
        var s = boar_sprite.instance()
        add_child(s)
        list_boar.push_back(s)
    for i in range(3):
        var s = crow_sprite.instance()
        add_child(s)
        list_crow.push_back(s)

then use list_snake[0] if you want one of snakes.

by (9,674 points)

volzhs, can you elaborate on how it's not safe? Objects are referenced by name regularly without incidence.

I think set_name()is safe and is a solution, but it's overkill. IMO OP needs plain references for manipulation.

I presume what volzhs means is that one runs the risk of attempting to name a scene with a duplicate name, not that it’s unsafe to reference by name (he can weigh in if there are some other concerns). I don’t see the OP’s desire to reference a scene by name as overkill though, especially if he wishes to access the scene from a different script.

But not sure how to differentiate between instanced nodes

this says all.
Here's an added bonus with the method in this answer: easily handling multiple animals in a for loop.

access the scene from a different script

OTOH you indeed pointed out a nice feature of your method.

Actually, the title of the thread is "Can I give instances names?". That was what I answered. I'm not sure what your issue is...

My issue? Really? Am I not allowed to comment in Q&A unless I have issues or what's your point? Have I offended you in any way?

Michael Paul answer is correct to the original post. right.
but I pointed it could be unsafe to search by node name as like what Michael Paul said.

the risk of attempting to name a scene with a duplicate name

it will be named automatically as unique name when try to set same name under same parent node, like @snake_1@2 or something.
this can happen when you remove node by queue_free() and make another one after that.

func _ready():
    get_node("Button").connect("pressed", self, "on_pressed")

func on_pressed():
    if has_node("my sprite"):
        get_node("my sprite").queue_free()

    var sp = Sprite.new()
    sp.set_name("my sprite")
    add_child(sp)

you can see that "my sprite" is in scene at first button click on Debugger > Live Scene Tree tab
but after second click, you can see "@my sprite@2", not "my sprite"
because first one "my sprite" is not gone yet.

I just want to point it.
you should make sure and be careful to get a reference by node name.

of course you can name it "my sprite" correctly if you use free instead of queue_free, but need to be careful to use free also.

volzhs, thanks for the clarification.

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.