@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. 👋
MV Need decimal values in variables and calculate with them
Javascript/Plugin SupportThis 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.
@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.
developer.mozilla.org
www.w3resource.com
www.digitalocean.com
security.stackexchange.com
dev.to
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.
Set your variable to
...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.
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.
eval() - JavaScript | MDN
The eval() function evaluates JavaScript code represented as a string and returns its completion value. The source is parsed as a script.
JavaScript eval function - w3resource
The JavaScript eval function is used to execute JavaScript source code.
Reasons Why You Should Never Use eval() in JavaScript | DigitalOcean
You may have never heard of the eval function in JavaScript, and that’s probably for a good reason! It should almost never be used and here we explain why.
What are the security issues with "eval()" in JavaScript?
Every time that someone mentions eval(), everyone says that there are "security issues" with it, but nobody ever goes into detail about what they are. Most modern browsers seem to be able to debug ...
Everything Wrong With JavaScript `eval()`
This week I had the pleasure to get to know more about the infamous eval() function, as it was my fir...
dev.to
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.
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.
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.
Original thread: https://forums.rpgmakerweb.com/threads/need-decimal-values-in-variables-and-calculate-with-them.137379/