Legacy GM [solved]easier / more compact way for this code?

J

joedirtdigger

Guest
greetings one and all.

SO I have this code and I know it will work, but I am curious if I am missing an easier or more compact way to do accomplish the task. As it stands I will have to write 100 if statements to account for all the variables possible and that is just a bit excessive imo. I was wondering if anyone had any ideas on how to accomplish the same thing with a single if statement or group of if statements designed to adjust itself via other variables.

The code is used to adjust the image index for the players HP gauge. I have a 100 slide sprite with decreasing visual indication of player hp. The code will convert the players current hp into a percentage that is then checked against the "if statement from hell" to display the correct image index based off the data given.

The reason I have chosen to do this in the way described is so that as the players HP total increases as game play progresses the hp gauge will continue to accurately show the current hp vs total hp on the hp gauge.

I am also not using the built in HP gauge as I do not like the standard hp bar my hp gauge is a half circle instead and wraps around the players ship.

so given all that info here is a snip of the current code:



///scr_cal_perc_damage(current_player_hp, total_player_hp)

hp_perc_holder = (current_player_hp / total_player_hp);
hp_percent = round(hp_perc_holder * 100);

if (hp_percent <100 && hp_percent > 98) {
draw_sprite(spr_player_hp_ring100, -1, x, y);
image_index = 1;
} else if (hp_percent < 99 && hp_percent > 97) {
draw_sprite(spr_player_hp_ring100, -1, x, y);
image_index = 2;
} else if (hp_percent < 98 && hp_percent > 96) {
draw_sprite(spr_player_hp_ring100, -1, x, y);
image_index = 3;
} else if (hp_percent < 97 && hp_percent > 95) {
draw_sprite(spr_player_hp_ring100, -1, x, y);
image_index = 4;
} else if (hp_percent < 96 && hp_percent > 94) {
draw_sprite(spr_player_hp_ring100, -1, x, y);

etc, etc, etc


thanks in advance for any replies
 
Code:
var hpRatio = current_player_hp / total_player_hp;
image_index = round(hpRatio*33);
I think something like this should work.
I use a similar method for displaying health percentages with a bar that only has nine states (each state is an image) in my game:


Code:
///scr_lifeform_update_health_bar(lifeform)

//healthbar has 9 states, 0-9 no fill to full fill

var hpRatio = argument0.hitpoints / argument0.maxHitpoints;
var barState = round(hpRatio*9);

argument0.healthBarCurrentBar = barState;
argument0.healthBarIsVisible = true;

argument0.alarm[4] = room_speed*5;
 
D

DevNorway

Guest
Code:
//Text
hp_perc_holder = (current_player_hp / total_player_hp);
hp_percent = round(hp_perc_holder * 100);

//Whatever you try to do here
draw_sprite(spr_player_hp_ring100, -1, x, y);
image_index = round(hp_percent/50);
 
J

joedirtdigger

Guest
Online Handle : just to make sure I understand your code...

looks like your math at the start is about the same as me calculating the percentage, so I get that just fine

after that I see you are using the arguement0 and from what I can tell it it being used to pull data from the targets (lifeform) object (hitpoints, maxhitpoints, healthbarcurrentbar, and healthbarisvisible)

I see that your polling the (lifeform) object for the hitpoints and max hitpoints to calculate the ratio

can I assume that the arguement0 is to adjust for any object that may need to run this script, so when passed to this script from the object is it actually passing something like "obj_lifeform"?


then it appears to be assigning data back to the(lifeform) object variables based off your math results

from what I can tell the var barstate = round(hpratio*9) is setting the number that will be passed back to the object via "healthbarcurrentbar" to be used for the image index number in the object draw event. would this be correct?
 
J

joedirtdigger

Guest
Code:
//Text
hp_perc_holder = (current_player_hp / total_player_hp);
hp_percent = round(hp_perc_holder * 100);

//Whatever you try to do here
draw_sprite(spr_player_hp_ring100, -1, x, y);
image_index = round(hp_percent/50);
looks like even less code even better!

however I'm a little confused why the last variable assignment divides the hp_percent by 50 before assigning it to image_index.
it appears to be so that the newly generated whole number value for the percentage is more in line with the image index however I've mathed a few different possibilities and for a player with max 100 hp and current 99 hp this calculation equals out to 1.98 which would round to 2 instead of 1 and technically that's not correct. unless I am reading how the round statement works wrong.
 
Online Handle : just to make sure I understand your code...

looks like your math at the start is about the same as me calculating the percentage, so I get that just fine

after that I see you are using the arguement0 and from what I can tell it it being used to pull data from the targets (lifeform) object (hitpoints, maxhitpoints, healthbarcurrentbar, and healthbarisvisible)

I see that your polling the (lifeform) object for the hitpoints and max hitpoints to calculate the ratio

can I assume that the arguement0 is to adjust for any object that may need to run this script, so when passed to this script from the object is it actually passing something like "obj_lifeform"?


then it appears to be assigning data back to the(lifeform) object variables based off your math results

from what I can tell the var barstate = round(hpratio*9) is setting the number that will be passed back to the object via "healthbarcurrentbar" to be used for the image index number in the object draw event. would this be correct?
Yup! That's exactly how it works. And the healthbar just draws itself if isVisible is set to true and its image_index is determined by the CurrentBar variable.
 
J

joedirtdigger

Guest
alright this is what I came up with

///scr_cal_perc_damage(selected_object)

hp_perc_holder = (argument0.hp / argument0.total_hp);
hp_percent = round(hp_perc_holder * 100);
percent_flip = (100 - hp_percent) //I know this is a bit archaic but it gets me the image index I want straight away.

argument0.current_hp_gauge = percent_flip;
//my draw event handles the visable/not visable state via a "is the ship selected script"
 
Top