GML Increasing Health without increasing length of bar... [SOLVED]

A

Alexander Olofsson

Guest
So basically, I've been searching around google on how to increase the health value without it increasing the length of the bar.. anyone have a solution? I haven't found anything :/
Help would be appreciated!
The code for the health bar:
draw_sprite_ext(spr_life2,1,view_xview[0]+54, view_yview[0]+700, obj_player.hp/100,1,0,c_white,1)

Also, just another quick question, is there any way to move the health bar without actually changing the x and y coordinates in the code, or like have a preview of what it's going to look like?
 
D

davoid

Guest
I just did this today.

Do you have a set length you want the bar to be?

If so, you can divide your current hp / max hp * 100 to get a percentage, then divide that by the factor you want and floor that to reduce it to the length you would like

https://forum.yoyogames.com/index.p...lth-bars-on-objects-solved.32287/#post-200067

This was my approach, see the 2nd chunk of code.

I chose to standardize the health bars to 50 pixels in width because it was then just a simple division by 2 to translate the percentage and it looked right for what I wanted it to be (small enough to not get in the way, big enough to be read at a quick glance)
 
Last edited by a moderator:

SSJCoder

Member
Initiate health:
Code:
hp_cur = 50; // change to your starting health
hp_max = hp_cur;

Increase max health:
Code:
hp_cur += 50;
hp_max += 50;

draw_hpBar (script):
Code:
// draw_hpBar (x_offset, y_offset);

var pct;
pct = obj_player.hp_cur / obj_player.hp_max;

draw_sprite_ext( spr_life2, 1, view_xview[0]+argument0, view_yview[0]+argument1, pct, 1, 0, c_white, 1 );

So to draw your health bar at the same coordinates you provided:
Code:
draw_hpBar( 54, 700 );

As you said, you want to be able to move it without changing the code,
Initiate:
Code:
hp_offx = 54;
hp_offy = 700;

Step:
Code:
if ( keyboard_check( vk_control ) && keyboard_check_pressed( vk_enter ) )
{
    hp_offx = real( get_string( "X Offset: ", string( hp_offx ) ) );
    hp_offy = real( get_string( "Y Offset: ", string( hp_offy ) ) );
}

Draw:
Code:
draw_hpBar( hp_offx, hp_offy );

During the game, press control + enter to change health bar draw location
 
A

Alexander Olofsson

Guest
Thank you so much!!
If I may ask another question, do you possibly know how to make an energy bar?
More specifically, I already have one, but do you know how to make it so it doesn't immediately regen after I do an action?
So I have to have a second for it to regen?
 
D

davoid

Guest
Like a stamina bar?

I used an alarm to say stamina/energy++ at a flag tripped by the alarm, set that alarm to be triggered on events where you use energy, and then just say if stamina/energy == staminaMax/energyMax, stop incrementing stamina/energy and flip your flag again.
 

SSJCoder

Member
do you know how to make it so it doesn't immediately regen after I do an action?
So I have to have a second for it to regen?
Sure

Initiate
Code:
// initiate idle time
idle_time = 0;

Step
Code:
// update idle time
idle_time += 1;

// regen
if ( idle_time >= room_speed )
{
    // regen health here ..
}

When you do an action which stops regeneration for 1 second
Code:
idle_time = 0;
 
Top