Search code examples
javascriptgame-maker

Calculating percentage with min-max in Gamemaker


I have a healthbar that i want to use a min/max and actual number.
6 is min
8 is max
the actual number will float between them with 7 as the beginning number.
tried ((max_n + min_n) / act_n)*100, but of course that doesn't work.
i think i was sick that day when they went over that in school (eyeroll)


Solution

  • I'm not sure I understand what you're doing, but I believe you're calculating the average the wrong way. Shouldn't you have the actual value be divided by the total possible?

    Currently, your logic looks like this: (8 + 6 (14) / Actual (7) )*100, which would be 14/7 = 2 * 100 = 200.

    I think you need to reverse these two arguments, the min+max and the actual value.

    If it were me, instead of doing it that way, I would actually subtract the minimum amount from both, and then divide it by the difference between the two. If the 6 and 8 are dynamic, this may look like:

    ( (act_n - min_n) / (max_n - min_n) )*100

    Or, with values, that would be: ( (7.2 - 6) / (8 - 6)*100

    (1.2 / 2)*100 = 60, so your percentage would be 60%

    I don't use Javascript, so please ignore any spacing errors I made. I don't know how strict the language is.