Windows I need help with enemy spawning

T

TorbCraft

Guest
I want to make a spawn systems that makes it to where, when a enemy dies, a timer starts ( lets say 10 seconds ) then the enemy will respawn. I couldnt figure it out, all the tutorials I could find on it is when ALL enemies die they respawn. But I want a respawn system sorta like World of Warcraft where when they die, the timer starts. Any help is good. Thanks :D
 
A

Aura

Guest
That could be done in a number of ways. Creating a temporary instance and using that to respawn the enemy is the most versatile and the easiest method. Create a new object, call it obj_TempEnemy or something.

When the enemy instance is destroyed, create an instance of this object. You could use the Destroy event for that. Using a with statement, assign a variable to object_index of the calling enemy instance. You could do that by using the other keyword which refers to the calling instance inside a with statement. For example, if you put this code in the Destroy event of obj_Enemy1, then other.object_index would return obj_Enemy1.

Code:
with (instance_create(x, y, obj_TempEnemy)) {
   alarm[0] = 10 * room_speed;
   my_obj = other.object_index;
}
Then using the Alarm 0 event of obj_TempEnemy, you could do this:

Code:
instance_create(x, y, my_obj);
instance_destroy();
What the above code does is that it creates an instance of the object that it was created by, then destroys itself, for it is no longer needed.
 
Or you could simply draw an effect for example, set your score variables etc, start the timer and deactivate the enemy instance.
And when the timer is at 0 you could activate the instance, set x & y to start positions, set the HP to *standard HP / full HP* and so on.
 

TheouAegis

Member
Don't destroy instances. That's the easiest way. Remove the sprite_index, set a variable to prevent it from running any further code or interacting with anything. Count down an alarm that will move it back to (xstart,ystart) and assign its sprite.

People are too eager to just destroy instances left and right willy-nilly these days. I blame the old tutorials.
 
T

TorbCraft

Guest
Or you could simply draw an effect for example, set your score variables etc, start the timer and deactivate the enemy instance.
And when the timer is at 0 you could activate the instance, set x & y to start positions, set the HP to *standard HP / full HP* and so on.
How would I do that? And wouldnt his hit box still be activated so the player couldnt walk that way? Im sorry Im making this a big deal but in the long run I will know more and the game Im making will be better :)
Don't destroy instances. That's the easiest way. Remove the sprite_index, set a variable to prevent it from running any further code or interacting with anything. Count down an alarm that will move it back to (xstart,ystart) and assign its sprite.

People are too eager to just destroy instances left and right willy-nilly these days. I blame the old tutorials.
Well Ive been watching HeartBeast and my code is still set to what his is so Im trying to change it the way I want.


Thanks for all the help so far guys :D
 

TheouAegis

Member
There can be no Collision if the enemy has no sprite. So if sprite_index is -1, there can be no collisions.

I'm not familiar with anybody's tutorials, although I am vaguely familiar with Shaun Spalding's tutorials.If the tutorial uses collision events, you will need to remove the sprite; but if collisions are handled by code like place_meeting or collision_rectangle, then you can just make the enemy invisible and only check for collisions if it is visible.
 
How would I do that? And wouldnt his hit box still be activated so the player couldnt walk that way? Im sorry Im making this a big deal but in the long run I will know more and the game Im making will be better :)
Uhm actually I didn't think about the collision there and I'm not quite sure whether the collision works or not while disabled. But i actually think it won't collide. You can try it out with a disabled test instance.
 
T

TorbCraft

Guest
There can be no Collision if the enemy has no sprite. So if sprite_index is -1, there can be no collisions.

I'm not familiar with anybody's tutorials, although I am vaguely familiar with Shaun Spalding's tutorials.If the tutorial uses collision events, you will need to remove the sprite; but if collisions are handled by code like place_meeting or collision_rectangle, then you can just make the enemy invisible and only check for collisions if it is visible.
So I set sprite_index = -1; when the health of the enemy = 0. The enemy goes invisible but still has collision and does damage. Also, Im still new to gamemaker so I dont quite get what Aura meant with the 10 second alarm. If someone can, can the show me the exact code I would use and explain how it works. Thanks :) ( sorry if im asking too much ) Im not quite sure how to set alarms and make it return a value... [EDIT] Nevermind, I used Auras coding. It works :D Thanks alot for all the help everyone :)
 
Last edited by a moderator:

TheouAegis

Member
You shouldn't be able to collide with it at all. I'm curious what your player-enemy collision code is, because I just tested setting sprite_index to -1 (or -sprite_index if the sprite isn't the very first one in the resource tree) and my player passed right through it just fine.
 
T

TorbCraft

Guest
You shouldn't be able to collide with it at all. I'm curious what your player-enemy collision code is, because I just tested setting sprite_index to -1 (or -sprite_index if the sprite isn't the very first one in the resource tree) and my player passed right through it just fine.
/// Damage the player
if (state != scr_enemy_stall_state) {
var dir = point_direction(other.x, other.y, x, y);
var xdir = lengthdir_x(1, dir);
var ydir = lengthdir_y(1, dir);
var damage = instance_create(other.x+xdir, other.y+ydir, obj_damage);
damage.creator = id;
damage.knockback = 80;
state = scr_enemy_stall_state;
alarm[1] = room_speed;
}


This is whats in my obj_enemy_slime collision event.
 

TheouAegis

Member
Weird. Which version GM are you running?

I made an object. I put in its collision with player event:
show_message("HI")

I run into the object. Message pops up.

I put in the object's Press Space event:
if sprite_index > -1 sprite_index = -1;

I pressed space then moved toward the object. The player passed right over it with no messages.
 
T

TorbCraft

Guest
Weird. Which version GM are you running?

I made an object. I put in its collision with player event:
show_message("HI")

I run into the object. Message pops up.

I put in the object's Press Space event:
if sprite_index > -1 sprite_index = -1;

I pressed space then moved toward the object. The player passed right over it with no messages.
Sorry I havent replied... Been busy. Im running Gamemaker Studio 1.4 and the reason why my coding is like that is because im trying to make a 2d RPG game.
 

TheouAegis

Member
Well my point is if you set the Sprite index to -1, or any negative number really, then there will be no Collision detection at all. If you are getting Collision results, then there has to be something else going on in your code that is simulating a collision when there actually isn't one.
 
Top