0 Favourites

For each optimization

This forum is currently in read-only mode.
  • 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?

  • 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.

  • 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

  • Construct 3

    Buy Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Buy Now Construct 3 users don't see these ads
  • 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

    <img src="http://i.imgur.com/Ove6m.png" border="0" />

    (the sub-events are no relevant)

    Items are turrets, Enemies are targets

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)