Convert a numeric var into a percent var


#1

I am have not used fair math in my game for a few reason that has been discussed on other threads. However, I find I do need to use it for a display value. The problem being is I am 100K+ plus into the game code and this would be a major pain to hunt it down and place in each section of the code. What I have is maxhp set as a numeric var. I would like to do is take the number from the var and change it to a percent for a bar display during a battel scene, any ideas.


#2

Depends on what you want it to be a percentage of…

But n/100 = 1% of n.

so x / (1% of n) = how much percentage of the original n value.


#3

@CJW
Sorry this a little above my pay grade lol. That is why my code tends to be so long, I cant seem to grasp these niffty little code tricks. Lets say they have a maxhp of 12. how would I write this to display it in a bar graph percent display. Thanks


#4

Not code just math ^^

So 12 = 100%

To find 1% you do: (12 / 100) = 0.12

Finally, you get your actual health value, say 6 (should be 50% right?), and do:

6 / 1% (so 0.12) = 50! : )


To reiterate:

12 = 100%

12 / 100 = 0.12

current_health / 0.12 = current_percentage


#5

@CJW
Ok that seems easy enough so I just need to write a short script to handle the conversion. :slight_smile:


#6

Personally, I would write this as:

health_percent = ((current_health / max_health) * 100)

… Probably not in ChoiceScript syntax. It’s been a while since I’ve worked in CS.