Can I do math in arrays

wickednick

Veteran Say I have an array set to [5,5,5] and I want to subtract from the second number in the array by one and add to the first number 2, so it's now [7,4,5]. How can I do this?
I know you can directly change the numbers in the array, but for what I'm doing I actually want to be able to perform math on the individual numbers in the array. Is this possible?

• SigmaSuccour

eomereolsson

Veteran A single value in an array is the same as any variable and you can perform math operations with it as usual. So instead of setting the first value of the array to 7 with array = 7; like you are used to, you also can use array = array +2; (or array += 2; for short).
Same for subtracting from the second value. Use array -= 1;

Trihan

Speedy Scripter array = array + 2;
array = array - 1;

• Starbird_Resources

Nolonar

Veteran array = array + 2;
array = array - 1;
Alternatively,

JavaScript:
array += 2;
array -= 1;

Does the same thing, but is shorter to write.

Trihan

Speedy Scripter Alternatively,

JavaScript:
array += 2;
array -= 1;

Does the same thing, but is shorter to write.
I was going to do the shorthand but wasn't sure it would be as intuitive to read if OP wasn't familiar with that method.

wickednick

Veteran So if I'm using variable 2 it would be something like: array(2) = array(2) + 2;
this was what I was trying to make work \$gameVariables.value(1) = \$gameVariables.value(1) - 1;

Last edited:

Andar

Veteran \$gameVariables.value(1)
is NOT an array.

value(id) is a function of \$gameVariables that returns the value stored in the variable of id, it is not the variable itself so it can't be turned into an array element.

You'll need to go deeper into javascript to access the values directly without the function framework.
Unfortunately I don't know that syntax, you'll have to wait until one of the others above shows you.

wickednick

Veteran is NOT an array.

value(id) is a function of \$gameVariables that returns the value stored in the variable of id, it is not the variable itself so it can't be turned into an array element.

You'll need to go deeper into javascript to access the values directly without the function framework.
Unfortunately I don't know that syntax, you'll have to wait until one of the others above shows you.
From what I've seen from reading other stuff and watching videos to make an array you make something like \$gameVariables.setValue(2,[5,5,5,5]);
then if you want to access it or change something it's \$gameVariables.value(2) = \$gameVariables.value(7);
so now the array reads 5,5,7,5
The question is how can I change those numbers by adding or subtracting from them. Say I want to subtract 1 from position three every time I push a button.

Andar

Veteran no, that is also nonsense (value(2)) because the number 2 is not an array.
an array is more than just adding a bracket to something.
and what works for a newly defined array will not work for predefined variables or for arrays stored inside predefined variables that are accessed by functions.

As you've already seen you cannot get it right by guessing - which is why I haven't suggested alternatives because then I would be the one guessing. You'll need the exact syntax of the internal access to the game variables for this to work, and that is not through the value function at all - that can only be used to retrieve the entire array if it was stored by script into a game variable.

Nolonar

Veteran From what I've seen from reading other stuff and watching videos to make an array you make something like \$gameVariables.setValue(2,[5,5,5,5]);
Technically, this is more than simply "make an array". You are making an array with the 4 elements: 5, 5, 5, and 5, and storing it into Variable #0002.

You can then retrieve that array using \$gameVariables.value(2);

then if you want to access it or change something it's \$gameVariables.value(2) = \$gameVariables.value(7);
so now the array reads 5,5,7,5
First, 2 is invalid, because 2 is a number, not an array.
You are trying to access the 3rd(*) element of 2, which makes no sense because 2 doesn't have any elements, since it's not an array.

(*) I said "third", but actually it's the fourth. Arrays are accessed using an index (think of it as a house number), and in JavaScript the first element has index 0. So the third element has index 2, not 3.

Next, even if 2 were an array (which it isn't, but let's just pretend), you still wouldn't be getting what you expect.

\$gameVariables is an object created by the RPG Maker engine, and contains a bunch of stuff, such as the setValue() and value() functions. Internally, \$gameVariables uses an array _data to store all the variables you need for your game, which are accessible from within the editor.

The value() function gives you the value of whatever variable is stored in the \$gameVariables._data array at the index you specified. By asking for \$gameVariables.value(2), you are:
1. retrieving the value of 2 (the "fourth element of 2").
2. asking for the value of the variable stored in the index as indicated by 2.
So if 2 was the array [5,5,5,5] (it isn't, but let's just pretend), then you'd be asking for the value of Variable #0005, which is not what you want.

What you actually need is \$gameVariables.value(2), which will retrieve the value of Variable #0002 (in which we have stored our array), and access its fourth element. Although, since your aim is to get [5,5,7,5] instead of [5,5,5,7], you'll need index 2 instead, so \$gameVariables.value(2)

Next, \$gameVariables.value(2) = \$gameVariables.value(7); might result in [5,5,7,5], but it also might not. That's because \$gameVariables.value(7) is not 7; it's the value of Variable #0007 (which could be 7, but it could also be something else entirely). Instead it should be \$gameVariables.value(2) = 7;

Finally, since you used \$gameVariables.value(x) = y; and appear to be a beginner, I believe it's important that you understand the difference between value type and reference type variables. Otherwise, you might end up in a situation where you believe your code should work, but doesn't.

Variables come in 2 forms: value type and reference type. In the following example:
JavaScript:
a = 2;
b = a;
b = 3;

That's because numbers are value types. We are storing the value 2 into variable a, then copying that same value into variable b, then overwriting b with the value 3. We never changed the value of a to anything other than 2.

However, in the next example:
JavaScript:
a = [9, 8, 7];
b = a;
b = 5;
b is obviously [9, 5, 7], but what about a? The answer: it's also [9, 5, 7].

Why is that? Because arrays are reference types. Instead of storing the entire array (which could be huge) into a and b, the array is created somewhere in memory, and a reference to that array is stored into a and b. As a result, they both refer to the same array, and modifying the array in one location will also modify it in another.

Why is that important? Because \$gameVariables.value() is a function that gives you a result. It is not the variable itself, and therefore cannot be overwritten. When you use \$gameVariables.value(2), the result you're getting is a reference to your array, and modifying a specific element from it also modifies the original array.

If \$gameVariables.value(2) were a number instead, you would have overwritten the result (the value of a number) with something else. But the value of \$gameVariables.value(2) itself still wouldn't have changed.

• caethyril

^_^ So if I'm using variable 2 it would be something like: array(2) = array(2) + 2;
this was what I was trying to make work \$gameVariables.value(1) = \$gameVariables.value(1) - 1;
As mentioned, \$gameVariables.value(1) returns the array you stored in that variable: the index, e.g. , should be outside. Another example:

\$gameVariables.setValue(1, [10, 20, 30]); \$gameVariables.value(1) = \$gameVariables.value(1) + 5; \$gameVariables.value(1) += 5;

This says:
1. Set game variable 1 equal to [10, 20, 30] (an array of 3 numbers).
2. Set the first value (index 0) in that array equal to its current value plus 5.
3. Add 5 to the second value (index 1) in that array.
The resultant value of game variable 1 should be [15, 25, 30].

• Latest Profile Posts

I'm doing a fairy tale project Little Red Riding Hood and the Woodsman. I'm almost done with the demo in my native language and I intend to make a demo version in English as well. I'm just going to use the translator because I don't know the pronunciations very well, I only know how to say "Hey" and nothing else.. Maybe you could take a look at my project.
Do you think horror RM games are still a thing today? 