Force variable to give integer in dialogue

Kes

Veteran
Veteran
Joined
Aug 3, 2012
Messages
22,853
Reaction score
12,388
First Language
English
Primarily Uses
RMMZ
I have a text message for the player to tell them that they have received a small reward. The amount of that reward is obtained by dividing a number. That, obviously, doesn't always result in an integer and so the text will say e.g. You have received a bonus of 13.5 EXP. That is also the amount shown in the Status menu. I think this looks ridiculous.

I have tried using Mod instead of Div but that gave double the amount it should. (I fried my little brain trying to work out how the heck that could happen. I thought the risk of using it would be that it only rounded down, not that it would, in a fit of generosity, hand out twice the amount). What I would like to do is for the variable to round up/down when the result is shown in the text box.

Could anyone tell me how I might do this.

Thank you.
 

Andar

Veteran
Veteran
Joined
Mar 5, 2013
Messages
36,693
Reaction score
9,863
First Language
German
Primarily Uses
RMMV
Modulo operation gives the undivisable rest after the division - 9 mod 7 results in 2 because 9 fits seven one time, leaving two.

you can convert any number to interger after an operation to prevent that.
if I remember correctly the command is either to_i or trunc, but it has been too long since I last programmed to remember what is correct for javascript.
 

Kes

Veteran
Veteran
Joined
Aug 3, 2012
Messages
22,853
Reaction score
12,388
First Language
English
Primarily Uses
RMMZ
@Andar Thanks for explaining why Mod doesn't work.

I doubt that the command is to_i as that is the Ruby command, so I assume it's trunc. However, I've been staring at the commands and cannot see where/how to add that to the calculation. Could you explain a little more?
 

Kes

Veteran
Veteran
Joined
Aug 3, 2012
Messages
22,853
Reaction score
12,388
First Language
English
Primarily Uses
RMMZ
@Andar Once again thank you. I haven't yet worked out exactly how to do that in the editor, but looking at the article you linked to, it results only in rounding down, never rounding up, so I'm not sure that works in this instance. The old to_i rounded both up and down, but not this version.
 

Andar

Veteran
Veteran
Joined
Mar 5, 2013
Messages
36,693
Reaction score
9,863
First Language
German
Primarily Uses
RMMV
the article I linked also gives the other versions of the command for the different types of rounding.

and the use goes like

variable = math.trunc(formula);

resulting in the variable getting the trunctated result of the formula
 

Ahuramazda

The Last Elementalist
Veteran
Joined
Nov 9, 2012
Messages
337
Reaction score
231
First Language
English
Primarily Uses
RMMZ
I know for my damage formula's it seems to work for rounding up/down, but not sure if its best practice in the end.... I use parseInt() when I do a lot of math that can potentially end up as decimals.

Ill use my melee attack damage formula as the example.
Code:
damage  = (((a.atk+Math.pow((a.atk/10), 2)))*(100/(100.00+b.def))+((a.atk-b.def)/2))
fi = $gameVariables.value( 8)
wi = $gameVariables.value( 9)
wa = $gameVariables.value(10)
ea = $gameVariables.value(11)
damage *= ((fi+wi+wa+ea)*0.0025)+1.00
if (a.isStateAffected(40)) { damage *= 1.25 }
if (a.isStateAffected(47)) { damage *= 0.75 }
if (b.isStateAffected(46)) { damage *= 0.75 }
if (b.isStateAffected(54)) { damage *= 1.25 }
if (damage <= 0) { damage = 1 }
damage = parseInt(damage*a.pdr)
return damage
 
  • Like
Reactions: Kes

Kes

Veteran
Veteran
Joined
Aug 3, 2012
Messages
22,853
Reaction score
12,388
First Language
English
Primarily Uses
RMMZ
@Lady_JJ You're right it is Math.round and I've finally got it working. (Don't care what anyone says, I think Ruby was easier to get my head round.) Thank you very much.

@Ahuramazda What I'm trying to do isn't part of a damage formula, but on-map with just one variable so I can't immediately see how parseint() can help in this instance. However, I shall keep it in mind should I need to do something more complex.
 

Ahuramazda

The Last Elementalist
Veteran
Joined
Nov 9, 2012
Messages
337
Reaction score
231
First Language
English
Primarily Uses
RMMZ
@Kes it would be used the same general way, but instead at the end you would just use javascript to set the variable to the value. ($gameVariable.setValue(id, value), and then when that variable ID is called in a text box it would show as you would expect.

and I am not 100% certain, but at one point when I was using Math.round() if I had very complex formula or the end result was very large/small, for some reason it would bug the game and crash... but that has been a long time and several MZ updates since I saw that error. That is part of what made me stick with parseInt() in the end.

Nothing wrong with Math.round(), but I just wanted to make sure you knew extra options just in case :D
 

caethyril

^_^
Global Mod
Joined
Feb 21, 2018
Messages
3,807
Reaction score
2,867
First Language
EN
Primarily Uses
RMMZ
By default if you try to assign a non-integer numerical value to a game variable, it will floor it (round down) to an integer. I guess you're using a plugin that changes that behaviour.

It's often a good idea to stick with integer values, at least until the final calculation step, to avoid rounding binary/decimal rounding errors like 0.1 + 0.2 = 0.30000000000000004.

Good to hear you got it working!
 

Latest Threads

Latest Posts

Latest Profile Posts

Coin Locker title screen:
Still in progress...

Screenshot_134.png
the girls.png
So, these are all the girls of the party =3 (beside the protagonist if you play as a female.)
Now, I just have one more character sheet of the guys left and I'm done with their presentations =3
Can you recognize where this area is from? :kaocry:
Mq9QM3h.png
For those with up to 9 elements of magic; how do you structure your database. By element? Or by what is offensive vs supportive spells?
would anyone here be interested in rough concept art for the main protagonist of my game that might get changed later? :kaoswt2:

Forum statistics

Threads
124,442
Messages
1,163,680
Members
163,256
Latest member
onbetltd
Top