|
|
|
|
Reply From: |
volzhs |
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.
volzhs, can you elaborate on how it’s not safe? Objects are referenced by name regularly without incidence.
Michael Paul | 2017-11-30 02:30
I think set_name()
is safe and is a solution, but it’s overkill. IMO OP needs plain references for manipulation.
snwflk | 2017-11-30 16:16
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.
Michael Paul | 2017-11-30 17:09
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.
snwflk | 2017-11-30 17:42
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…
Michael Paul | 2017-11-30 17:52
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?
snwflk | 2017-11-30 17:57
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 | 2017-11-30 20:53
volzhs, thanks for the clarification.
Michael Paul | 2017-12-02 02:06