[Solved] Using $gameVariables.setValue to change one array element?

Hi, everyone!


I'm having trouble changing only one array element using $gameVariables.setValue. I've tried $gameVariables.setValue(47[1], "test"), and that doesn't seem to do anything. Every other combination I've tried has thrown errors at me. I already have the variable initialized as an array of several values, set at the beginning of the game... I just need to be able to change values individually.


I feel like the answer is staring me right in the face, but I just can't get it! 🙁  Any help would be mega appreciated! <3


-yuyu
@yuyu!


What is 47[1] supposed to mean?


Does game variable 47 hold an array? And you're trying to access the second element of that array?


$gameVariables.value(47)[1] = "new value"


You can access the array directly and change the value.
@Tsukihime


Thank you for your response! 🙂


"47" is the variable id I'm testing with, and [1] is the array index I hope to change. I thought the element [1] would have to be listed right after the variable id itself, but that didn't do the trick.


*edit*


I've also tried "$gameVariables.setValue(47, "test")[1]", but that didn't work, either. ;__;
you cannot use the setvalue function to access an array, because that is not defined for that function (unless you rewrite them with a plugin).


Why didn't you use the syntax Tsukihime provided?


EDIT: Show us how you defined the array, that should clear things up - you need to define an array before you can access its elements.
This is one way to work with arrays in game variables:

Code:
// set variable 1 to an array of numbers
$gameVariables.setValue(1, [1, 2, 3]);

// Access variable 1, and index into the second element and set it to 10
$gameVariables.value(1)[1] = 10;

// print out variable 1, which is the array, which should now be [1, 10, 3]
console.log($gameVariables.value(1))
you cannot use the setvalue function to access an array, because that is not defined for that function (unless you rewrite them with a plugin).


Why didn't you use the syntax Tsukihime provided?



Ahhh, that makes sense. Thank you. 🙂  For some reason, the edit updated super slowly for me, so I only saw the first line of his message.

@yuyu!


What is 47[1] supposed to mean?


Does game variable 47 hold an array? And you're trying to access the second element of that array?



$gameVariables.value(47)[1] = "new value"


You can access the array directly and change the value.



That works! Thank you so much, Tsukihime! 🙂


Just as a head's up to everyone: I did encounter a really, really weird bug with chaning variables assigned via a batch command. I initialized every array I intend to use (via batch variables) and then tried to change the value of just one. For whatever reason, they all want to change with it. o.o


Doing this:


2A4Y4aR.png



Makes this happen:


4Pb5kj2.png



It works just fine if I initialize them all on their own, though. 🙂 That's good enough for me!


Thank you both so much! I'll go ahead and mark this thread solved.
All of the variables hold a reference to the same array.


Therefore, changing the array for one variable, will change all of the other variables as well because they point to the same thing.
All of the variables hold a reference to the same array.


Therefore, changing the array for one variable, will change all of the other variables as well because they point to the same thing.



Doh! I do seem to remember hearing something about that in my newbie programming classes.


Thank you again!! You're a life saver! ^_^

Original thread: https://forums.rpgmakerweb.com/threads/solved-using-gamevariables-setvalue-to-change-one-array-element.66973/