The RPG Maker Resource Kit

Other Game Creation => Program Troubleshooting => Topic started by: Grafikal on November 12, 2009, 05:38:10 PM

Title: Variables hold decimals?
Post by: Grafikal on November 12, 2009, 05:38:10 PM
I have no way of testing this right now otherwise I'd check really fast and wouldn't have to ask.
I was just rolling through the event systems and thought I'd remake my bank event system whenever I get some time, but the thought dawned on me if I would even be able to have a variable hold a number less than 1 and greater than 0, if only for an instant.

Like if I wanted to post a 5% interest on something in the system, I would generally write:

Variable001 == Money
Variable002 == 5
Variable003 == 100
Variable002 /=Variable003      (5/100)
Variable002 += 1                  (1.05)
Variable002 *= Variable001     (1.05*Money)

Does that .05 retain itself for a couple of actions then into 1.05 to do the math correctly? Or does it just round it and Money = Money instead of Money = 1.05*Money?
Title: Re: Variables hold decimals?
Post by: Cascading Dragon on November 12, 2009, 10:32:55 PM
Well I am pretty sure the standard "Control Variable" feature doesn't work with decimals, but what you could do is use something like this.

Use the "Control Variable" button with the 1st two parts, then using using a call script

$game_variables[3] = 100.00
$game_variables[2] /= $game_variables[3]
$game_variables[2] += 1
$game_variables[2] *= $game_variables[1]


As far as I know, that should work, but I don't have VX on this computer so I can't check it. Hope this works
Title: Re: Variables hold decimals?
Post by: Grafikal on November 12, 2009, 11:21:50 PM
Oh, very cool. I'll have to check this out later tonight.
Title: Re: Variables hold decimals?
Post by: albertfish on November 12, 2009, 11:30:47 PM
Or you could just do
Money *= 105
Money /= 100
Title: Re: Variables hold decimals?
Post by: Grafikal on November 12, 2009, 11:46:35 PM
It's an event system, it's supposed to be pretty easy to set up for newbies. I kind of figured using call scripts would be a little much for an event system, but i'll go with it. It's separated as 100 and 5 because 5 has to be able to be called dynamically in text messages using \v[n]. |n| will equal the interest for your account. It's just less the user has to change when they customize it. Either way, I won't be doing it like 105.
Title: Re: Variables hold decimals?
Post by: Cascading Dragon on November 13, 2009, 12:16:30 AM
Well I could write up a small script (that would be plug and play) that will allow all variables to hold decimals. Then you could include that in  your event system...though it defeats the purpose of "event system"
Its up to you, grafikal. Would you like me to try that?
Title: Re: Variables hold decimals?
Post by: Grafikal on November 13, 2009, 01:39:16 AM
if the using call scripts like how you said, then i think that should be enough. call scripts are apart of the events lol. it's not hard to explain to someone how to use the call scripts, i mean hell, the way it's set up like that you wouldn't even have to edit anything in that event command anyways, you could change the variable itself from 5 to whatever you want to be the interest. lol, thanks though man
Title: Re: Variables hold decimals?
Post by: Cascading Dragon on November 13, 2009, 01:44:21 AM
Ok then. Your welcome.
I think I am gonna do that script anyway, because it'll help me out immensely.  :D
See it on the forum....sometime. lol

EDIT:

I looked at it....and I don't think I am gonna do it now. lol. It'd take too much time, and I was hopeing it'd be quick. HAHA....thats what I get
Title: Re: Variables hold decimals?
Post by: Grafikal on November 14, 2009, 03:18:33 AM
Using call scripts doesn't work. It won't retain decimals. It rounds to 0 and thus adds 0 gold each time period. :/
I had a system to use decimals that worked, but you could only use preset rates.
Title: Re: Variables hold decimals?
Post by: Cascading Dragon on November 14, 2009, 04:27:48 PM
It doesn't?? ....let me try it now that I am home.
One question. Did you use "Add money" button and pick the variable? That might be the problem....but like I said, I am gonna try it myself.

EDIT 1:
The Change Money button doesnt work with this. However, the variables are shown right when I call them. (ex. I gave player 500, 5% would be 25. The variable showed 525 {which is right} before and after I added the money. But then I added the money and I checked my money and I only had 1000. Which doesn't add up)
So let me find another way of doing this. Hold on

EDIT 2:
I just realized that you didn't want to give the player money yet. However, when you do, use this
$game_party.gain_gold($game_variables[2])
But every thing works right as far as I can see for me. If you still have problems, shoot me your project and I'll see whats wrong. Ok?
Title: Re: Variables hold decimals?
Post by: Grafikal on November 14, 2009, 05:21:28 PM
Ah, very very nice. Check out my Bank System that I updated last night. Here: http://rmrk.net/index.php/topic,31324.0.html That will show you how I had my interest rates going. Download the project at the bottom. I did use the script commands a little bit in there as well. Where I plan to use it is in the Common Event for Interest Rates. I have it set up something like this:

Key: Variable 1 = The timer used for this common event
      Variable 2 = The interest rate, in this example, 5
      Variable 3 = Amount of gold currently in the account

Loop:
 >Wait frame(s): 600
 >Variable[0001]:Timer += 1
 >Conditional Branch: If Variable[0001] == 12
   >Call Script:
                 >$game_variables[2] += 100
                 >$game_variables[2] /= 100
                 >$game_variables[3] *= $game_variables[2]
   >Variable[0001]:Timer == 0
Loop Above
 >End

Basically what happened in there was, the interest rate, 5, added 100 to it so it become 105, then divided by 100 so it becomes 1.05. Then the current amount of gold in the account gets multiplied by that. So if you have 1000 gold, your account gold becomes 1050.

At no point does gold get added to the player. It just increases the value of another variable. If I display \v[3] in a text message, it doesn't show that account gold has increased from 1000 to 1050, it just stays at 1000. The only way the player gets money is if they withdraw money from the account, but since the variable doesn't seem to change, it doesn't update so the interest doesn't compound.

P.S. it still doesn't work, so take a look at the project i attached to this post, and let me know what you come up with.
Title: Re: Variables hold decimals?
Post by: Cascading Dragon on November 14, 2009, 10:18:35 PM
Well for one, there is a major problem with your Call Script. This
$game_variables[2] == $game_variables[4]
Should actually be this.
$game_variables[2] = $game_variables[4]
The difference is that what you had is checking if they are equal, and what I just said makes variable 2 equal variable 4.
Now thats the 1st thing I saw. I'll go through and run it again to see if it works. Be back soon.

EDIT:
Well I fixed your demo, and I am uploading it now. I added comments to your common event to show what I changed. It wasn't that bad. Here you go.
Title: Re: Variables hold decimals?
Post by: Grafikal on November 15, 2009, 02:07:46 AM
Ahhh, I seee. I'm just used to seeing the conditional branch with the double equals. lol. I'll download this and check it out real soon. Just have to close some programs and reboot in WindowsOS.
Title: Re: Variables hold decimals?
Post by: Cascading Dragon on November 15, 2009, 02:29:23 AM
Yea. Its confusing if you don't know alittle bit about programming in general. lol.
Hope it is working for you. :D
Title: Re: Variables hold decimals?
Post by: Grafikal on November 15, 2009, 03:07:37 AM
That's badass man. You're awesome, thanks for helping me out. I would never of have guessed that it was necessary to add the .00 to 100 to make 100.00 to prompt decimals. Very nice. :)
Title: Re: Variables hold decimals?
Post by: Cascading Dragon on November 15, 2009, 03:26:32 AM
You are welcome.
Haha. That confused me at 1st too. I was like, "Well my calculator can do decimals...why can't my program do it??".
But anyway, glad to help. :)
Title: Re: Variables hold decimals?
Post by: Grafikal on November 15, 2009, 09:08:54 AM
Questionnnn!

Is there some way in call script to round a value to the nearest whole number? It doesn't matter if it's rounded up or down, just to a whole number in general.
Title: Re: Variables hold decimals?
Post by: Kokowam on November 15, 2009, 01:04:18 PM
You could try using the mod variable (%) function for decimal places and if it >=5, then you add 1 to the variable and if it's <5, then you do nothing.

Just an idea. :P
Title: Re: Variables hold decimals?
Post by: Grafikal on November 15, 2009, 04:20:27 PM
Mod is for use by remainders. Like long devision. It's not a percent. Like 17/3 = 3 remainder of 2, therefore Mod 17 = 2. I actually did some research on how Mod works. It's basically useless all the time. lol
Title: Re: Variables hold decimals?
Post by: Cascading Dragon on November 15, 2009, 06:12:19 PM
Umm...well I'll have to check to see if Ruby has a round feature. I'll be back when I find it.

I never have used Mod....its very limited in its uses.
Title: Re: Variables hold decimals?
Post by: Grafikal on November 15, 2009, 06:41:48 PM
Let me know if these help, I understand what is happening, I don't know how to implement it in-game.

http://www.rpgmakervx.net/index.php?showtopic=4216 (I feel like this is very easy here)
http://www.ruby-forum.com/topic/90277
http://www.sitepoint.com/forums/showthread.php?t=646232
Title: Re: Variables hold decimals?
Post by: Grafikal on November 15, 2009, 07:02:02 PM
HOLY SHIT AWESOME. I fixed it myself!!

This is the code to round a variable to a whole number:

$game_variables[5] = 5.00
$game_variables[5] *= 0.25
$game_variables[5] = $game_variables[5].to_f
$game_variables[5] = $game_variables[5].to_i


the .to_f first converts the value to a floating number as a decimal and then the .to_i rounds it to an integer. I'm not certain as to why but you can't just do .to_i without first doing .to_f. :D :D

Source (http://www.rpgrevolution.com/forums/index.php?showtopic=18393&view=findpost&p=200610)
Title: Re: Variables hold decimals?
Post by: Cascading Dragon on November 15, 2009, 07:21:26 PM
Well thats good.
However I found two easier ways, and only one fits in the call script. Just do it
$game_variables[1]=$game_variables[1].round
Its easier and smaller. lol
Title: Re: Variables hold decimals?
Post by: Grafikal on November 15, 2009, 07:34:03 PM
Ooo, I like that, it beats having to write it so long. I'll check that out.

Edit: Oh, is it necessary to have the spaces between the variables and the = sign? looks like you don't have the spaces, i wasn't sure if it matters.
Tested, looks like it doesn't matter. cool
Title: Re: Variables hold decimals?
Post by: Cascading Dragon on November 15, 2009, 07:38:28 PM
I didn't put any spaces, but that's because it wouldn't fit on one line of the call script....which apparently it needs to. However, if it works for you with spaces, go for it
Title: Re: Variables hold decimals?
Post by: Grafikal on November 15, 2009, 07:44:58 PM
Oh, if it needs to all be on one line, then maybe it might not work because the variables I'm using are 3 digits long and not 1, so without spaces the line reads all the way across then overlaps to a new line. With spaces, it'll place one variable on a line then put the next variable on a new line below it. Either way, doing it with .to_f then .to_i worked with spaces if they were on more than one line. I'll test this further.
Title: Re: Variables hold decimals?
Post by: Cascading Dragon on November 15, 2009, 07:48:44 PM
Maybe its just my bad luck that is causing it to give me an error. I don't know.
Let me know the results, and maybe I can find a shorter way. lol
Title: Re: Variables hold decimals?
Post by: Grafikal on November 15, 2009, 08:24:11 PM
I found that .round has to be connected to the variable without being split, so having code on separate lines is fine as long as the code isn't cut in half somewhere and continued on a new line.

EDIT: I have this stupid bug now.

$game_variables[552]/=100
$game_variables[552]+=1
$game_variables[548]*=$game_variables[552]
$game_variables[548]=
$game_variables[548].round


[552] = the Loan's interest rate. At the moment, it equals 25. [552]=25
[548] = how much money you withdraw in a loan to borrow. (Example: 10000G)

So what should be happening is:
25/100 = .25
.25+1 = 1.25
10000*1.25 = 12500
(Then if it happens to be a decimal, it'll round it.)

However, what's happening is that the result remains the amount you withdraw without adding on the interest. So in this example, the result ends up staying 10000 instead of becoming 12500 like it should be. It might be caused by the .round, but I'm not sure. Does .round only round decimals, or does it round whole numbers up to like the next whole number? Like rounding 12500 to the nearest 10 thousand which remains 10000 instead of printing 12500. :/

I'll run a test quickly with the .to_f and .to_i and see if that makes a difference because as far as I'm concerned, .to_f makes it round decimals. We'll see.

EDIT2:
Well I checked it, and changing .round to .to_f and .to_i didn't make a difference, so I decided I'd remove the math. I had it there so that the player could remotely change variable[552] to fit what they wanted for an interest rate. So if [552]=25, then the interest rate would be 25% by the math that was done. However, the math isn't doing anything at all and apparently ends up equaling 1 or something, then 1*[548] would still equal [548]. There lies the problem.
Anyways, I removed the option for that variable and instead changed it to a static number, so I made it directly like:
$game_variables[548]*=1.25
Now doing this, in the end it works out just how it's supposed to, it's just when there was that math involved that it didn't work.
Can you take a look at the math above in my previous edit and see what might be the cause?
Title: Re: Variables hold decimals?
Post by: Cascading Dragon on November 15, 2009, 09:32:22 PM
Your problem is quite simple. You forgot to put ".00" after the 100.
It should look like this.
$game_variables[552]/=100[b].00[/b]
$game_variables[552]+=1
$game_variables[548]*=$game_variables[552]
$game_variables[548]=
$game_variables[548].round
Title: Re: Variables hold decimals?
Post by: Grafikal on November 15, 2009, 09:36:55 PM
OH DAMNIT! dirty double 0's. I used to love them so much. They used to be apart of my name :( now they betray me.

Well, as soon as I get those in there and re-create all of those variables I deleted and fix it up again, I'll upload the project (FINALLY) and I'll have fully functioning Banking Event System that is as easy to use as copy/paste a few common events and map events. Those decimals really cut down on a lot of work and added a LOT of easy customization.
Title: Re: Variables hold decimals?
Post by: Cascading Dragon on November 15, 2009, 09:41:31 PM
Haha. Yea. Its just a little oversight. Happens to me all the time...usually a spelling error though. Hope it works now