Charas-Project

Game Creation => Requests => Tutorials => Topic started by: Momeka on November 09, 2019, 12:38:21 PM

Title: Percentages in RPG Maker 2003
Post by: Momeka on November 09, 2019, 12:38:21 PM
(Here is a Google Docs version of the tutorial. It's a bit better formatted and might be easier to read) (https://docs.google.com/document/d/13R_ezM_oy1U9S0czPpEbxDkJb1ciAy6NimNHR6BU77k/edit?usp=sharing)

Percentages in RPG Maker 2003

While percentages isn’t something that usually comes up when making a game in RPG Maker 2003 it does have it uses. Especially when you’re making custom systems and menus. Like in a healthbar to display a character’s HP or maybe a skill that scales its damage depending on the users remaining health.
Since I’m forgetful and math isn’t my strong suit I keep forgetting how it works and end up having to look it up again and again. So I figured it would be easier to write a little guide that I can go back to and it might help others as well.

There is links to a demo project at the bottom. The demo will showcase the examples used in the guide. So you can look at all the events yourself.

So let’s get started.

Converting values to percentages

Usually when you want to get the percentage of something you would divide it by the max value. Like 45 / 75 results in 0.6 which would be 60%. This doesn’t work in RPG Maker 2003 since it doesn’t support decimal numbers (also known as floating points). So we are stuck working with regular integers. That means when we’re calculating the percentage we want to get back a number from 0 to 100 instead of a 0.0 - 1.0.
To achieve this we can use this formula:
result = (100*current + max/2)/max
With 45 out of 75 it would turn into this:
result = (100*45 + 75/2)/75
result = (4500 + 37)/75
result = 4537/75
result = 60

But how do we get it into RPG Maker 2003? Well, let’s start by creating a common event and we’ll take it step by step from there.
(https://i.imgur.com/vB112EC.png)
Name it to something like “Get %” and set the trigger to “None”.

Secondly we’ll need 4 variables. So let’s start with creating them:
% Result - This variable will contain the result of our calculations. The actual percentage so this is the variable you’ll want to use to set your Healthbar or whatever it is you need the percentage for.
% Max Value - This variable stores the max value of the number we want to get the percentage of. This would need to be set before you call the Get % common event.
% Current Value - This variable stores the current value of the number we want to get the percentage of. This would also need to be set before you call the Get % common event.
% Temporary - This is a variable that is only used during the calculation in the common event. You won’t need to set it before nor use it afterwards.

Your variable window should look something like this:
(https://i.imgur.com/GPk4gkl.png)

Now that we have all the variables we can start implementing the formula in the common event. We’ll start with this part of the formula:
result = (100*current + max/2)/max
We’ll do this by setting the % Temporary  variable to the % Max Value variable. After that we divide it by 2. Simple enough, you should see something like this:
(https://i.imgur.com/v7gsbai.png)

Now let move on to this part:
result = (100*current + max/2)/max
Start by setting the % Result variable to the % Current Value variable. Then we multiply the % Result variable by 100. So now you should have this:
(https://i.imgur.com/PJAoxXQ.png)

Now just add the % Temporary to the % Result variable.
(https://i.imgur.com/5AFU7cP.png)

Finally just divide the % Result variable with the % Max Value variable.
(https://i.imgur.com/GbFCA5H.png)

With that the common event is complete. So let me show how it would be used in an event. Say our heroic hero has been injured or just healed and we need to update their healthbar, then we would do something this:
(https://i.imgur.com/hlnWAdV.png)

In this case 333 would be our hero's max hp so we set the % Max Value variable to that. And we set the % Current Value to be equal another variable, in this case that variable would store the hero's current hp.
Then we simply call the common event we just made to calculate the percentage. Since the spritesheet index always start at 1 and not 0 we need to add a 1 to the result.
(https://i.imgur.com/pvptE5z.gif)

But what if our healthbar isn’t from 0 to 100, what if it’s from 0 to 34? Well then let’s move on to the next part.

Converting percentages to values

To convert a percentage to a number we would use this formula:
result = (percent * (max - min) / 100) + min
In the case of 60% out of 75 it would be like this:
result = (60 * (75 - 0) / 100) + 0
result = 60 * 75 / 100
result = 4500 / 100
result = 45
Now you might think it’s weird that there is a bunch of zeros in the formula. That cause this formula allows us to convert from more value ranges than zero to some higher value. We could do 23 to 434 if we wanted or whatever else.
If all we wanted was values that start from zeros we could use this formula instead:
result = (percent * max) / 100
Which is simpler to implement but we won’t go through it in this tutorial.

We will need another Common Event so let's make one. Name it to something like Map %. As before the trigger should be set to “None
(https://i.imgur.com/t9Qy5Xu.png)

We’ll also need an additional 4 variables. So let’s go ahead and make them:
Get Result - This variable will contain the result of our calculations. This is the value you would use after you called the common event.
Get Max Value - This is the maximum value of the range we want to get the percentage. This would need to be set before you call the event.
Get Min Value - This is the minimum value of the range we want to get the percentage. This too would need to be set before you call the event.
Get % - This is the percentage to use. 0 would be the minimum value, 50 would be half and 200 would be double the max value etc. This will also need to be set before you call the event.

Now your variable window should look like this:
(https://i.imgur.com/YPzFXQU.png)

With that done let’s move on to implementing the formula. We’ll start with this part:
result = (percent * (max - min) / 100) + min

We do this by setting the Get Result variable to be equal to the Get Max Value variable. The we subtract Get Min Value from it.
(https://i.imgur.com/xUH77FU.png)

Next let’s do this whole part:
result = (percent * (Get Result) / 100) + min
Start by multiplying Get Result variable with the Get % variable. Then simply divide Get Result with 100.
(https://i.imgur.com/mhDLhsz.png)

The final step is just to add Get Min Value to the Get Result variable and we’re done!
(https://i.imgur.com/h3pTLRY.png)

Now lets see it used in action. In this case we have a space ship that over the duration of 2 seconds it should move to a position and scale up. Now this could easily be done with a Move Picture event but maybe we want to be able to pause and resume the movement or even let the player control where along the path the ship should be. To do this we have a parallel process event on a map. It’s set up like this:
(https://i.imgur.com/w3ywdeV.png)

The Current Frame variable is where along the path the ship is. It’s essentially just a timer ticking up. The % Max Value is set to 120, which would be 2 seconds. RPG Maker 2003 runs at 60 frames per seconds, meaning the event will update 60 times each seconds. Thus 120 times for 2 seconds.
With the Current Frame and % Max Value set we can get the percentage of the path traveled by using our Get % common event we made before. With a percentage we can map the ships positions and size with our new Map % event. Just set the min and max value of each variable.
Then we simply draw the ship as an image using the X, Y, and Size variables we got back.
(https://i.imgur.com/sMsMZtD.gif)

Demo Project

The demo project contains the two examples used above.
You download it from here:
[Google Drive] (https://drive.google.com/file/d/1VzwV4PehKpFaeXdQ26RdfO2UxjCCgegd/view?usp=sharing)

Title: Re: Percentages in RPG Maker 2003
Post by: Momeka on November 09, 2019, 12:39:02 PM
My preorder of Death Stranding didn't arrive and I was bored.
Title: Re: Percentages in RPG Maker 2003
Post by: Prpl_Mage on November 10, 2019, 11:38:43 AM
My preorder of Death Stranding didn't arrive and I was bored.

I was thinking about preordering as well. From what I get from the reviews it usually recieves top scores but people wouldn't really recommend it to just anyone.

Also like the work on this, much more useful than the workarounds I've done in the past that have ended up being slightly inaccurate.
Title: Re: Percentages in RPG Maker 2003
Post by: Momeka on November 10, 2019, 03:52:54 PM
I was thinking about preordering as well. From what I get from the reviews it usually recieves top scores but people wouldn't really recommend it to just anyone.

Yeah, been trying to avoid trailes and reviews. I want to go in as blind as possible. But heard people are having mixed feelings about it.
Title: Re: Percentages in RPG Maker 2003
Post by: Moosetroop11 on November 12, 2019, 08:59:18 AM
Interesting stuff. I can think of several places in the past where I could/should have used something like this rather than the hacky way I've done bars for things...
Title: Re: Percentages in RPG Maker 2003
Post by: Zoltar on November 12, 2019, 05:09:25 PM
I can't understand how to extract those files. Mondo confusing. it takes me t these gnarly looking sites with all sorts of bad links...Am I the only one?
Title: Re: Percentages in RPG Maker 2003
Post by: Donut on November 13, 2019, 02:55:08 PM
Very useful tutorial ! Thanks :)
Title: Re: Percentages in RPG Maker 2003
Post by: Momeka on November 16, 2019, 05:50:51 PM
I can't understand how to extract those files. Mondo confusing. it takes me t these gnarly looking sites with all sorts of bad links...Am I the only one?

It's Google Drive... there is a download button in the top right corner.
Title: Re: Percentages in RPG Maker 2003
Post by: Fisherson on November 17, 2019, 07:11:48 PM
Really this should be built into a maker without needing a code source or pluggin. Red, you are a clever bastard and let nobody tell you otherwise. ^_-