Put an Float in a variable?

Linard

Veteran
Veteran
Joined
Jun 29, 2016
Messages
202
Reaction score
18
First Language
Filipino
Primarily Uses
RMMV
Is there a way to save a float value in a Variable?
 

taarna23

Marshmallow Princess
Global Mod
Joined
Jul 20, 2012
Messages
2,402
Reaction score
4,969
First Language
English
Primarily Uses
RMMZ
From the RMMV script calls list:


$gameVariables.setValue(var, value);


You can use this to set whatever value you like: integer, float, string, array; whatever you like.
 

Zeriab

Huggins!
Veteran
Joined
Mar 20, 2012
Messages
1,269
Reaction score
1,423
First Language
English
Primarily Uses
RMXP
By default floats are what's used, as that is the basic number format of JavaScript
 

Linard

Veteran
Veteran
Joined
Jun 29, 2016
Messages
202
Reaction score
18
First Language
Filipino
Primarily Uses
RMMV
@taarna23: Thanks for the list :)  , But i tried to put a float value and the output was an Integer.


ex: $gameVariables.setValue(1, 5.68);


By printing it on console my expected output is 5.68 but the output was only 5
 

taarna23

Marshmallow Princess
Global Mod
Joined
Jul 20, 2012
Messages
2,402
Reaction score
4,969
First Language
English
Primarily Uses
RMMZ
Ugh. Yup, I just dug in, and this snippet:


Game_Variables.prototype.setValue = function(variableId, value) {
if (variableId > 0 && variableId < $dataSystem.variables.length) {
if (typeof value === 'number') {
value = Math.floor(value);
}
this._data[variableId] = value;
this.onChange();
}
};


...says if the variable being set is a number... truncate it. If it's 1.99, make it 1. If it's 1.0005, make it 1. I actually really don't like this. I wonder why it was done this way.


@Zeriab Sadly, in this case, no floats. Truncate all the things. :(
 
Last edited by a moderator:

Linard

Veteran
Veteran
Joined
Jun 29, 2016
Messages
202
Reaction score
18
First Language
Filipino
Primarily Uses
RMMV
Game_Variables.prototype.setValue = function(variableId, value) {
if (variableId > 0 && variableId < $dataSystem.variables.length) {
if (typeof value === 'number') {
value = Math.floor(value);
}
this._data[variableId] = value;
this.onChange();
}
};




@taarna23: I forgot to check what's the process behind the setValue. Thanks for this one I've created my own function that stored a Float Value in the variable.


Game_Variables.prototype.setValuetoFloat = function(variableId, value) {
if (variableId > 0 && variableId < $dataSystem.variables.length) {
if (typeof value === 'number') {
value = value.toFixed(2);
}
this._data[variableId] = value;
this.onChange();
}
};


For now i there's no error prompt. But i don't know if it's compatible.
 

Shaz

Veteran
Veteran
Joined
Mar 2, 2012
Messages
40,111
Reaction score
13,713
First Language
English
Primarily Uses
RMMV
That'll work fine.  You could also have done this:


$gameVariables._data[variableId] = value;
$gameVariables.onChange();


That last line is to set the map to refresh, in case any event pages are conditioned by variables.
 

Zeriab

Huggins!
Veteran
Joined
Mar 20, 2012
Messages
1,269
Reaction score
1,423
First Language
English
Primarily Uses
RMXP
@taarna23


What you see is an attempt to implement integers on top of floating points. In JavaScript all numbers are double precision floating points.


You can represent integers using floating points I recommend reading http://speakingjs.com/es5/ch11.html#safe_integers if you are interested.
 

taarna23

Marshmallow Princess
Global Mod
Joined
Jul 20, 2012
Messages
2,402
Reaction score
4,969
First Language
English
Primarily Uses
RMMZ
@Zeriab Oh, I see. Ensuring we don't have funky wraparound issues (like World of Warcraft did back in the day with gold, teehee).


Still, it's not really helpful to truncate it. Especially if one WANTS a floating point as in this case. This way, it's limiting all numbers to truncated integers, which seems somewhat unhelpful, as I said.


However, truncate vs. round makes more sense now.
 

Users Who Are Viewing This Thread (Users: 0, Guests: 1)

Latest Threads

Latest Profile Posts

Don't forget, aspiring writers: Personality isn't what your characters do, it is WHY they do it.
Hello! I would like to know if there are any pluggings or any way to customize how battles look?
I was thinking that when you start the battle for it to appear the eyes of your characters and opponents sorta like Ace Attorney.
Sadly I don't know how that would be possible so I would be needing help! If you can help me in any way I would really apreciate it!
The biggest debate we need to complete on which is better, Waffles or Pancakes?
rux
How is it going? :D
Day 9 of giveaways! 8 prizes today :D

Forum statistics

Threads
106,049
Messages
1,018,546
Members
137,835
Latest member
yetisteven
Top