RMMV Need decimal values in variables and calculate with them

caethyril

^_^
Veteran
Joined
Feb 21, 2018
Messages
2,640
Reaction score
2,011
First Language
EN
Primarily Uses
RMMZ
@Zion - like Trihan said, you should keep the actual value (integer) and the display value (text) in separate variables. Increase the number, recalculate the text, then display the text. :kaohi:
 

Restart

Veteran
Veteran
Joined
Mar 15, 2019
Messages
834
Reaction score
671
First Language
English
Primarily Uses
RMMV
@Restart
String conversion is fairly heavy, as opposed to simply removing an operation as I've done which actually speeds up MV/MZ. Depending on how often your method is used, you will create serious resource requirements, where as mine will always yield a positive gain on performance.
This is a wrapper function for this dedicated purpose, not a replacement of the existing code for every variable. As is, your change can breaks variables displaying in text boxes, and getting them to display right which is the whole point.

Zion said he'd only be occasionally changing it by +-0.1; if the computer is chugging due to occasionally dealing with a three or four character string every few seconds, there are far bigger performance problems (and if it's changing by +-0.1 multiple times a second, that aquarium's pH is gonna make it melt through the floor within a minute).


I also have trouble seeing how eval poses a security risk in this case; MZ is running in a browser environment; anyone with control of the game can get it to run arbitrary JS at any point. Still, I can always swap in JSON.parse if it makes you happier.
 

ImaginaryVillain

High Cultist of the Sporkle
Veteran
Joined
Jun 22, 2019
Messages
934
Reaction score
5,749
First Language
Absurdism
Primarily Uses
RMMZ
@Restart
As I said earlier, "And all that entails". Meaning with what I handed him, he would gain the ability to do so very much. However that ability comes with all the pitfalls of it.

As for Eval, largely it depends on how it's deployed. If it's just exported to an exe... then it's unlikely to be an issue. If however it's deployed to a server, Eval can be used to execute code on a server with the same permissions as the caller.


Not an exhaustive list, just the first handful of results that came up.

Honestly though, for all that I say Eval is bad... RM uses it for it's script commands so, we're already well past not secure and into "uh oh" territory. It's largely just reflex at this point to point out it's a security flaw. It's real crime is just being insanely slow.

Will any of this matter here? Probably not. I'm just very big into optimization.

@ImaginaryVillain yes but I have a problem with your method as described above :(
Ah sorry about that. I admit I didn't really pay too much attention when scanning through stuff. Alright if you have chosen to use the method I suggested...

Set your variable to
JavaScript:
($gameVariables.value(#) * 10 + 0.1 * 10) / 10;

...where # is the variable # you're adding to. For the subtraction, just replace the + with a - in that code. As for why you're getting those results, it's because Javascript is well in the simplest terms, bad at a math. Specifically floating point math, there's a long explanation but let's just say when decimals are involved, Javascript isn't always going to give you the right answer.

However as my method frees up the numbers to at least use decimals, you can simply multiply them, so they turn into Integers, then divide the result to turn it back into a decimal.
 

Restart

Veteran
Veteran
Joined
Mar 15, 2019
Messages
834
Reaction score
671
First Language
English
Primarily Uses
RMMV
@Restart
As I said earlier, "And all that entails". Meaning with what I handed him, he would gain the ability to do so very much. However that ability comes with all the pitfalls of it.

As for Eval, largely it depends on how it's deployed. If it's just exported to an exe... then it's unlikely to be an issue. If however it's deployed to a server, Eval can be used to execute code on a server with the same permissions as the caller.


Not an exhaustive list, just the first handful of results that came up.

Honestly though, for all that I say Eval is bad... RM uses it for it's script commands so, we're already well past not secure and into "uh oh" territory. It's largely just reflex at this point to point out it's a security flaw. It's real crime is just being insanely slow.

Will any of this matter here? Probably not. I'm just very big into optimization.

Yeah, but again, this a local program - the user isn't gonna be able to escalate permissions using rpgmaker, since it's still running with their permissions; if they wanted to execute arbitrary code within the game, they can do it from the console (or by editing the js files), and if they wanted to execute arbitrary code outside the game, they can (evidently) run executables already.

If you were building the game with a client-server setup, you'd need to use a fundamentally different architecture than rpgmaker, since the client-side data needs to be assumed to be untrustworthy at all times (if you're doing anything with player-to-player interactions, you can't store things like hp, inventory, player location, et cetera on the client - as many poorly-made mmorpgs have learned the hard way).

But, as you said, the fact that script calls already exist means that the horse has left the barn ages ago - it's pretty trivial to (for instance) create a rpg maker save file that executes arbitrary code when loaded (and this is something that happens with real games as well, like the pokerus thing MrCheeze made for pokemon red and blue).

The important thing is to be aware of the security context.
 

Latest Threads

Latest Profile Posts

Fire-Emblem Style Level Up System, Boost on Class Change, And Animated Arrows | RPG Maker News #115

Why keyboards don't sleep? Because they have two shifts.
I realized a while ago my game was toooo big in scope. even now I wonder am I trying to do too much by myself. and that's after completely changing the format of the game.
Last week at SLC. Cali awaits. TBH I love SLC more than SF.
Back home now. I had a good time, but I had a checkup this morning so I had to come home for that. Now I'm just waiting for my therapist meeting tomorrow...
Decided to work on Wishful Wanda today. Since it's a BFB spinoff game, I decided to make the Huang brothers
Easter eggs! I'm actually pitching this to the BFDI team, btw.

Forum statistics

Threads
113,935
Messages
1,078,525
Members
148,044
Latest member
Sernyx
Top