I want to count the number of objects within a domain and range.
I figured that if I could count the number of active objects on the left hand side of a moving bar, I would also be able to count the number of objects in view of the player.
You can use conditions to narrow down which objects are selected by an event. Here's an example. Create a line of sight object for the player, which will be the area where he can see enemies.
1.Always
--Set value A of Enemy to 0
2.Enemy is overlapping Line Of Sight
--Spread 1 in value A of Enemy
In event line 2, any actions we do to the Enemy Object will only affect the ones that are within the Line Of Sight.
This also applies to the Spread Value routine. It only spreads a value within the objects overlapping the Line of Sight.
You can then run a loop through them and count the number of enemies with a value greater than 0.
1. At the start of the level, this gives each Enemy object an ID number in Alterable Value A, starting with 0 (so three objects will be numbered 0, 1 and 2).
2. This action is always changing Alterable Value B. That complicated-looking formula basically gives you the distance between that object and the player.
So if you're 400px away from an enemy, that enemy will have a value of '400' in value B.
3. After that, we have a counter, which we reset to 0. Then we start a loop to run for each of the enemy objects (so if there are 5 enemies, it will run the loop 5 times)
4. Each time it loops, it will select the next enemy (by his ID number). If his value B (that enemy's distance from the player) is lower than a certain value (the range we're checking), then it adds 1 to the counter.
It will then move on to the next enemy, and so on. This way, it checks the distance of each enemy from the player and only counts those that are within a certain range.