• 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!

Collision with enemy causes Draw GUI and "missing" obj_player issue

A

AtomicToilet

Guest
Howdy folks. When my player (obj_player) collides with an enemy (obj_enemy) I get this Fatal Error message and I can't for the life of me think why, because nothing happens to destroy or remove obj_player:
------------------
FATAL ERROR in
action number 1
of Draw Event
for object obj_gui:

Unable to find any instance for object index '4' name 'obj_player' at gml_Object_obj_gui_DrawEvent_1 (line 2) -

draw_sprite_ext(spr_health_fill,1,view_xview[0]+16,view_yview[0]+5,obj_player.health/100,1,0,c_white,1);
---------------------

The Draw GUI code doesn't cause issues at any other point, and likewise, I have the enemy collide with the player when he's in a different state (ie. attack) and nothing untoward happens - this error message only appears when the player collides with the enemy in his default state (or he changes back from obj_player_attack and he's still touching obj_enemy).

(Incidentally, obj_player_attack is a child of obj_player and when triggered away from an enemy doesn't cause problems)

This is the Collision with obj_enemy code (used in obj_player):

if (sprite_index = spr_player) or (sprite_index = spr_player_recharge)
{
energy -= 20;
}
else
{
other.spd = 0;
other.image_blend = c_fuchsia;
}
And, in case it's needed, the GUI code:

draw_sprite(spr_health_bckgrnd,1,view_xview[0]+15,view_yview[0]+5);
draw_sprite_ext(spr_health_fill,1,view_xview[0]+16,view_yview[0]+5,obj_player.health/100,1,0,c_white,1);
draw_sprite(spr_health_border,1,view_xview[0]+15,view_yview[0]+5);

draw_sprite(spr_energy_bckgrnd,1,view_xview[0]+5,view_yview[0]+5);
draw_sprite_ext(spr_energy_fill,1,view_xview[0]+6,view_yview[0]+5,obj_player.energy/100,1,0,c_white,1);
draw_sprite(spr_energy_border,1,view_xview[0]+5,view_yview[0]+5);
----------------------------------------

I haven't used GM in a few months so I'm a bit rusty - am I missing something really obvious? Cheers for any help!
 
A

Aura

Guest
health is a built-in global variable, so you don't need to use obj_player.health in the first place. Simply using health would suffice.
 
A

AtomicToilet

Guest
Ahhhh! Thanks for reminding me of that ;) schoolboy error haha

Okay, so now I get the same error/problem EXCEPT it's for the 'energy' variable. ie.

FATAL ERROR in
action number 1
of Draw Event
for object obj_gui:

Unable to find any instance for object index '4' name 'obj_player'
at gml_Object_obj_gui_DrawEvent_1 (line 6) - draw_sprite_ext(spr_energy_fill,1,view_xview[0]+6,view_yview[0]+5,obj_player.energy/100,1,0,c_white,1);
(I've followed the Making Games 101 Youtube vid to get the GUI code)
 
A

Aura

Guest
The fact that you're using multiple objects for the player might have been the cause. It is possible that an object that you change your player into does not have obj_player as its parent. Either way, it is super inefficient method.

I'd suggest you to use FSMs instead:

http://gmc.yoyogames.com/index.php?showtopic=460834

Also try embracing your Draw GUI code in this IF statement:

Code:
if (instance_exists(obj_player)) {
   //Draw GUI
}
...and report back what happens. If the GUI disappears then the only reason is that an instance of obj_player or one of its children does not exist at that moment.
 
A

AtomicToilet

Guest
Hi Aura - you reminded me that I used FSMs in another prototype a few months ago so I went back and copied/tweaked that code, along with your suggestion to wrap the GUI in the if statement, and now it all works like a charm :) Thanks for your advice!
 
I

icuurd12b42

Guest
player dies, destroyed, gone. check if it exists with instance_exists first
 
Top