RMMV Need decimal values in variables and calculate with them

caethyril

^_^
Veteran
Joined
Feb 21, 2018
Messages
2,629
Reaction score
2,004
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
832
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,737
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
832
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

IlluminaughtyStore.jpg
:kaojoy:
Welcome to the Illuminaughty store.
Yes... they are "wearing Illuminati branded everything, reclining on huge Illuminati body pillows. Occasionally they'll "teehee" at each other and have pillow fights with Illuminati branded pillows full of hundred dollar bills."
BCj
Working on Escalia again, feels good. Have this overwhelming urge to map as well, but need to get some extra tiles commissioned first :D
Lee Sang wrote on Bandito's profile.
Hi, I've just tried your plugin "Game is Active" and when I tested it on Android, it gave me an error, something about "Cannot read property 'underfined' of underfined.
With my project, I want the player to have access to all the tools they need to get from New Game to Credits as soon as possible; I want the player's progress to only be impeded by the player not knowing how to use the tools the game provides.
Just a little close up shot...
w2xZocX.jpg

Forum statistics

Threads
113,867
Messages
1,078,031
Members
147,957
Latest member
Broskiman1
Top