How can I detect/count number of objects spawned in a room?

So I built an Item Spawner and it’s set to spawn items every so often…

What I want to do is detect how many of those items exist in a room/area and if its too many its game over/player killed.

I’m thinking maybe a bounds volume or something?

Can someone help me figure out how to do this or point me in the right direction? I’ve been mucking around with it all day and so far not much luck…

The closest idea I have at the moment is maybe somehow counting the objects as they spawn and if it reaches a certain set limit then I could do game over potentially?

I would make a new trigger box class and when I need to check how many items are in the volume I would check the overlapping actors.

TArray<AActor* > ItemsInArea;
    
BoxComponent->GetOverlappingActors(ItemsInArea);

return ItemsInArea.Num();

Then check the size of the array. You might want to replace AActor* with your item base class. If the objects don’t despawn and not pickup-able, you could count the objects as they spawn.

I would build it like a scoring system. Have an integer in your GameMode, increment on spawn, decrement when one of the actors is destroyed. Check the number of actors after each increment. If it’s too many, end the game.

For extra style points you could instead write a subsystem for this. Programming Subsystems Livestream

If you want to know how much spawned in generell usethe getAllActorsOfClass Node and Count the Array. If you want it in a rooms, use a boxTrace (or multiple with different Locations so you can simulate Like hallways etc)

Just a side note: GetAllActorsOfClass is slow. It works though.

I would do the way you are thinking, and count them against the limit on spawn. I’m assuming you are spawning them using SpawnActorFromClass? If so there is a return node you can add that instance that is spawned to an array and check whether it has passed the maximum allowed and if it has then run your logic for the fail state.

If you are destroying the spawning objects you can then remove them by their reference from the array as well.

This will give you all the objects spawned by that spawner, but across the whole world not based on their location. If you want to check them in a specific area, then Koraemon’s answer is the way to go. The blueprint script for that look like this.

You May Not want it in Tick. But when I understand the OP He wants it in several situations and thats fine to use it. One can also use EQS but I doubt it is faster.

Thank you for the help and suggestion

I think I kinda understand but I struggle with the coding a lot however, i think i can translate that into blueprints similarly, thank you very much

@Ruffhaus Games

Thank you so much for going the extra mile to provide me with the Blueprint Script Example. I’m a visual learner so I think that probably helps me the most right now to give me a better idea on how to proceed with it. I’m trying to work on building my first Android Game with a simple fun idea I had. I’ll be sure to mention all of you guys in the game credits as an extra thanks for all your help.Anyhow, I’ll give this a try later on today after work.

I keep fiddling around with this stuff and watching lots of youtube videos trying to figure things out and understand it all but still struggling to understand more of exactly what I am doing other than recalling it from memory after using it a lot.

Actually, I’m using a getAllActorsFromClass although I could probably do just one but I have yet to decide if I want one object or three so I might change it to just one.

Yeah thanks for confirming that, I figured out another way using the BeginPlay Node. :smiley:

That’s what I’m using now because I am unsure of if I want to spawn one object or multiple but I think I might change it to just spawning one otherwise it would probably be three objects maybe.

If I can figure out a way to just easily change the color material on it at random then I’ll probably just stick to one object.

@Ruffhaus Games

So I just created the Object Counter but it’s counting 3 at a time as 1? I think this is because I have 3 spawn points spawning 3 different objects. I decided to go with 3 different types of meshes for variance just so the game would look more interesting.

How can I correct that problem?

Also, I couldn’t seem to get the box collision to work after making the same example so I think I did something wrong or something is wrong somewhere. Is it working on your end?

So if you are doing multiple spawn points but want them all to contribute to an overall number of spawned enemies, I would have the array of spawned enemies and all subsequent logic stored somewhere else rather than the spawner itself, probably the game mode or game state, then do all the checking there. Just let the spawn points handle the actual spawning logic.

In terms of the overlapping actors/box collision, it might be to do with the collision profiles? It is hard to diagnose without seeing it.

Okay, yeah that’s kinda where I’m at now, getting really close now.

I have a Printstring that is showing the boxes being counted but its counting 3 at a time strangely

I finally got the Item Counter working after mucking around with it for about 2 hours this morning! Yay!! Thanks so much for all your help!!!