"Decoding" a variable

KotoYama

Veteran
My game has tons of randomized content since it would be too much trouble to create a unique set of variables for each event I made a system of "keys" (similar to Minecraft) in which each number would contain a certain parameter value.
Now the question is how do I decode them a.k.a. check each numeral's (digit) value. Imagine a variable has a value of 123 where 1 would be the difficulty, 2 levels number and 3 anything else, etc.
I'm not an expert, but I hope I managed to explain myself.

Last edited:

Jules98

Veteran
You can 'decode' a variable like this using a modulo operation, but using an array to store multiple values in a single variable would be so much more practical. I'm not particularly knowledgeable on javascript, though, so I can't help you any further with that.

Another Fen

Veteran
If you are working with numbers, a popular way is to use division and modulo operators (modulo being the remainder of a division). (Edit: Too slow )

So for example, 123 / 100 would be 1.23 (or 1 after rounding down), and 123 % 100 would be 23 (remainder).
Game variables used in event commands are automatically rounded down in MV, other variables you might use in scripts or plugins you would have to round down yourself.

Keep in mind that numbers in Javascript have a fixed accuracy and can only store a total of ~15 decimal digits before becoming inacurate. If you need your key to store more information you can use a string or an array/object for that (or multiple variables).

You could maybe also look into a plugin for "Self Variables" or "Self Data" (I know there was a popular option for this for the VXAce, no idea how MV fares here though).

KotoYama

Veteran
So I'll need to find a plugin that could check the variable's digits(had no luck so far) value or some knowledge of Javascript?
I've read something about strings and modulo operators, but I still have no idea how to use them in order to get the effect I want

Dev_With_Coffee

Veteran
I did not understand it well, but I believe that you want to separate the digits of a variable in others.

Let's consider that the variable reaches a maximum of 9999, thus having a maximum of 4 places.

You can do this with a simple Javascript command, but if you need to do it for events, it would be like this:
Code:
``````◆Comment：---------------------------------------------------------
◆Control Variables：#0025 MyNumber = 5423
◆Comment：---------------------------------------------------------
◆Control Variables：#0021 A*** = MyNumber
◆Control Variables：#0022 *B** = MyNumber
◆Control Variables：#0023 **C* = MyNumber
◆Control Variables：#0024 ***D = MyNumber
◆Comment：---------------------------------------------------------
◆Control Variables：#0021 A*** /= 1000
◆Control Variables：#0022 *B** /= 100
◆Control Variables：#0023 **C* /= 10
◆Comment：---------------------------------------------------------
◆Control Variables：#0021 A*** %= 10
◆Control Variables：#0022 *B** %= 10
◆Control Variables：#0023 **C* %= 10
◆Control Variables：#0024 ***D %= 10
◆Text：None, Window, Bottom
：    ：\v[21],\v[22],\v[23],\v[24]``````

The digit of place "D" does not need the split operation.

If you need to work with a larger variable let me know, that I make a command via Javascript or a mini-plugin.

KotoYama

Veteran
I see. Cool! Thanks! This should work for now!

mlogan

Global Moderators

This thread is being reopened at OP's request.

KotoYama

Veteran
I have a problem. My variables don't get rounded automatically(maybe cause they're placed in a common event?). I'm using Math.round(\$gameVariables.value(X)) script call, but it won't work properly if the decimals have values superior to 0.5

Jules98

Veteran
Any numeric value stored in \$gameVariables is automatically truncated (rounded down), so Math.round() won't do anything since the value is already round in the first place.

JGreene

Veteran
Try using Math.ceiling or Math.floor if you want to round them

KotoYama

Veteran
Try using Math.ceiling or Math.floor if you want to round them
Math.floor works great! Thanks!
Any numeric value stored in \$gameVariables is automatically truncated (rounded down)
It doesn't work like that for me. Tired of explaining that :0 Plus I need those decimals for other purposes.

Dev_With_Coffee

Veteran
It doesn't work like that for me. Tired of explaining that :0 Plus I need those decimals for other purposes.
Do you need to use fractional numbers?
If so, try the multiplication operation.
Example:
JavaScript:
``````var num_data=128.51;
num_data*=100; //return 12851``````

Assuming that you will have fractional numbers with up to 2 digits:
128.51 is 0,0,0,1,2,8,5,1
60.23 is 0,0,0,0,6,0,2,3
0.5 is 0,0,0,0,0,0,5,0

Now if you need to separate data for different information in the same variable it gets more complex:

It would be better to store this data in an Array with pure Javascript and then distribute it in game variables.
I don't know if it is worth doing this as a single variable.

Last edited:

Latest Profile Posts

Micro rant. The worst part of working from home when you live with your family is seeing how inconsiderate they are. "Hey, you're sitting in front of your computer, that means you're not doing anything! I want to put the TV at 200 decibels!"

If I was working in the office would you still talking me like that?
It's definitely discouraging when streamers give a try-to-say-it-nicely (i.e. bad) review of your game demo. It's like jumping into a cold pool. Bad initally, but it gets better with time. And you are now more alert and see better.
Some screenshots of the pub and lodging with the beginnings of clutter taking shape

Backgrounds are not my strong suit...

Just realized I forgot doing the clouds... woops
Went to get some stuff for February 10, the birthday of my favorite ink monster...
I have a little one-person party for Bendy every year, complete with the life-size plushie I made. And yes, I bake a birthday cake!