Handling Multiple Instances of the Same Enemy Effeciently?

Welcome to our brand new Clickteam Community Hub! We hope you will enjoy using the new features, which we will be further expanding in the coming months.

A few features including Passport are unavailable initially whilst we monitor stability of the new platform, we hope to bring these online very soon. Small issues will crop up following the import from our old system, including some message formatting, translation accuracy and other things.

Thank you for your patience whilst we've worked on this and we look forward to more exciting community developments soon!

Clickteam.
  • Hello, I'm having an issue in my game where I'd like to have randomly spawning enemies acting independent of each other. Normally, this isn't a problem. I can just spread a value or use the ForEach object. But in my case, I have two separate objects for the enemy: The enemy hit box, and the enemy sprite. I attach the enemy sprite to the invisible hit box as I feel it makes for better game play.

    My initial strategy was to spread values for both the sprite and the hit box's values "ID", but the problem with this came with events that came afterwards.

    It would go something like:

    ID of enemysprite = 0
    ID of enemy = 0
    > Set position of enemysprite to (0,0) enemy

    But I would have to do this for each number. At points in the game, there can be upwards of 20+ enemies on screen due to random generation. With the above method, animations in particular would be very cumbersome to add in. I figured there had to be some quicker way to do this, but everything I've tried so far hasn't worked.

    Here's what I tried:

    Always
    > Spread value 1 in ID for Enemy
    > Spread value 1 in ID for Enemysprite

    ID of "Enemy" = ID of "Enemysprite"
    > *Do animation stuff*

    This usually works briefly when an enemy is first spawned, but eventually the sprites stop following along with the enemy and the hit box goes rogue from the sprite. I end up with a bunch of spawned enemies moving independently of their sprites which should be following them. I am unsure if this is because I am constantly spreading values, but it seems to me like it starts out working and then for some reason fusion forgets that particular sprites and hit boxes are supposed to be paired together.

    I have also tried spreading values when the object and sprite are first randomly spawned. That didn't work either. I have tried ForEach as well, but that seems to only work on single objects unless I am missing something.

    The extra factor of making my sprites separate from the actual enemy hit box seems to be complicating things for me. Is there an efficient way I can handle multiple instances of the same randomly spawning enemy while maintaining sprites separate from their intended hit box?

    I appreciate any help that can be offered!

  • I recommend leaving object pairings behind and using the embedded detector method instead. Saves a lot of headaches and is, in my experience, also better when it comes to performance. Please login to see this link.

    I also posted some more information about this method here, which may help next to the tutorial. Please login to see this link.

    Please login to see this link.
    Please login to see this link. | Please login to see this link. | Please login to see this link. | Please login to see this link.

  • I recommend leaving object pairings behind and using the embedded detector method instead. Saves a lot of headaches and is, in my experience, also better when it comes to performance. Please login to see this link.

    I also posted some more information about this method here, which may help next to the tutorial. Please login to see this link.

    This is a good method, i just want to highlight that with the latest Fusion you have to click a setting in properties that makes fusion not reset the animation, "don't reset frame duration" is the name i think.

    Please login to see this link.
    Please login to see this link.|Please login to see this link.|Please login to see this link.

  • Foreachs aren't necessarily bad performing,
    for 20 enemies it should hold with no problem even on mobile runtime

    i.e.

    (a foreach on all enemies at start of frame,
    and/or right on enemy creation if they are spawned)

    >>> create enemy (hitbox)
    >>> create sprite
    >>> set my_enemy("sprite") to Fixed("enemy")

    then, whenever you need to pair:

    (say, "always")
    >>> start loop foreach enemy

    on each enemy
    +my_enemy("sprite") = Fixed("enemy")
    >>> set position at 0,0 from enemy
    >>> set animation to animation("enemy")
    etc. etc.

    add every hitbox-to-sprite "sync" action here,
    so the sprite would "display" what the enemy hitbox does,
    and you shouldn't have any other hassle and just deal with the hitbox


    If object count really becomes a problem,
    consider you could invert the problem (move the sprite, use the hitbox only for collision detection)
    and thus use only 1 hitbox you would cycle through all enemies each frame
    (eventually changing its animation/scale depending on which enemy it refers to)

    a selection of my Fusion examples can be found Please login to see this link.

  • Thanks for the responses guys! I am willing to try a new method, but first want to see if ForEach will work.

    [MENTION=18866]schrodinger[/MENTION] I can almost get ForEach to work, but something is not quite synching up. In your example, am I creating ForEach instances in two separate names? Or is it all the same ForEach on the enemy object?

    I didn't mention this earlier, but enemies in this case would be spawning in different areas at random. So essentially what I do is set a random range for a value (1-4) and depending on what value comes out, the enemy would spawn at a certain "Waypoint" of sorts. This also happens once every second. I believe this is what is causing things to not synch up correctly. That, or I misunderstood your example.

    I am getting enemies to spawn and mostly stay where they're supposed to, but sometimes the enemy itself goes rogue and abandons the sprite for some reason. Animations also seem to not be matching up properly.

  • made a very quick example:

    Please login to see this attachment.

    if you still have issues you can perhaps try to prepare a little .mfa we can check?

    a selection of my Fusion examples can be found Please login to see this link.

  • made a very quick example:

    Please login to see this attachment.

    if you still have issues you can perhaps try to prepare a little .mfa we can check?

    This worked perfectly! Thank you!

    Turns out I was misunderstanding you. I thought you were referring to the ForEach object, but your method ended up working once I saw the example. Thanks again!

  • I recommend leaving object pairings behind and using the embedded detector method instead. Saves a lot of headaches and is, in my experience, also better when it comes to performance. Please login to see this link.

    I also posted some more information about this method here, which may help next to the tutorial. Please login to see this link.

    That diybandits example is great. For a little extra bit of performance, you could probably add an extra collision detector 'animation' that encompasses both wall detectors, but not the ground detector. Then you first test to see if this "allWalls" detector overlaps anything - if not, then you don't bother testing the left and right detectors. So, whereas the diybandit method tests 3 overlaps each frame (left, right, ground), this method would test 4 overlaps during a wall collision (allWalls, left, right, ground) but only 2 overlaps at all other times (allWalls, ground). For a flying enemy (or most enemies in a bird's-eye-view game) you could also incorporate the ground detector into the "allWalls" detector, which would result in only 1 overlap check on most frames (allWalls).

    I know you've already found your preferred solution, Megabeard - just thought I'd mention this anyway in case anyone's interested.

    Please login to see this link.
    My Fusion Tools: Please login to see this link. | Please login to see this link. | Please login to see this link.

Participate now!

Don’t have an account yet? Register yourself now and be a part of our community!