swiftdeathsk
Member
This is just a short tutorial explaining what arrays are, and just how to use them in RGSS.
-Difficulty: 2/5 (easy)
-Requires: Basic alegbra knowledge, and basic scripting knowledge.
What is an Array?
An array is basically a group of numbers, strings (words/text), or even variables that can be designated to a variable or used as it is.
Think of an array as a book. There are several pages to a book, but it is all still part of the book. For example, if you had a very short book with 4 pages, but page 2 was missing, you would have 3 pages left, and if you place the page numbers into an array (in order, but they do not have to be), it would look like [1, 3, 4]. Let's throw in the variable part i mentioned. For this, let's give the book a title, how about "Learning_Arrays", and that will be the name of the variable we assign it to. If we want to assign the array of [1, 3, 4] to our book's name, we just need to set it to the variable, like this:
Learning_Arrays = [1, 3, 4].
Now, if we want to call upon that array, we can use the variable Learning_Arrays instead of typing out the whole array.
Here is a list of the indexes of the elements (remember - elements are the objects in the array) inside our new array:
Index = Value:
0 = 1 (because arrays start with 0 as the first index of the elements. Computers just seem to count starting at 0 more than they do at 1, I'm not completely sure as of why, but it's just one of those things you will have to remember)
1 = 3
2 = 4
How do I use an Array in RGSS?
Once you have an array set to a variable (you can also just use it without a variable, but whenever we don't use the variable, you will need to write out the whole array, which is [1, 3, 4]), you can use it in equations and other scripting elements.
First, let's learn how to use a certain number from an array without typing the actual number (this is useful for when you have variables as the array elements, because they can change). I won't go over every scripting command, just a few. (If you need more explaination on these scripting commands, just open up the RMXP help file and click on Arrays in the keywords list.).
Let's start with the self[nth] command. Say we want a command to find out what the 2nd element (remember, the elements start with 0, so this would be the 3rd number, which is 4 because [1, 2, 4] is the 3rd number, and 2nd element) in our Learning_Arrays is, then store that element to another variable, we'll call this one "A_Number". Instead of saying A_Number = 4, what if the page number magically changed somewhere in the code before you set A_Number to it? Well, we can use the self[nth] command to find out what number is in the 2nd element. It would look something like this:
A_Number = Learning_Arrays.self[2]
I'll break that down for you:
What this does is self[2] accesses the variable Learning_Arrays, which is where our array is stored, then it searches for the 2nd element (once again, remember that the elements in an array begin with 0), which is 4 ([1, 3, 4), and then stores that value to A_Number.
Let's go on to another scripting command for arrays, this time we will be learning how to use self[start, length]. What self[start, length] does is it finds the values of multiple elements in an array, starting at the start element, and returning an array of the numbers between start and length.
For example, say we want to find the first 2 elements (0 & 1 to be correct) and store that into a variable called "Another_Array". We would use this line:
Another_Array = Learning_Arrays.self[0, 2]
I'll break that down:
What this does is it accesses the array in the variable Learning_Arrays, and finds the 0th (th? lol) element and then returns the next 2 elements (this includes the start element), which would be an array of [1, 3], and then stores the new array to the variable Another_Array.
Let's go over one more scripting command, and for the rest you can just look in your RMXP help file. This time, we will learn the self[nth]=val command. Basically what self[nth]=val does is allows you to change the value of an element in an existing array. Let's take the new variable we just created, Another_Array, and take the value of A_Number we created earlier, and make the first (0th) element in the array of Another_Array become the same value as A_Number. Confused? Don't worry, I'll break it down in a second. Let's go onto the line of code first:
Another_Array.self[0] = A_Number
Very simple, actually. Now I'll break it down:
What this does is first accesses the array stored in Another_Array (which is [1, 3]), then finds the 0th element, which is 1, and then changes that value to the value stored in the variable A_Number, which is 4. The new array for Another_Array would be [4, 3]. See? It's not really that hard at all!
If there is something I missed or explained wrong, please post and I'll update it
-Difficulty: 2/5 (easy)
-Requires: Basic alegbra knowledge, and basic scripting knowledge.
What is an Array?
An array is basically a group of numbers, strings (words/text), or even variables that can be designated to a variable or used as it is.
Arrays are set inside brackets, most commonly in [ ]'s.sephspawn":39btnlii said:The "objects" inside you array are called elements. They are organized by indexes, that is, their position in the array. The first element in your array is at index 0 and increases by 1 each following object.
Think of an array as a book. There are several pages to a book, but it is all still part of the book. For example, if you had a very short book with 4 pages, but page 2 was missing, you would have 3 pages left, and if you place the page numbers into an array (in order, but they do not have to be), it would look like [1, 3, 4]. Let's throw in the variable part i mentioned. For this, let's give the book a title, how about "Learning_Arrays", and that will be the name of the variable we assign it to. If we want to assign the array of [1, 3, 4] to our book's name, we just need to set it to the variable, like this:
Learning_Arrays = [1, 3, 4].
Now, if we want to call upon that array, we can use the variable Learning_Arrays instead of typing out the whole array.
Here is a list of the indexes of the elements (remember - elements are the objects in the array) inside our new array:
Index = Value:
0 = 1 (because arrays start with 0 as the first index of the elements. Computers just seem to count starting at 0 more than they do at 1, I'm not completely sure as of why, but it's just one of those things you will have to remember)
1 = 3
2 = 4
How do I use an Array in RGSS?
Once you have an array set to a variable (you can also just use it without a variable, but whenever we don't use the variable, you will need to write out the whole array, which is [1, 3, 4]), you can use it in equations and other scripting elements.
First, let's learn how to use a certain number from an array without typing the actual number (this is useful for when you have variables as the array elements, because they can change). I won't go over every scripting command, just a few. (If you need more explaination on these scripting commands, just open up the RMXP help file and click on Arrays in the keywords list.).
Let's start with the self[nth] command. Say we want a command to find out what the 2nd element (remember, the elements start with 0, so this would be the 3rd number, which is 4 because [1, 2, 4] is the 3rd number, and 2nd element) in our Learning_Arrays is, then store that element to another variable, we'll call this one "A_Number". Instead of saying A_Number = 4, what if the page number magically changed somewhere in the code before you set A_Number to it? Well, we can use the self[nth] command to find out what number is in the 2nd element. It would look something like this:
A_Number = Learning_Arrays.self[2]
I'll break that down for you:
What this does is self[2] accesses the variable Learning_Arrays, which is where our array is stored, then it searches for the 2nd element (once again, remember that the elements in an array begin with 0), which is 4 ([1, 3, 4), and then stores that value to A_Number.
Let's go on to another scripting command for arrays, this time we will be learning how to use self[start, length]. What self[start, length] does is it finds the values of multiple elements in an array, starting at the start element, and returning an array of the numbers between start and length.
For example, say we want to find the first 2 elements (0 & 1 to be correct) and store that into a variable called "Another_Array". We would use this line:
Another_Array = Learning_Arrays.self[0, 2]
I'll break that down:
What this does is it accesses the array in the variable Learning_Arrays, and finds the 0th (th? lol) element and then returns the next 2 elements (this includes the start element), which would be an array of [1, 3], and then stores the new array to the variable Another_Array.
Let's go over one more scripting command, and for the rest you can just look in your RMXP help file. This time, we will learn the self[nth]=val command. Basically what self[nth]=val does is allows you to change the value of an element in an existing array. Let's take the new variable we just created, Another_Array, and take the value of A_Number we created earlier, and make the first (0th) element in the array of Another_Array become the same value as A_Number. Confused? Don't worry, I'll break it down in a second. Let's go onto the line of code first:
Another_Array.self[0] = A_Number
Very simple, actually. Now I'll break it down:
What this does is first accesses the array stored in Another_Array (which is [1, 3]), then finds the 0th element, which is 1, and then changes that value to the value stored in the variable A_Number, which is 4. The new array for Another_Array would be [4, 3]. See? It's not really that hard at all!
If there is something I missed or explained wrong, please post and I'll update it