wickednick

Regular
Regular
Joined
Feb 19, 2016
Messages
77
Reaction score
6
First Language
Nick
Primarily Uses
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?
 

eomereolsson

Regular
Regular
Joined
Sep 29, 2021
Messages
582
Reaction score
508
First Language
German
Primarily Uses
RMMV
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[0] = 7; like you are used to, you also can use array[0] = array[0] +2; (or array[0] += 2; for short).
Same for subtracting from the second value. Use array[1] -= 1;
 

Trihan

Speedy Scripter
Regular
Joined
Apr 12, 2012
Messages
6,524
Reaction score
7,032
First Language
English
Primarily Uses
RMMZ
array[0] = array[0] + 2;
array[1] = array[1] - 1;
 

Nolonar

Regular
Regular
Joined
Feb 18, 2018
Messages
504
Reaction score
722
First Language
French, German
Primarily Uses
RMMZ
array[0] = array[0] + 2;
array[1] = array[1] - 1;
Alternatively,

JavaScript:
array[0] += 2;
array[1] -= 1;

Does the same thing, but is shorter to write.
 

Trihan

Speedy Scripter
Regular
Joined
Apr 12, 2012
Messages
6,524
Reaction score
7,032
First Language
English
Primarily Uses
RMMZ
Alternatively,

JavaScript:
array[0] += 2;
array[1] -= 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

Regular
Regular
Joined
Feb 19, 2016
Messages
77
Reaction score
6
First Language
Nick
Primarily Uses
So if I'm using variable 2 it would be something like: array(2)[1] = array(2)[1] + 2;
this was what I was trying to make work $gameVariables.value(1[2]) = $gameVariables.value(1[2]) - 1;
 
Last edited:

Andar

Regular
Regular
Joined
Mar 5, 2013
Messages
39,292
Reaction score
11,475
First Language
German
Primarily Uses
RMMV
your problem is that
$gameVariables.value(1)[2]
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

Regular
Regular
Joined
Feb 19, 2016
Messages
77
Reaction score
6
First Language
Nick
Primarily Uses
your problem is that

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[3]) = $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

Regular
Regular
Joined
Mar 5, 2013
Messages
39,292
Reaction score
11,475
First Language
German
Primarily Uses
RMMV
no, that is also nonsense (value(2[3])) 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

Regular
Regular
Joined
Feb 18, 2018
Messages
504
Reaction score
722
First Language
French, German
Primarily Uses
RMMZ
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[3]) = $gameVariables.value(7);
so now the array reads 5,5,7,5
First, 2[3] 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[3]), you are:
  1. retrieving the value of 2[3] (the "fourth element of 2").
  2. asking for the value of the variable stored in the index as indicated by 2[3].
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)[3], 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)[2]


Next, $gameVariables.value(2)[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)[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;
b is obviously 3, but what about a? The answer: 2.

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[1] = 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

^_^
Global Mod
Joined
Feb 21, 2018
Messages
5,019
Reaction score
4,346
First Language
EN
Primarily Uses
RMMZ
So if I'm using variable 2 it would be something like: array(2)[1] = array(2)[1] + 2;
this was what I was trying to make work $gameVariables.value(1[2]) = $gameVariables.value(1[2]) - 1;
As mentioned, $gameVariables.value(1) returns the array you stored in that variable: the index, e.g. [1], should be outside. Another example:

$gameVariables.setValue(1, [10, 20, 30]); $gameVariables.value(1)[0] = $gameVariables.value(1)[0] + 5; $gameVariables.value(1)[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].

You might find this page and its links helpful~
 

Latest Threads

Latest Posts

Latest Profile Posts

The tutorial is live now.
Whew... I've been on a tear recently. Been working nonstop. Feels really good to be back in the groove! I think it's because I can finally see the end approaching. I only have 8 more side quests to develop (then NPCs, some art, then pretty much done).
how the hell do i change my username???
So imagine this but autotile. Stained glass ocean.
Photoshop_3xMc7HoPt8.png
Working on his first Game.

Forum statistics

Threads
134,743
Messages
1,250,212
Members
177,497
Latest member
uppo
Top