.push is not a function (beginner question)

Hello,

I'm new to RPG Maker and everything related to coding. I get an error message and can't figure out why. No plugins used, just the basic program.

I created an event and when I walk over it it does:

image1.png


and in the end it tells me:

image2.png


$gameVariables.setValue(30,[1,2,3,4]);
$gameVariables.setValue(31,$gameVariables.value(30)[2]);
$gameVariables.value(30).push("test");
$gameVariables.value(31).push("test");

\V[30] is displayed as I wanted it to be: 1,2,3,4,test
\V[31] generates the error message but I wanted it to say: 3, test

Why?

What I want to do in the end is the following: Create a character with different attributes. Later I want to check if a certain attribute is x or y and then do something depending on the result. I searched how to modify these things and found that you can for example .push and .slice arrays and thought this might be useful, so I tried. Not very well it seems.

Thank you for reading.
Push is a function that can only be used on arrays. Variable 30 is an array of [1,2,3,4], so that will work fine. But variable 31 is not an array, it's the third element of the array in variable 30, which is the number 3, and therefore not something you can use push on.
When I write

$gameVariables.setValue(30,[1]);
$gameVariables.setValue(31,$gameVariables.value(30)[0]);

it returns

\V[30] = 1
\V[31] = 1

In both cases the variable return 1 but 30 is an array (or acts like one if I try to push something into it) and 31 is not. It's just the number 1? I don't understand. Can you point me towards something where can read up on this case?
The reason you're getting the same value for both 30 and 31 is that the \V code stringifies the value, so even though variable 30 is an array it's being represented as a string. You can push to it because it's still an array. 31 is an integer (because it's a specific value *from* the array), and push is not a function that exists on integers.

If you want to be able to push to 31 as well, you need to set its initial value as an array:

JavaScript:
$gameVariables.setValue(31, [$gameVariables.value(30)[0]]);
The reason you're getting the same value for both 30 and 31 is that the \V code stringifies the value, so even though variable 30 is an array it's being represented as a string. You can push to it because it's still an array. 31 is an integer (because it's a specific value *from* the array), and push is not a function that exists on integers.

If you want to be able to push to 31 as well, you need to set its initial value as an array:

JavaScript:
$gameVariables.setValue(31, [$gameVariables.value(30)[0]]);
This helped me understand the problem, thank you.

Original thread: https://forums.rpgmakerweb.com/threads/push-is-not-a-function-beginner-question.162147/