0 votes

I have 5 preloaded scenes and I have generate 3 instances of each.
I'm trying to get the 3 instances for one of them by the name but I get only one.

func check_inst():
    var scene_array = []
    var red_inst=[]
    for item in self.get_children():
        if item.get_class() == "Sprite":
            scene_array.append(item)
    print(scene_array)
    for i in range(scene_array.size()):
        if scene_array[i].name == "RedCircle":
            red_inst.append(scene_array[i])
    print(red_inst)

The scenearray outputs all 15 instances, but the redinst prints only one of the three. Am I doing something wrong?

If I print an array with all instances are 15, so in the array

in Engine by (219 points)
edited by

1 Answer

+1 vote
Best answer

Name is unique variable for every instance. First name is Sprite, but next ones get names Sprite02 and Sprite03. Instead You can use

is_class("Sprite") == true.

For scripts You can use class_name, for custom scenes - filename.

by (2,412 points)
selected by

Thanks, I thought about this too, so I tried also to use "RedCircle1" before post here which didn't work.
Now I tried with 02 but again I get an empty array. The name of the sprite in scene is RedCircle. If I check with this name I get an 1 item array. If I check about RedCircle01 or RedCircle1 I get an empty array

So what is red circle ? Is it a scene You created based on Sprite ? If so, than all instances of this scene will share common FILENAME, at least if You loaded them from code. FILENAME is a string containing resource path, so maybe You can use it for checking instead of NAME.

If You insist on calling NAME, then You can test what exactly are those names spelled with print(), or simply running your project and viewing nodes in remote tab. Usually they contain some @sign.

RedCircle is a scene which contains a sprite with name RedCircle, the same the other 4 scenes.
I'll try without using name.

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.