GameMaker Respawning enemies and traps (outside of view_camera)

L

L1NDHOLM

Guest
What would be the best way to make objects (like enemies and traps) to respawn at their original position if destroyed/left behind AND left outside the view_camera? (like old school NES games).

My current code works like it deactivates everything and activates everything within my view_camera, like this:
Code:
inst_lay_id = layer_get_id("Instances");
instance_deactivate_layer(inst_lay_id);

var cam, cleft, ctop, cw, ch;
cam = view_camera[0];
cleft = camera_get_view_x(cam);
ctop = camera_get_view_y(cam);
cw = camera_get_view_width(cam);
ch = camera_get_view_height(cam);

instance_activate_region(cleft, ctop, cw, ch, true);
But this code is not sufficient anymore.

I'm thinking that all remaining enemies and traps need to be destroyed when they get outside my view_camera view and then somehow respawned and deactivated till i walk back to their original position where they get activated.

Is there a better way? How would i do this?
 

Relic

Member
That sounds fine.

In the create event of the enemies, have them store their original x and y position.

When outside the view, have them create a new instance of themselves at their original location and destroy the original. You can still activate/deactivate outside the view if you need but make sure this happens prior to any deactivation.
 
L

L1NDHOLM

Guest
Okey, thanks. But what do you think i should do with the enemies that i kill? I'm guessing that i can't use instance_destroy on them anymore?
 
M

Micle

Guest
You could always have an object that will take care of respawning enemies that you always keep activated, so it contains the coords for all spawn locations, spawns the enemy and then if the enemy is out of view it'll be deactivated by default. You can also give each enemy an ID that will represent a respawn location, so whenever it dies, the controller object will be able to spawn another enemy at a certain location ( to make a limit for each respawn coord )
 
L

L1NDHOLM

Guest
It seems really complicated, i don't think i have the skill that to work.
Is there a way to get a more focused room_restart so it only restarts a specific layer in my room? Then i could put all enemies and traps there.
 

Relic

Member
You could continue to use instance_destroy to kill enemies. Don’t make the “return me to original location” code a part of the destroy event. Make it a part of the “am I inside the view?” check of the step event. Or there may even be an event called “outside view”- I forget.

If there are other things happening in the destroy event like sound effects and increasing scores then you will want to disable these when destroying because outside the view.

Create event:
view_destroy=false
Orig_x=x
Orig_y=y

Step event:
If outside view {
view_destroy=true
Create instance at orig_x, orig_y
instance_destroy()
}

Destroy event:
If not view_destroy {
Do all your death animations/sounds/score updates
}
//otherwise do nothing

Sorry for poor pseudo code, on phone
 
L

L1NDHOLM

Guest
I kind of solved this.
Instead of killing the enemies they turn invisible (visible = false) and collision with obj_player/obj_bullets is turned off when their hp <= 0. Now when they get outside of our view they go back to their start position, get the right starting direction, get their hp back to max and turn visible. After all this they get deactivated until they get back in my view again.

Is it horribly weird to do it this way?
 
M

Micle

Guest
I kind of solved this.
Instead of killing the enemies they turn invisible (visible = false) and collision with obj_player/obj_bullets is turned off when their hp <= 0. Now when they get outside of our view they go back to their start position, get the right starting direction, get their hp back to max and turn visible. After all this they get deactivated until they get back in my view again.

Is it horribly weird to do it this way?
I wouldn't say it's horribly weird but it could definitely cause some performance issues in the future. However if it works fine, you're getting good performance and it's not causing anymore issues then it's fine. In the end I wouldn't say it's the best way, there's definitely better ways but sure, all that matters is you getting it to work well and reliably.
 

Relic

Member
Yeah, whatever works, works. You need to be aware of the limitations such as if you count up enemies on the screen, have to check if hp is less than zero.

Just be aware the instances are there and collisions might not be the only thing an invisible enemy can influence in the game.
 
Top