[SOLVED] Health Bar Visible Only Some of the Time

G

Glo

Guest
Hey, guys,

What I'm trying to get done in the game I'm working on is having a set of health bars that are only visible when you click a button. The health bars don't actually represent health, though. They represent the number of enemies in the room, so the level ends when there are too many enemies.


My question is how to keep track of the number of enemies even when the health bar instance isn't created.
I was trying to do something like this
Code:
//Enemy variable Create event
E_Coli+=2
Code:
//Health bar Step event

if
health=0
{
game_end()
}

if E_Coli+=2//This line is not functional
{
health+=2
}
The thing is that Game Maker won't allow me to write if E_Coli+=2. How can I get the health to change whenever more enemies are made?
 
G

Glo

Guest
Another thing that might be worth noting is that the health bar is destroyed at the click of a button so that it is not visible. Whatever code I use has to work even when the health bar instance is not created, if that makes sense.
Thanks, guys, for looking at this.
 

NicoDT

Member
Hi!
You can count enemies in the room with instance_count(obj_enemy_parent) (this way it will count all type of enemies, just make sure to set that object as a parent to all enemies)

//Create event
Code:
MaxHP = 10
Step Event
Code:
if instance_count(obj_enemy_parent) = MaxHP {
game_end()
}
Another thing that might be worth noting is that the health bar is destroyed at the click of a button so that it is not visible. Whatever code I use has to work even when the health bar instance is not created, if that makes sense.
Thanks, guys, for looking at this.
If what you want is to see is one bar per enemy, what you can do in the object's draw event is the following.

Code:
for (i=0 ; i< instance_count(obj_enemy_parent); i++) {
draw_sprite( spr_bar , 0 , 50+i+10 , 50)
}
with this you will draw as many bars (these bars are sprites) as enemies in the room, and separated by the amount you specify (in this case 10).


Let me know if that's what you needed.
 
G

Guest User

Guest
i think that should be 'instance_number();' instead of instance_count'.
 
G

Glo

Guest
Yeah, that code works with instance_number()!
What about getting the health bar to change color/length when the object isn't in the room all the time?
 

NicoDT

Member
Yeah, that code works with instance_number()!
What about getting the health bar to change color/length when the object isn't in the room all the time?
Ok, if you want to do the healthbar that way, just check this function in the documents "draw_healthbar"
It will be much more clear than I could explain it :).
Check it and If you have any doubt with it let me know.
 
G

Glo

Guest
I used this tutorial to help me get the bar to extend. So there's one thing finished!
However, I found out that as soon as I put in an object to destroy the enemies in the room the instance_number() code will not work.
 

Gai Daigoji

Member
you should surround the code with a check to see if the object exists first.

if object_exists(obj_youAreChecking)
{
do stuff with obj_youAreChecking;
}
 
G

Guest User

Guest
instance_exists() might work better if they are not destroying/recreating the enemy object itself, just the instances in the room.
 
Top