For each optimization

For questions about using Classic.

Post » Sat Feb 25, 2012 6:08 pm

So, I'm working on a tower defense, and I noticed I was getting some fps drop at a point in the game. After some investigation, I found out it was at the event that checks if an enemy is in range of a tower:

For each Tower
- For each Enemy
-- Distance between Enemy and Tower < range
-> Add target Enemy

So the problem here is that every tick it goes through all the enemies and towers and check the distance, and that gets really cpu intensive at some point.
Here it was approximately when the number of towers * the number of enemies was over 1000

I believe I could solve this by making the game not cycle through EVERY enemy and tower EVERY tick, but I'm not sure how to make this

Could be something like
For each Tower
- For the first 50 enemies (first tick)
-- Distance between Enemy and Tower < range
-> Add target Enemy

For each Tower
- For the second 50 enemies (second tick)
-- Distance between Enemy and Tower < range
-> Add target Enemy

And so on.
Anyone could give me a hand at this?
B
83
S
21
G
15
Posts: 1,039
Reputation: 15,134

Post » Sat Feb 25, 2012 7:53 pm

Instead, use:

For each tower
- enemy: Pick by comparison: distance(enemy.x, enemy.y, tower.x, tower.y) < range

That should be much faster. You could alternately use the turret behavior, which is even faster than that.
Moderator
B
88
S
32
G
33
Posts: 3,005
Reputation: 27,432

Post » Sat Feb 25, 2012 8:26 pm

Here is a way to handle all the enemies over 2 frames. Change "%2" to as many frames as you want to span.
global 'current'

//// This only needs to be run at the stat of the layout or when an enemy is created or destroyed.
For each Enemy
-> Enemy: set variable 'group' to loopindex %2

Always
-> set global 'current' to (global('current')+1) %2

For each Tower
- Enemy: variable 'group' equal to global('current')
-- For each Enemy
--- Distance between Enemy and Tower < range
-> Add target Enemy
R0J0hound2012-02-25 20:27:46
B
79
S
24
G
54
Posts: 4,743
Reputation: 40,749

Post » Mon Feb 27, 2012 6:28 am

Hey guys, thx for the help. But after I tested these solutions, I found out that the problem whas with the turret behavior. Turns out that when it was set to rotate, it would result in lag. I solve this problem by disabling the rotation, and creating custom firing events.
Before, with 20 towers and 20 enemies, I would get 30~40 fps, now eith 80 towers and 60 enemies I get nofps drop at all!
I`ll update this with the events later, i`m using a tablet and typing here is hard >_<

EDIT:

Alright, so, for everybody out there making heavy use of the turret behavior, this might come in handy


(the sub-events are no relevant)

Items are turrets, Enemies are targets7Soul2012-03-02 20:28:39
B
83
S
21
G
15
Posts: 1,039
Reputation: 15,134


Return to Help & Support using Construct Classic

Who is online

Users browsing this forum: Yahoo [Bot] and 7 guests