Legacy GM Spawn one instance in Step event

M

Mystic

Guest
I already have all of the code inside of the step event. It checks if a player is in range of an enemy, the gun appears in the enemy's hand and aims towards the player. What I have in the step event for this is:
Code:
if distance_to_object(obj_player_boss) < 200 {
if instance_exists(obj_shotgunner_gun) {
if distance_to_object(obj_shotgunner_gun) > 10 {
if !distance_to_object(obj_shotgunner_gun) < 2 {
instance_create(x,y,obj_shotgunner_gun)
}
}
} else {
instance_create(x,y,obj_shotgunner_gun)
}
When I run this, I expect obj_shotgunner_gun to spawn on top of the enemy (enemy not shown in code, but enemy has a step event that holds this code) when I am in a certain range (200px). Usually what happens is the gun appears on top of him and dissapears when I leave, which is what I want. But upon killing the enemy (destroying the instance), the enemy is destroyed while obj_shotgunner_gun is still there. Near the beginning of my step event (the segment that checks if the enemy's health is equal to or less than 0 (not d'n'd)), I have this.
Code:
if enemyhp<=0 {
instance_destroy(self)
audio_play_sound(snd_death,1,false)
global.gold+=20
global.kills+=1
instance_destroy(instance_nearest(x,y,obj_shotgunner_gun))
}
Even when the object is destroyed, the instance of the gun remains. How do I fix this?
Hopefully I provided enough code/context for this to be clear.
 
K

kevins_office

Guest
Destroy the gun before destroying the enemy.
How can the enemy destroy the gun when he is already destroyed first?

If putting the destroy self after the destroy gun doesn't work in the same step event then try
to put the instance_destroy(self) an in alarm and run it a step later giving time for the other code to finish.
 

Bentley

Member
while obj_shotgunner_gun is still there
You might be creating a bunch of obj_shotgunner_gun instances. Every step the "distance_to_object" conditions are true, you create one. There's probably hundreds/thousands.

Also, I think that "else" is a typo. You're creating an instance if the "if" conditions are met, and if they're not, you're creating that same instance.

I read your code kind of fast, so sorry if I'm not being very helpful.
 
Top