Arrays or similar in RMMZ??

Hi everyone.
Is it possible to deal with arrays or similar in RPG Maker MZ?
I am especially referring to adding a value to a text array stored as variable.
I used to store text value in a variable using the script field and putting the value under quotes; but what if i want to add a new letter to this value?
I need this because I want to implement in my project a room where there are tiles on the floor with the letters of the alphabet: player has to "write" words by passing over the correct tiles, and this would be very easy to do if every new letter could be easily added to the others. I know this could be done in other ways, too, but with a lot of work and using of variables and/or swithces, especially if I wanto to make this thing a repeatable event (with, say, a dozen different words).
Any help?
I am especially referring to adding a value to a text array stored as variable.
you can still use array methods on a game variable array. so if you need add a value to one, you can still use .push() to do so

I need this because I want to implement in my project a room where there are tiles on the floor with the letters of the alphabet: player has to "write" words by passing over the correct tiles, and this would be very easy to do if every new letter could be easily added to the others
that would be easier with a string imo. youd start with an empty string and then concatenate the new letters on to the variable as the player steps on tiles. just a
Code:
$gameVariables.value(1) + "a"
inside the script box for control variables would do
You can put arrays into Game Variables. The game will not crash, but you should pay some attention.

I recommend using arrays only to those with some knowledge of JavaScript, but when you have, here you go!

To initialize an array, do Variable OP, select "Script", and input [ ]

To add an element, you should use "Script" instead of "Variable OP", and type this:

Code:
$gameVariables.value(1).push(2)

To query for some element, you can always type

Code:
$gameVariables.value(1)[2]
To get element 2 of the array of Game Variable 1
Are you sure you need an array for what you're trying to do? You might possibly achieve the same result using string methods. However, it greatly depends on your knowledge of JavaScript and how you intend to use it.
I have very little knowledge in JavaScript, I am mor into Python, bot if it's easy to do and you can give me some hint I'll really appreciate it
Are you sure you need an array for what you're trying to do? You might possibly achieve the same result using string methods. However, it greatly depends on your knowledge of JavaScript and how you intend to use it.
You could use the concatenation operator.

Let me give you an example. Let's say I have the word "car," and the player steps on the letter "s," creating the word "cars."

To do this, I'll show you an example in JavaScript:

```
let example = "car";
example += "s";
console.log(example); // output: cars
```

How do we translate this to RPG Maker?

Let’s say your variable 1 is equal to "car."

The player steps on the letter "S."

Variable 1 + (script) "s"

Now your variable will be equal to "cars."

However, I want to warn you about something important. The Visustella multi-language plugin doesn’t work with words defined in this way. So, if you want to make a multilingual game using the Visustella plugin, you won’t be able to, as it doesn’t work with variables containing strings (or at least I haven’t been able to make it work).
I have very little knowledge in JavaScript, I am mor into Python, bot if it's easy to do and you can give me some hint I'll really appreciate it
...did you read the very first reply to your thread? From @Robro33, an hour and twenty minutes earlier? Where he literally gave you the exact syntax to use?
Many many thanks to @Robro33 and @Aerosys, you saved my life!
Really appreciated also @ISC step-by-step explication.
This community is wonderful
Hey everyone,
now I am dealing with how to remove the last element of my array/string.
I have declared a variable as a string with var1 = [ ]
Then I am adding element with $gameVariables.value(1) + "a"
After putting some characters in it, how can I remove the last one?
I've been trying JavaScript commands like pop, lenght or slice, bot I only got some weird results. Probably I am also messing up things with JS's syntax...
Any help?
Thanks in advice!
At this point, it is probably the most efficient to google "javascript how to remove last element of an array/last character of a string". You will find many pages that describe all possible operations on arrays and strings.

To remove last element of an array:
Code:
$gameVariables.value(1).pop()
Replace 1 with your Variable ID

To remove last character of a string:
Code:
const string = $gameVariables.value(1)
$gameVariables.setValue(1, string.substring(0, string.length - 1))
Again, replace the two 1s that you see in the $gameVariables function, but keep the last 1 next to string.length as it is.
To add - you're starting with an empty array [], but that gets converted to a blank string when you try to add a string to it:
  • [] + "a" -> "" + "a" -> "a".
So you would use @Aerosys's string example, or change your approach to use an array.
Thanks!!
At this point, it is probably the most efficient to google "javascript how to remove last element of an array/last character of a string". You will find many pages that describe all possible operations on arrays and strings.
That's exactly what I did for the two hours before writing my post! 🙂 I like trying to learn things by myself before asking for help, but as I wrote I only got wrong or weird results, so I came here and asked :-D
Thanks to your help now I got what I wanted, still I got a question: with your code, when removing the last element the remaining value of the variable is 0, but I'd like it to be empty. I already easily fixed this with a conditional, but as I wrote I like to learn things, so: is there a way to do this only by changing your code?
Many thanks!!
when removing the last element the remaining value of the variable is 0, but I'd like it to be empty
the value of the variable is probably "", but the value() function was written to return 0 in a few specific cases. its likely that it outputs 0 instead of empty strings too. you can try using $gameVariables._data[1] instead and see what you get, but make sure you set its value at some point first before accessing it
Or you can forget $gameVariables and use something like $gameSystem.variables to make your own variable list.

For example, you initialize it in the first map of the game.

$gameSystem.variables = {};

Then you can just use something like $gameSystem.variables.stringA = "this is a string value"
Learning how to use Arrays has opened up a whole world of possibilities for me. I highly recommend searching for some RPG Maker tutorials that give examples of using Arrays and you can check out a free online course like W3Schools for JavaScript and you will find lots of useful information. I also just search for specific JavaScript questions and can usually find what I need.

I have some tutorials that highlight different uses of Arrays. My tutorials progressed as I learned more about how to use Arrays and some of those tutorials specifically mention Arrays in the tutorial title or description.

My current Sudoku project uses a LOT of Arrays. For instance, right now, I am using an Array of stored data that I can pull out and use in a Variable to identify other specific Arrays and I use other Variables to identify the position location of the value I want in the appropriate Array. So my script is using Variables to identify both the Array I need to pull data from and the position of that data in the Array.

And 3 years ago, I had zero experience with RPG Maker, programming, and JavaScript.

My best advise is to take a step back, watch a bunch of tutorials and recreate the Array portions yourself in a test project. Read about Arrays and how they are used in a free JavaScript resource and recreate those functions in your test project. Then use your test project to experiment with what specifically you want to do at this time.

Because learning as much as you can about Arrays now, how they work in MZ, all the things you can do with them, will probably greatly expand your options going forward.

And this forum is great for specific questions.

Good luck and embrace the power of the Array!
Like Robro says, the core $gameVariables.value method returns 0 for falsy values, e.g. a blank string.

An empty array is still truthy, so that would be a valid workaround. If you want to display the contents of an array as a string, you can still do so using Array#join:
JavaScript:
$gameVariables.value(1).join("");
E.g. if variable 1 had the value ["a", "b", "c"], the above code would return "abc".

However, I think I'd just use a branch for "is variable = 0", like you've already done.




you can use this website to practice coding your arrays

Original thread: https://forums.rpgmakerweb.com/threads/arrays-or-similar-in-rmmz.171789/