Game Object indexing from Collections

Is it possible to get a count of instances of a specific game object in code? Would be usefull for scoring such as enemies killed, coins collected and such. I could do the could manually but it would help to keep numbers accurate.

No, I’m afraid that is not something we expose in an API. You have to do the book-keeping yourself.

Coins collected should really easy. You’re already detecting when the player collides with coins right and at that time you remove the coin. Update a counter then and there.

For enemies killed I assume it’s something similar. When you detect that an enemy is to be destroyed you update a counter or pass a message to something that can increase a counter.

1 Like

Would it be possible to simply post a message from the init of the script if the object that needs counting? You could send it to a counting script. That would be a very simple way of achieving what you need. You could include another message in the final part of the script.

1 Like

Thanks for the reply. I wanted the game to automatically know how much coins for example was instanced in the level then do the count that you mention the after the player completes the level check how much coin were collected versus how much coins were in the level initailly. Guess I gotta do the book keeping myself.

Perfect I never thought of that. Thanks.

That’s an acceptable solution in some cases, but if you need to count a large amount of instances it would mean one script per instance and there’s a 1024 cap on the number of scripts.

I think in my case a have a levelinfo script attached to my level game object(currently tracking what level is currently loaded and tilemap info, another problem I have but leaving for a later date) and each instance of a coin for example would pass a coin count message increment to a total_coins_count variable in the levelinfo script. Keep in mind the total number is temporary for each level loaded, but the achievement for callecting all coins would be saved.(1 of 3 stars for example)

Anyway would be cool to somehow get a count in the future directly such as;

collection.get_instance_count(“game object directory”)

that would return a single variable
i.e
local total_coins = collection.get_instance_count (“gameobjects.collectables.coin”)

something like that

2 Likes

+1