Make multiple instances of the same enemy behave correctly?

0 favourites
  • 5 posts
From the Asset Store
Give Sound to the enemies that are part of your game! :)
  • Greetings all,

    I'm having a bit of trouble when using an enemy spawn sprite to spawn multiple instances of the same enemy. As each one is spawned every few seconds, I want each one to behave independently. choosing its own direction and amount of time to wait. I've checked through many existing topics on this forum already and have attempted those solutions, but still can't get it quite right.

    The code in question involves these Spawn sprites and these sEnemyFlyer sprites in the code below. The first small bit is the code for the spawner to spawn the sEnemyFlyer enemy. The larger bit below the black line is the AI that each sEnemyFlyer should adopt and use.

    Here's what I would expect to happen:

    1) As each Flyer is created from the spawned, it first checks the "On created > sEnemyFlyer" Event, then does all the actions in that event including settings its own instance variable for "AI" to 0.

    2) When that instance of the Flyer enemy sets its AI to 0, it then trigger the next event once, choosing a direction, waiting a random amount of time, then switching to AI =1

    3) After doing all the actions when AI = 1, it should switch back to AI = 0, resetting the animation and starting over.

    The problem here is that none of the enemies created by the spawner behave correctly: they don't appear to inherit the "On created" actions, and only travel at 0 degrees at their Bullet.Speed value, ignoring the entire AI loop. The only case where this enemy almost works is when an sEnemyFlyer is already present on the layout at the start. In other words, the only one not spawned from the spawner sprite.

    I would tend to think this is an issue of picking combined with the "For each" loop, but no matter how I configure it, I can't seem to get these enemies behaving properly and independently. If you know of any solutions, please share them. Thank you for taking the time to read my question!

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • When I'm setting up stages like this I use the variable itself to toggle on/off. I had to use them a lot with my theme park game. You don't need 'for each' or 'trigger once' here. I would set it up with the following edits:

    second event: simply AI=0, set angle of motion, set AI=1 (notice no wait)

    third event: AI=1, wait random time,(wait moved to here) do the event 3 logic

    That should work...

  • I believe the 'system trigger once' is causing your problem in the 'for each' loop. I'm thinking that will run once at the beginning of the layout and never run again because of the 'trigger once'.

    I'm not sure why you even have that separated—why can't you put those actions up with the 'on created'?

    I don't believe this is causing the problem you are referring to but be careful of using the system 'wait x seconds' in this fashion. Especially if these enemies can get destroyed. If one gets destroyed during the wait the following actions will still run but may be unpredictable. In my case it picked all instances to apply the following events—took me forever to debug. Timers are much preferred.

    Also, in the function, you don't pick any of the enemies so it most likely will pick all of them. So each time one enemy is supposed to fire all of them will. (Pass the UID of the enemy as a parameter of the function then 'pick by ID' as the first sub-event)

  • I believe the 'system trigger once' is causing your problem in the 'for each' loop. I'm thinking that will run once at the beginning of the layout and never run again because of the 'trigger once'.

    ...

    Also, in the function, you don't pick any of the enemies so it most likely will pick all of them. So each time one enemy is supposed to fire all of them will. (Pass the UID of the enemy as a parameter of the function then 'pick by ID' as the first sub-event)

    I did indeed condense those two events, but "trigger once" must be present or else when the angle of motion is picked for each instance of the enemy, it's picked every tick (the enemy sort of vibrates in place when this happens). I made that its own sub-event, though.

    As for the function part, I'm not great at intuiting function parameters, so I may have done this incorrectly. Calling the function now looks like this:

    Call function "8DirShot" (sEnemyFlyer.UID) . When the function "8DirShot" is called, Parameter 0 is (sEnemyFlyer.UID).

    The conditions when the function is called are now set to this:

    On function: "8DirShot"

    Pick sEnemyFlyer where sEnemyFlyer.UID = Function.param(0)

    For "" from 1 to 8:

    Is this closer to what you meant with your suggestions? And yes, timers are a wonderful thing, and I'm combing through and trying to replace "wait" with timers wherever possible. Thank you so much for all the help so far.

  • Yes, the function part looks correct. Is it working fine?

    If the "trigger once" part I mentioned is working fine the way you have it then good—I was just unsure what the result would be.

    One thing you could think about is adding more states—if it makes things more organized for you.

    0 = spawned

    1 = moving

    2 = charging (not moving and waiting to shoot)

    3 = firing

    4 = pause, then set back to 1

    Just a thought, but not necessary—however makes sense to you

    Tip on the timers. Use one tag, call it "state" or "AI" or something.

    On Timer Tag "state"

    -for each enemy

    --state = 0

    --else state = 1

    --etc etc

    It can make it easier if the timer needs to be interrupted (destroyed, stunned, etc.) just one timer to stop and restart or can just change the state if don't want to restart the timer. Also, I've heard you want to do a 'for each' as a sub event for the timer in case two instances' timers go off at once. It will only have those that timers go off picked, not all, but can run them separately.

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