Need help with variables

Hey everyone, is it possible to store a list of numbers and access them in order?
I'm working on an open world game and am currently trying to implement a music system. I've already added the first playable instruments, so you can dependent on the instrument you selected and the keys/combination of keys you press play notes or chords. That part was easy and works wonderfully.

Now comes the tricky part - since I want the players to be able to earn gold with music (basically busking) I'm trying to figure out how to implement songs and tunes. If I want those to be longer than 5 notes or so it'll become an eventing nightmare as far as I can see.
I was hoping there is a way to store a tune's notes as a string of numbers and one after another if the right note is played.
For example a progression of C E G B G E C I'd display as 1 3 5 7 5 3 1, then check on the first note played if that's indeed a C or 1, on the second note played if that's an E or 3 and so on. It would be easy -if tedious to event such small numbers, but I'm really hoping that it's possible to implement whole jigs or reels into this system, and those have at least 50+ notes or so.

So, does anyone have an idea how to implement this? 🙂
yes and no, but you could store the order of playing them

you can take a look how I did the ocarina of time instrument.
that saves the song, use images to illustrate how to do it nicely.

which I think your after.

[here] is the forum link to it.
You could store each song's notes as digits of a number and extract them using the Mod operator...but the Number type in JavaScript is only precise to 16~17 digits, so a 50+ note song wouldn't work with that approach (at least, not with only 1 variable).

You can use a script call to store an "array" in a variable, e.g.
  • Control Variables -> Script: [1,3,5,7,5,3,1]
You can then access each value via script call, by specifying its index, i.e. its position in the array.
JavaScript:
$gameVariables.value(123)[0]
This gets variable ID 123, and returns the value stored at index 0, i.e. the first value (1). The next value is index 1 (3), then index 2 (5), etc. You may want to use a different variable for "current index", e.g.
JavaScript:
$gameVariables.value(123)[$gameVariables.value(456)]
Obviously, replace the variable IDs with something appropriate for your game~

Another array check that would probably come in useful is .length. This counts how many values are in the array, e.g.
JavaScript:
$gameVariables.value(123).length
E.g. the length of your example is 7. So if the index variable gets to 7 or more (because index starts at 0, not 1), you know that the entire song has been completed.

Technical information about arrays in JS can be found here:
The way I'm doing this in my game is like this:
First, I set a variable to an empty array, using the script option to set it to [].

FsMWIbS.png


Then, in my piano playing event, I use the input keymapper to map various keys on the keyboard to the note I want and run over the notes so that depending on the key pressed it will play the right sound effect, show the right picture of the keys being pressed down, and push the current note into my array - I use the script

$gameVariables.value(45).push(item)

to add the current note to my Piano Array (the item there would be replaced by whatever you're using for your notes, that could be numbers, or strings with the note names, or what have you. I'm using the note names that I chose in the keymapper.)

The net result is that my piano array will end up as a long list of note names, with a new one added each time the player hits another note.

Now, I have a parallel event (Piano Listener) which will run if I'm checking to see if a song being played matches the right notes. It uses an If statement to take a slice of the last N notes in the array and check they match the N notes of my song (I did this a while ago, so I'm not quite sure why I convert both sides to strings; maybe I couldn't get the if to work otherwise!). For example, if it was 20 notes long, you might have it:

Code:
If: Script: $gameVariables.value(45).slice(-20).toString() == ['a', 'g', 'g', 'f', 'g', 'gsharp', 'asharp', 'gsharp', 'g', 'gsharp', 'f', 'c', 'd', 'f', 'gsharp', 'csharp', 'd', 'fsharp', 'g', 'gsharp'].toString()

(And replace the song string on the right of the == there with whatever you're using for each note, so that could be numbers or what have you). The .slice(-20) is taking a slice of the last 20 notes added to the array, so the player can mess around and hit other notes before beginning, and it will always check the most recent 20 notes they've played. The .toString() on both sides is converting both sides to strings before it compares them, for some reason.
You could store each song's notes as digits of a number and extract them using the Mod operator...but the Number type in JavaScript is only precise to 16~17 digits, so a 50+ note song wouldn't work with that approach (at least, not with only 1 variable).

You can use a script call to store an "array" in a variable, e.g.
  • Control Variables -> Script: [1,3,5,7,5,3,1]
You can then access each value via script call, by specifying its index, i.e. its position in the array.
JavaScript:
$gameVariables.value(123)[0]
This gets variable ID 123, and returns the value stored at index 0, i.e. the first value (1). The next value is index 1 (3), then index 2 (5), etc. You may want to use a different variable for "current index", e.g.
JavaScript:
$gameVariables.value(123)[$gameVariables.value(456)]
Obviously, replace the variable IDs with something appropriate for your game~

Another array check that would probably come in useful is .length. This counts how many values are in the array, e.g.
JavaScript:
$gameVariables.value(123).length
E.g. the length of your example is 7. So if the index variable gets to 7 or more (because index starts at 0, not 1), you know that the entire song has been completed.

Technical information about arrays in JS can be found here:

Thank you, thank worked really well! And it was a lot easier then I expected. Great 😀
Your suggestion really makes this easy. So, back to transcribing songs...
Thank you, thank worked really well!
Yay! 😂

(I did this a while ago, so I'm not quite sure why I convert both sides to strings;
Object equality applies by reference, e.g.
  1. [1,2,3] == [1,2,3] is false, because the arrays on the left and right are independent objects.
  2. var a = [1,2,3]; a == a is true, because the left and right refer to the same object.
To check "do these arrays contain the same values", you need to check each pair of values manually, typically with a loop. The MV/Z core scripts define an equals method on Array to make this easier, simplifying the check to [1,2,3].equals([1,2,3]) (true).

By contrast, string equality applies by value: [1,2,3].toString() returns "1,2,3", and "1,2,3" == "1,2,3" is true.

Original thread: https://forums.rpgmakerweb.com/threads/need-help-with-variables.180196/