Legacy GM [Solved] Problem with custom Health Bar

R

ruwcom

Guest
Hello, so I been trying to set a custom health bar that is divided in a border, a green bar (for the life) and a red bar (to be under the green bar when it starts shrinking). I been trying multiple methods but nothing seems to work, right now this is the code I'm using:
Code:
/// Draw

    // Draw Health Border
    draw_sprite_ext(spr_lifebarborder, 0, 32, 32, 1, 1, image_angle, image_blend, 1);
    //Draw Redbar
    draw_sprite(spr_lifered,1,32,32);
    // Draw Healthbar
    draw_sprite_ext(spr_lifegreen, 0, 32, 32, (obj_play.hp / obj_play.maxhp) * 5,1, image_angle, image_blend, 1);
This is all in a draw GUI event in the object for the GUI, hp is the current hp that changes went the character gets hit, and maxhp is the total.

the problem that I get is that, even when the red bar and the border align perfectly, they are more small than it should and in the other hand the green bar is a lot wider than it should.

I would prefer they all to have exactly the size of the sprites that they use, and also align correctly.
even with that the bar seems to get smaller just fine, the problems are the proportions.


I hope I made my problem clear, thanks.
 

SoVes

Member
Code:
(obj_play.hp / obj_play.maxhp) * 5
if you're using xscale, you shouldn't time it by 5, the sprite will be 5x as large as the original sprite
 
R

ruwcom

Guest
Without the *5 it seems like everything is the same size, so that issue is fixed, but I still don't get how to put the green bar over the red one, and why the pixels are like two times smaller than the rest.
 

Simon Gust

Member
In your code you are drawing both bars at the same positions, which leads me to believe that the sprite origins (in the editor) arent in the same place.
 
R

ruwcom

Guest
Thanks I don't know why the red one had a really weird origin, I fixed that I played with the x and y position and everything is in place now, the only thing is that size change thing.
 

Simon Gust

Member
Thanks I don't know why the red one had a really weird origin, I fixed that I played with the x and y position and everything is in place now, the only thing is that size change thing.
Ok, I assume your healthbar you loaded as a full image.
There is a function that only draws part of a sprite called draw_sprite_part().
Code:
var mult = obj_play.hp / obj_play.maxhp;

/* mult will range from 1 (full hp) to 0 (0 hp or dead) */

// Draw Healthbar
var wdt = sprite_get_width(spr_lifegreen) * mult;
var hgt = sprite_get_height(spr_lifegreen);

draw_sprite_part(spr_lifegreen, 0, 0, 0, wdt, hgt, 32, 32);
I believe this is how it is arranged.
 
R

ruwcom

Guest
Ah yes that works perfectly! but I mean that the lifebar doesn't escalate properly to the view size, right know I have my view port to double the size of the original view, and the only thing that doesn't get bigger with the view is the lifebar. and I now is that because when I launch the project with the original view the bar fits just fine.
 

Simon Gust

Member
There is a second sorta view called GUI in game maker. It has an individual size apart from the game view.
I then assume the healthbar is drawn in the draw GUI event.

To solve this you can scale the gui the same as the view.

Here is what I use to have everything scaled up twice
Code:
view_wview = 960; 
view_hview = 540;

view_wport = 1920; 
view_hport = 1080;

display_set_gui_size(960, 540);

surface_resize(application_surface, view_wview, view_hview);
As you can probably guess, my display has a resolution of 1920x1080, so I set the view size to half that, then also the gui size.
But most importantly I resize the application surface also. This stops the game from displaying inbetween pixels and it looks perfectly scaled.
On top of that, it will reduce draw load on your graphics card because only a 1/4 of the pixels have to be submitted to the graphics pipeline or something.
 
Top