The Daily Click ::. Forums ::. Klik Coding Help ::. Counting objects in a variable reference
 

Post Reply  Post Oekaki 
 

Posted By Message

Airflow

imafirinmahlazr

Registered
  24/09/2003
Points
  -197

VIP MemberSonic SpeedSnow Cloud!Computer
7th September, 2005 at 00:53:40 -

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.

please help?!

 
n/a

Destroyer (CrobaSoft)



Registered
  10/10/2004
Points
  1106
7th September, 2005 at 10:34:50 -

Start of frame
---> (enemy) Spread value 0 in alterble value A

Always
---> (enemy) Set alt. Value B to sqr((X( "Player" ) - X( "Enemy" )) pow 2 + (Y( "player" ) - Y( "enemy" )) pow 2)


Always
---> set ( "counter1" ) to 0
---> Start loop "count" NObjects( "Enemy" ) Times

On loop "count"
(enemy) alterble value a = loopindex("count")
+ (enemy) alterble value b <= range
---> Add 1 to "counter1"


Image Edited by the Author.

 
Visit www.crobasoft.com or you're a Noob.

DeadmanDines

Best Article Writer

Registered
  27/04/2006
Points
  4758
8th September, 2005 at 08:13:19 -

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.

 
191 / 9999 * 7 + 191 * 7

Airflow

imafirinmahlazr

Registered
  24/09/2003
Points
  -197

VIP MemberSonic SpeedSnow Cloud!Computer
12th September, 2005 at 21:17:07 -

Hey, thanks destroyer. I'll try that out(and interpret it if I'm lucky.)

 
n/a

Airflow

imafirinmahlazr

Registered
  24/09/2003
Points
  -197

VIP MemberSonic SpeedSnow Cloud!Computer
14th September, 2005 at 01:53:33 -

Dines: your idea is good.

Destroyer, your thing didn't work. But I was wondering how you think it works...?



 
n/a

DeadmanDines

Best Article Writer

Registered
  27/04/2006
Points
  4758
14th September, 2005 at 12:10:36 -

Destroyer's method works like this:

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.

Seems like a nice way of doing it to me

 
191 / 9999 * 7 + 191 * 7
   

Post Reply



 



Advertisement

Worth A Click