Quote: (+MADcat) yay | (+MADcat) now we have 2 girls | ...
blade2k » articles & tutorials

jump to:
Tutorials » Game Creation » RPG Maker 200x

Percentage Algorithm


Reading Article
Author: stick
Posted on 08/02/2004
print :: back to list

T

here have been some incorrect posts and tutorials on various sites lately about the proper way to code a percentage algorithm. This should set the record straight.

In short, this formula will tell you what percent a number is of a number. I loose you there? I’ll reword it. This formula will tell you “ 51 is 89% of 57 .” Or, here, you algebraically challenged folks can fill in the blanks: _Y_ is _X_ percent of _Z_ . Insert any numbers you want, this formula will tell you what percent 86 is of 152, 93 is of 437, and 14 is of 91.

For those of you who told your algebra teachers your freshman year “yo, who’s gonna use this shit in real life anyway?”, I suggest you pick up a phone. You have some apologies to make.

----------------------Part 1: Declaring Variables-------------------------

Two variables are needed. For simplicity’s sake, we’ll use 0001 and 0002. If you don’t know what variables are, see my newbie tutorial on them.

We’ll name the two variables as follows

[0001: Max HP]
[0002: Current HP]

---------------------Part 2: The Algorithm---------------------

This is simple shit. The hard part is figuring out what the fuck a percentage algorithm is in the first place.

1. Set the code to change variable [0002] to the character’s current hit points.

2. Put in another piece of code to change variable [0001] to the same character’s MAX hit points.

3. Change variable [0002] to be multiplied by 100.

4. Divide variable [0002] by [0001]. (NOT [0001] by [0002], idiots)

variable [0002] should now reflect the percentage you were trying to come up with.

here’s what the actual code should look like:

<i>
<> Variable Ch:[0002: Current HP] Set, Hero HP

<> Variable Ch:[0001: Max HP] Set, Hero Max HP

<> Variable Ch:[0002: Current HP] * , 100

<> Variable Ch:[0002: Current HP] / , Var. [0001]val.
</i>

------------------------------Part 3: Using the code---------------------------

Using the code can be tricky. Whatever you do, I wouldn’t run it off a constantly-repeating cycle. I have those 4 lines of code execute when my custom menu system pulls up. Then energy bars display according to the percentage number. If [0002] is 50, then I have the energy bar display half full. I use pictures for that, but CMSs are another story. Just remember to use it only when you need it, and not have the script constantly running. If you do that you never know what number [0002] will end up being. You might even get stuck in parallel hell.