How do I Make AI reload?

0 favourites
  • 6 posts
From the Asset Store
A sound pack containing 132 laser gun sound effects, including mono and stereo versions of audio files.
  • I've been at this for a while now, and honestly don't know what I'm doing wrong. My goal is to have enemy's runaway to their teammate and reload at the same time when they run out of ammo. This is my current code that's not working:

    + Enemy: Ammo ≤ 0

    + Enemy: Pick nearest to (Enemy.X, Enemy.Y)

    + System: Trigger once

    -> Enemy: Pathfinding: Stop

    -> Enemy: Pathfinding: Find path to (Enemy.X, Enemy.Y)

    -> Audio: Play Array.At(OpposingGun,13) not looping from Sounds at volume 10 dB at object Enemy (inner angle 360, outer angle 360, outer gain 0 dB) with tag ""

    -> System: Wait Array.At(OpposingGun,11) seconds

    -> Enemy: Set Ammo to Array.At(OpposingGun,6)

    The array is for storing the properties of the weapon, In this case the reload sound effect and reload time.

  • Never use "Trigger once" with multiple object instances, it will not work correctly! If you want to execute some code for an Enemy instance once, you should use state variables, for example create isReloading instance variable.

    There is another issue - once you picked an enemy with no ammo, you can't pick another instance (its teammate) in the same event. A common solution is to use a family. Add Enemy sprite to EnemiesFamily and then you should be able to do something like this:

    Enemy Ammo<=0
    Enemy isReloading=0
    . EnemiesFamily Pick nearest to (Enemy.X, Enemy.Y)
    .. Enemy Set isReloading to 1
    .. Enemy Pathfinding Stop
    .. Enemy Find path to (EnemiesFamily.X, EnemiesFamily.Y) 
    

    I also recommend running this code in the same event where the enemy fires a bullet. This way you will be saving resources because it will not be running on every tick, and it will only execute for a single enemy instance.

    When reloading is finished, set isReloading to 0.

    There may be other complications. For example you probably don't want two enemies with no ammo to run to each other. So you might need to move isReloading instance variable to the family level. And when picking a teammate check that it's not reloading.

    Also, the available teammate may be too far away, or there may not be a clear path to it - you should consider these things too in your code.

  • This is incredibly helpful advice, I have one question though: In the event where the code chooses an enemy to run to, how would I check to make sure the target isnt reloading?

  • Like I said, you will have to define "isReloading" variable on the family. I actually suggest doing this early for all instance variables and behaviors - move them from the Enemy sprite to the EnemyFamily. It will make your life easier in the future if you add more enemy types.

    Then you can pick EnemyFamily instances with isReloading=0, and then pick the nearest among them.

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • I've made the system you described, but there's a problem. The reload sound gets played rapidly, making a buzzing sound. I don't know how to prevent this from happening other than "Trigger once while true"

  • No, don't use "Trigger once" - it won't work correctly with multiple enemies.

    Play reloading sound only when a particular event happens, for example the enemy arrives to the reloading spot (Pathfinding On Arrived). Or when it starts the reloading animation.

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