+5 votes

If I am instancing various enemies into a scene and they are being destroyed continually, I need to check the count of each type at various times.

Do I need to do this manually via variables or an array, or is there a function to get a current count of how many are in the current scene? getinstancecount() is only for the multimesh....is there something like this for normal instanced objects?

I realise that this may be tricky as each instance has a unique name. Since I am tagging them all with a set_meta ("type" , "enemy") it would be great to be able to get a count via metatags. Possible?

in Engine by (268 points)

4 Answers

+14 votes
Best answer

Add each kind of enemy subscene to 'ENEMIES' group and just:

get_tree().get_nodes_in_group("ENEMIES").size();
by (1,293 points)
selected by
Thanks, that is really nice and simple way of doing it! I have been creating so many metatags that I completely forgot about using groups.
Yeah, it's simple fast and flexible.. and when it's for scenes like 'enemy' it's usually a 'must have' since it will be useful in other scenarios also (like propagating events).

Is it possible to get an array or list of all nodes in the tree?

0 votes

Well How i personally would do it (this being from a noob mind you) i would do something along the lines of

---In a Global Script---
  func _ready():
    Globals.set("enemies", 0)
    pass

Then in the Enemy Script you could do this for example (Just an example of how it works)

---In Enemy Script---
func _ready():
    set_fixed_process(true)
    pass

func _fixed_process(delta):
    var EnemyCount = Globals.get("enemies")
    print(EnemyCount)
    if Input.is_key_pressed(KEY_1):
        Globals.set("enemies", EnemyCount + 1)

    if Input.is_key_pressed(KEY_3):
        Globals.set("enemies", EnemyCount - 1)

Hope it Helped, if not Feel free to Reply ;P mind you this is just my own personal opinion on how to do it

by (484 points)
Note, this might actually lead to race conditions, so make sure to only increment on main/physics thread.
+4 votes

You can count children from parent node with http://docs.godotengine.org/en/latest/classes/class_treeitem.html#class-treeitem-get-children
I prefer it manually since it might be faster.

by (150 points)
+3 votes

Thanks for the replies!

I have had a bit of a play, and as suspected it is WAY faster doing it manually. I have a few hundred instanced objects and it surprisingly slow to get an array of all children and traverse through it. So much so, that there is a distinct pause in the runtime when it happens.

Having said that, it could be useful if my instanced object where made children of named null groups rather than direct children of the scene root. They would in essence be a hierarchical group...anyway an idea to play with on another day :)

For those that might be interested, here is a basic code example of what alket suggested. This will count the number of instances I have tagged with the "enemy" type metatag:

    var enemy_count = 0 
    var children = get_parent().get_children()
    for i in children:
        if i.get_meta("type") == "enemy":
            enemy_count += 1

    print("enemy count: " +  str(enemy_count))   
by (268 points)

It is a very old post, but just to help others with the same doubt, it is always a good practice to use the available functions. In this case, you can use the function get_children() to get all the nodes inside a parent or specifically to get only the number of nodes attached you can use get_children().size() and you receive an int value back.

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.