Main Menu
  • Welcome to The RPG Maker Resource Kit.

Variables hold decimals?

Started by Grafikal, November 12, 2009, 05:38:10 PM

0 Members and 2 Guests are viewing this topic.

Grafikal

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?

Cascading Dragon

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

Grafikal

Oh, very cool. I'll have to check this out later tonight.

albertfish

Or you could just do
Money *= 105
Money /= 100

Grafikal

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.

Cascading Dragon

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?

Grafikal

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

Cascading Dragon

#7
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

Grafikal

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.

Cascading Dragon

#9
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?

Grafikal

#10
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.

Cascading Dragon

#11
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.

Grafikal

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.

Cascading Dragon

Yea. Its confusing if you don't know alittle bit about programming in general. lol.
Hope it is working for you. :D

Grafikal

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. :)

Cascading Dragon

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. :)

Grafikal

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.

Kokowam

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

Grafikal

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

Cascading Dragon

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.

Grafikal

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

Grafikal

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

Cascading Dragon

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

Grafikal

#23
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

Cascading Dragon

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