Envision, Create, Share

Welcome to HBGames, a leading amateur game development forum and Discord server. All are welcome, and amongst our ranks you will find experts in their field from all aspects of video game design and development.

Mini-Tutorial: Arrays (Difficulty: 2/5)

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.
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.
Arrays are set inside brackets, most commonly in [ ]'s.

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 :)
 
There is some good information here, but it isn't the best organized. Just at a glance, those are some very big paragraphs. Now in descriptive writing, that is good, but teaching, you will lose the attention of your reader. More or less, they will get lost in the reading, see it an cringe, etc.

The numbers/strings/variables inside the array are called elements (no, not like fire and water ), starting with 0 (zero) as the first element in the array.

That is a bit unclear.

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.

Things like that make a big difference.
 
swiftdeathsk":135qhltw said:
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),
...I don't get it...lol
If it begins with 0, why isn't the second element = 1?...heck why isn't it 3 because it's second in the list?
 

Anonymous

Guest

That is worded in a confusing way.

The second element's index should be 2, and its value is 3. However, the value of the element at index 2 is 4.
 
erm....here let me..... [23,54,1,24,66,33] that stuff can be made by you to be whatever you want.

0=23
1=54
2=1
3=24
4=66
5=33

the 0 is the first number as the array is counted by the computer...computers seem to like counting 0 as the first number......

think of it like with centuries....right now year 2007 its century 21 [the 21st century]
the fisrt century began at....
0000 not 0100....its a slightly different way of counting....its bloody confusing! but thats how it is....
 
but why is index 2 = 4 if it is the last number?....*smacks self**yes I do that a lot!*
lol okay so in this case
0 = 1
1 = 3
2 = 4
 

Anonymous

Guest

They don't "like" it, it has to do with the way arrays are created in memory. Arrays are sequential in memory, like so:

Code:
--------------------
|   |   |   |   |   |
--------------------

The array variable points to where the array starts in memory, the index is the offset from that starting point. So the first item in the array is at the starting point plus 0, the second item is at the starting point plus 1 (times the size of the element in bytes).

Think of it like beads strung on a string or a row of cubbies.
 
i have some questions.

1. i thought {} were hashes.
2. what common uses would you need an array for.
3. zeroeth should be made an internationlly recognized word dont u think :D
 
asan;164217 said:
i have some questions.

1. i thought {} were hashes.
2. what common uses would you need an array for.
3. zeroeth should be made an internationlly recognized word dont u think :D

1. hmm... lol i guess they could be, i dont really remember...
2. for lists of values stored to a single variable. it's much easier and faster to create an array for a single variable than to have to manage a ton of different variables. i've seen it used in tons of scripts laying around these forums, along with many other places. and arrays arent just for Ruby, arrays are found in every scripting language (that i know of). arrays are even in real life, hence why you learn what arrays are in math class.
3. yeah, i guess so... never have known what to add to the 0 when talking about a position... 0th works for me though
 
ToriVerly;169407 said:
I used .self on my array and I got 'undefined method "self" for MyArray:Array'
It worked without the self though--MyArray


odd... i used the .self addition in my own project, and it worked fine... are you sure you used the syntax right?
 
@swiftdeathsk
I had:
Code:
POSES = ['_idle', '_yawn', '_blink']
i = rand(POSES-1)
@pose = POSES.self[i]
and I got an error. My friend said to change it to POSES and that worked.

PS. if the rand(POSES-1) thing is bad syntax, I don't have the script right in front of me but that's what I think it was.
 
Use this for getting a random element from an array:
Code:
POSES = %w( _idle _yawn _blink )
@pose = POSES[rand(POSES.size)]
The %w( ) is just a quick way to do string arrays. Saves keystrokes.
 
ToriVerly;163745 said:
...I don't get it...lol
If it begins with 0, why isn't the second element = 1?...heck why isn't it 3 because it's second in the list?

The second element in the example [1,3,4] (or whatever it was) is 3, the index of that element is 1. By claiming the second element was four, he meant the third element, index 2.
 

Thank you for viewing

HBGames is a leading amateur video game development forum and Discord server open to all ability levels. Feel free to have a nosey around!

Discord

Join our growing and active Discord server to discuss all aspects of game making in a relaxed environment. Join Us

Content

  • Our Games
  • Games in Development
  • Emoji by Twemoji.
    Top