• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

Remembering which enemies have been killed

M

Matt93

Guest
I've been trying to implement a checkpoint system, in which the enemies that have been killed before a checkpoint won't respawn after I've hit a checkpoint. If you don't get to the checkpoint, enemies respawn as usual. Do I need to use a ds_list to store the id of the enemies killed? Any help with how to do this would be really helpful, as I can't work out what I'm doing, or whether I have the right idea with this. I've never used ds_lists before, so if I'm on track, any advice on what to do would be great.

Thanks!
 
B

bojack29

Guest
i would make an array with different lists inside each element of the array (1d).

Then make a variable that tracks the progress of the player. Assuming there are 10 segments of a level, and 10 segments of the array, if the player progress ivariable is 6 only segments 6-10 of the array (using lists) will be used to create enemies
 

johnwo

Member
ds_list or array could come in handy.

If you spawn all the enemies at the start of the room, you could do something like this, in a "control" object of your choice:
Code:
// Room start
alarm[0] = 1;

// Alarm 0
var enemyObjIndex = obj_enemy;
var currentArrayIndex = 0;
enemy_id_array[instance_number(enemyObjIndex)] = noone;

with enemyObjIndex other.enemy_id_array[other.currentArrayIndex++] = id;

// enemy_id_array[0..n], where n is the number of enemies - 1, now holds the enemy instance id's.
Although this is not tested whatsoever, it should work with little to no modification.

Cheers!

EDIT: The reason that the code runs in alarm 0 rather than the create event or room start event, is because the enemy objects may not have been instantiated yet.
 
M

Matt93

Guest
ds_list or array could come in handy.

If you spawn all the enemies at the start of the room, you could do something like this, in a "control" object of your choice:
Code:
// Room start
alarm[0] = 1;

// Alarm 0
var enemyObjIndex = obj_enemy;
var currentArrayIndex = 0;
enemy_id_array[instance_number(enemyObjIndex)] = noone;

with enemyObjIndex other.enemy_id_array[other.currentArrayIndex++] = id;

// enemy_id_array[0..n], where n is the number of enemies - 1, now holds the enemy instance id's.
Although this is not tested whatsoever, it should work with little to no modification.

Cheers!

EDIT: The reason that the code runs in alarm 0 rather than the create event or room start event, is because the enemy objects may not have been instantiated yet.
Thank you! This code works perfectly for storing the ids of each of my enemy objects. How would I then make sure that certain enemies respawned in their x and y starting positions? I'm guessing that when I kill an enemy, the id of that specific enemy would have to be removed from the array, and that instance not created again upon room restart. I place all enemy objects in the room editor, rather than spawning them with code - if that makes a difference?
 
Last edited by a moderator:

Yal

🐧 *penguin noises*
GMC Elder
I think an easier approach would be to keep two lists of enemies - one that's the IDs of enemies that's been killed before you reached a checkpoint - e.g. the ones that are permanently gone now, and one of the enemies that's been killed SINCE the last checkpoint, and are 'volatile' at the moment. When you reach a checkpoint, put everything in the volatile list in the permanent list and then clear the volatile list; when you die, clear the volatile list and restart the room. At the start of the room, go through the permanent list and destroy all objects whose IDs are in there. It's much easier to destroy existing instances than it is to recreate gone ones.
 
M

Matt93

Guest
I think an easier approach would be to keep two lists of enemies - one that's the IDs of enemies that's been killed before you reached a checkpoint - e.g. the ones that are permanently gone now, and one of the enemies that's been killed SINCE the last checkpoint, and are 'volatile' at the moment. When you reach a checkpoint, put everything in the volatile list in the permanent list and then clear the volatile list; when you die, clear the volatile list and restart the room. At the start of the room, go through the permanent list and destroy all objects whose IDs are in there. It's much easier to destroy existing instances than it is to recreate gone ones.
Okay I'm going to try this method too now. So would I need to put the ds_lists in a controller object? Does this need to be persistent? Thank you!

Edit: Please could you give me some tips, in code, as to how to go through a list, and delete the objects whose specific IDs are in there? I'm guessing something like with (LIST) ...
 
Last edited by a moderator:
A

Aura

Guest
Simply create the list in the creation code of the very first room and assign its ID to a global variable. If you want to setup the list for the next level, delete all the entries of the list when you move onto the next level. That should do it IMO.
 

Yal

🐧 *penguin noises*
GMC Elder
Edit: Please could you give me some tips, in code, as to how to go through a list, and delete the objects whose specific IDs are in there? I'm guessing something like with (LIST) ...
Code:
for(c = 0;c < ds_list_size(global.persistent_list),c++){
  with(global.persistent_list[| c]){
    instance_destroy()
  }
}
 
A

Aura

Guest
Read the Manual entry for DS Lists to see how they work.

Code:
for (var i = ds_list_size(global.dead);i >= 0; i--)
{
   if (instance_exists(global.dead[| i]))
   {
      with (global.dead[| i])
      {
         instance_destroy();
      }
   }
}
 
M

Matt93

Guest
Thank you everyone, it's working! And thank you for teaching me more about ds_lists along the way. Cheers.
 

wamingo

Member
I'd tie the mobs' respawn and removal/deactivation to their respective spawnpoints. They have to know when to spawn anyway...

a spawnpoint object may be so:
Code:
//create
triggered=false;  // set this to true when you respawn

// END-STEP:
triggered=false;
every mob would need creation code which you may do from the room editor or when instance_create:
Code:
prevSpawn = idofspawnpoint   // triggers respawn
nextSpawn = idofspawnpoint   // triggers de-spawn

// step
if (prevSpawn.triggered){
    // respawn code
}
else if (nextSpawn.triggered){
    // de-spawn code
}
 
Top