Spawning + Deactivating Enemies

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!

  • Hello again folks

    I'm currently in the process of building a top down classic Zelda style dungeon crawler, and having some issues with enemies.

    I'd like it to be that when you enter a room the enemies therein will spawn, but then deactivate/be destroyed when you leave so as to not use up memory, and also any which you kill whilst in there will not respawn when you revisit the room.

    I suspect this is a pretty common query, so hopefully someone can recommend me some best practices as how to do this?

  • You could use a "room-id" alterable value for the enemies and set it to the room-id which they belong to. When you exit o re-enter a room, all the enemies with that room-id should be deactivated/reactivated accordingly.

    Similarly you could use a flag for the enemies, which indicates whenever an enemy is alive or not, and in a loop you can destroy all the enemies which are no more alive.

    You should also group the enemies with a qualifier, that way you can easily loop through all their instances.

    If you want to keep the number of enenies at the minimum, you may use the same enemies for each room ( or even create them on the fly ) and keep trace of how many active enemies a room should contain. That way, when you re-enter that room, you can reactivate ( or create ) the required amount of enemies.

    You could use different animations for the enemies accordingly with the room-id, so that when you enter in a specific room, the enemies will be shown with the right sprite.

    Makes sense ?



  • That's an interesting idea, setting a flag/variable when an enemy is deactivated, then running a loop after to deal with them, I'd previously tried doing it with a loop running on each enemy individually, which didn't seem to work (I'm a few years into learning coding and sadly scoping through loops still causes me a lot of problems).

    I've spent a few more hours working on this problem since posting this thread, and the approach I arrived at seems to work quite well, I'll try to explain it as simply as I can.

    1. I have a camera object in the centre of each screen, which the game camera is focused on. When the player leaves the room via the Top, Bottom, Left or Right doors, the camera then jumps the required number of pixels in each direction in order to centre it in the next room

    2. On top of this camera I have a 'Spawner Activator' object, which is the same size as each room. When it overlaps the enemy spawners, it triggers the loops which spawn the corresponding enemies and sets their ID value to that of the spawner, and then sets a variable in each spawner to 1 afterwards so the enemy only spawns once. The enemies themselves only exist as long as they're overlapped by the Room Spawner.

    3. If the player kills an enemy whilst it's overlapped as per the above, the enemy's ID value is then stored in another object, which is always running a loop for every spawner. If the value matches the spawner's Fixed Value, the spawner is then destroyed.

    4. If the player leaves the room without killing the enemy however, so the Room Spawner object is no longer overlapping them, the enemy is immediately destroyed and the spawner variable is reset to 0.

    There is however a bug with my approach:

    If you leave an enemy room and go into one with no enemies the above approach works fine, it'll turn the enemies on and off without trouble. If however the next room also has enemies in it, the ones in the previous room won't be switched off, probably because the Room Spawner object doesn't know which enemies it's overlapping and which it isn't?

    I've fixed this by setting a delay variable on the Room Spawner - any time it's lower than 5, it'll add 1 until 5 is reached. Every time the player leaves a room, this variable is reset to 0.

    I then tweaked all the overlapping events to that they only occur when the variable is set to 5, this way the code has a 5 frame delay to destroy any enemies which aren't being overlapped and establish which ones are, and the whole process seems to work pretty smoothly?

    It also makes me think about old games like the original Zelda and Golden Axe Warrior, how whenever you enter a new room/screen the enemies aren't present immediately, and arrive a second later in a puff of smoke - it makes me wonder if they were using the same sort of approach, driven by similar issues?

    A bit wordy I know, but I wanted to explain my process as thoroughly as possible for others who may be having a similar issue, and for more experienced coders to scrutinise and improve upon if they have any advice.

Participate now!

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