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

yuyu!

The Hero of Rhyme
Member
Joined
Feb 19, 2014
Messages
25
Reaction score
12
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
 
Last edited by a moderator:

Tsukihime

Veteran
Veteran
Joined
Jun 30, 2012
Messages
8,564
Reaction score
3,848
First Language
English
@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.
 
Last edited by a moderator:

yuyu!

The Hero of Rhyme
Member
Joined
Feb 19, 2014
Messages
25
Reaction score
12
@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. ;__;
 
Last edited by a moderator:

Andar

Veteran
Veteran
Joined
Mar 5, 2013
Messages
31,430
Reaction score
7,711
First Language
German
Primarily Uses
RMMV
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.
 
Last edited by a moderator:

Tsukihime

Veteran
Veteran
Joined
Jun 30, 2012
Messages
8,564
Reaction score
3,848
First Language
English
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))
 

yuyu!

The Hero of Rhyme
Member
Joined
Feb 19, 2014
Messages
25
Reaction score
12
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:





Makes this happen:





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.
 

Tsukihime

Veteran
Veteran
Joined
Jun 30, 2012
Messages
8,564
Reaction score
3,848
First Language
English
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.
 

yuyu!

The Hero of Rhyme
Member
Joined
Feb 19, 2014
Messages
25
Reaction score
12
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! ^_^
 

Users Who Are Viewing This Thread (Users: 0, Guests: 1)

Latest Threads

Latest Posts

Latest Profile Posts

Our latest feature is an interview with... me?!

People4_2 (Capelet off and on) added!

Just beat the last of us 2 last night and starting jedi: fallen order right now, both use unreal engine & when I say i knew 80% of jedi's buttons right away because they were the same buttons as TLOU2 its ridiculous, even the same narrow hallway crawl and barely-made-it jump they do. Unreal Engine is just big budget RPG Maker the way they make games nearly identical at its core lol.
Can someone recommend some fun story-heavy RPGs to me? Coming up with good gameplay is a nightmare! I was thinking of making some gameplay platforming-based, but that doesn't work well in RPG form*. I also was thinking of removing battles, but that would be too much like OneShot. I don't even know how to make good puzzles!
one bad plugin combo later and one of my followers is moonwalking off the screen on his own... I didn't even more yet on the new map lol.

Forum statistics

Threads
106,035
Messages
1,018,455
Members
137,821
Latest member
Capterson
Top