How do I optimize this enemy AI code?

0 favourites
  • 2 posts
From the Asset Store
Enemy turtles pack including fifteen fully animated variations
  • I'm wondering what the best way of optimizing code for a number of different AI would be.. currently using

    on EnemyFamily created [general enemy initialization code]

    on Enemy1 created [enemy specific initialization code]

    on Enemy2 created [enemy specific initialization code]

    on Enemy3 created [enemy specific initialization code]

    if Enemy1.count > 0

    [Enemy1 specific AI code]

    if Enemy2.count > 0

    [Enemy2 specific AI code]

    if Enemy3.count > 0

    [Enemy3 specific AI code]

    ... etc for 40 or so different enemies. Already using families to group most of the similar code but there's quite a few unique things to each enemy (e.g., teleportation, spell casting, etc.). Profiler tells me my enemy code is still using a good chunk of CPU time even when there are no enemies spawned. Wondering if there is a better way to segment off enemy specific code so it doesn't keep checking to run if nothing is spawned. For example, is it better to check if the enemy sprite is visible? Or have a Global variable increase if there is that type of enemy spawned?

    Thanks for any help guys!

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • It depends on what the AI actually is. When I wrote my Pacman game (see blog) - most of these games are experimental learning not actually trying to produce the game so to speak - I wrote the game so there was a single object type which represented the Pacman and Ghosts, and they had different code for deciding what to do - either code that accessed the keyboard, or code that did directional checks etc.

    The way I did this is to have a text instance variable which was an enemyType and use this to call a function indirectly with the UID as a parameter (e.g. FunctionlCall("AIFunc" & Object.enemyType,Object.UID). Functionality can then be arbitrarily applied to any of the object types (enemies in your case) by changing that instance variable.

    In something like your enemy movement where there may be minor variations but very similar AI, it might be worth have a couple of additional parameters as instance variables "modifier1" "modifier2" passed in the call, so that you can reuse AI functionality that is similar but slightly different .

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