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:
- retrieving the value of
2[3]
(the "fourth element of 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:
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.