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

Legacy GM [Solved] Health bar that works with different instances of the same obj?

R

ruwcom

Guest
So I made a health bar object for the enemies of my project, when the enemy gets hit it creates a health bar in the room and it shows how their health points decrease while receiving damage, checking that info from the create event of the enemy.
The problem is that, when I have multiple enemies on the view, the bar only keeps track of one random enemy, so when I hit other enemy the bar creates itself but it's always full, because the enemy that it's getting the information from isn't receiving any damage. I need to kill the enemy that works with the bar so it can track the HP of another enemy, but then the problem is the same.
My question is, is there a way to create a bar that keeps track of the HP of the enemy that created it? also my intention is to have only one enemy health bar showing at the time.

The code to create the bar
Code:
if (place_meeting(x,y,obj_hitbox)) && (damage = false)
{
    instance_create(x,y,obj_enbar);
}
And the code of the bar itself, if that helps
Code:
///the background of the bar
draw_sprite_ext(spr_enemylifered, 0, 129, 52, 1, 1, image_angle, image_blend, 1);
///the green bar that gets smaller with the damage
 draw_sprite_ext(spr_enemylifegreen, 0, 130, 53, (obj_enemy.hp / obj_enemy.maxhp),1, image_angle, image_blend, 1);
 
V

van0014

Guest
Use instance IDs to keep track of each instance

Code:
//Instance ID variable. Also set this in the HP bar create object. It gets overridden below
hpid = 0;
If (place_meeting(x,y,obj_hitbox)) && (damage = false)  {
//have only the most recent bar at a time
with (obj_enbar){instance_destroy();}
hpobj = instance_create(x,y,obj_enbar);
hpobj.hpid = id;
}
Code:
///the background of the bar
draw_sprite_ext(spr_enemylifered, 0, 129, 52, 1, 1, image_angle, image_blend, 1);
///the green bar that gets smaller with the damage
 draw_sprite_ext(spr_enemylifegreen, 0, 130, 53, (hpid.hp / hpid.maxhp),1, image_angle, image_blend, 1);
Another way is to have a hp bar that is always there. It would have hpid = 0 to start with, so it should be invisible if the object doesn't exist. Then on every hit, the ID can change to the object that gets hit. Use (hpid).x and (hpid).y to get position if it needs to move. You could have a timer as a cooldown if you wanted the bar to eveentually hide.
 
Last edited by a moderator:
R

ruwcom

Guest
I been thinking about something like that but how can I put that id into to the code of the bar so it only checks the object with the same id as the id assigned? I never really worked with something like that.
 

FrostyCat

Redemption Seeker
Learn the difference between an object and an instance and read the edited post from van0014.

First he put an instance variable named hpid into the created obj_enbar instance, then the obj_enbar instance uses that to reference its creator's hp and maxhp. Where you used obj_enemy.hp and obj_enemy.maxhp and violated the "no object.variable with multiple instances" rule described in my article, he instead used hpid.hp and hpid.maxhp. hpid is an instance ID and points to a specific instance. obj_enemy is an object ID and can't serve the same purpose.

The next time you need to work with a specific instance out of many, get the instance ID into a variable and use that as the subject. This dumb "objects must be on the left side of a variable reference dot" myth is something that I've been trying to put to death for over a decade, and I'm disheartened that it still has believers.
 

Fabseven

Member
For me it's really simple ...

in obj_monster or obj_whatever
draw event
Code:
if( hp < hpmax)
{
   draw_healthbar(...)
}
if your code is in the draw event of an obj var are instance-scope
 
Top