The RPG Maker Resource Kit

Other Game Creation => Other Game Making Programs => Topic started by: J-Crew on September 06, 2007, 12:29:26 AM

Title: [GM] Help with Arrays
Post by: J-Crew on September 06, 2007, 12:29:26 AM
I'm sure this is a dumb question...but for arrrays in GML, is it possible to see if an array contains a certain number and how are numbers added to an array? for example, if I had

array[0] = 0

how would I add a # to the array, and how can I tell if a certain # is in the array?

EDIT: Added global and local array details and predefined arrays.
Ok I think I got it  :tpg: I'm not completely sure though. I'll edit it if I learn anything else. I'm gonna try using 2-d arrays later. Ok here goes.

Unlike what I knew about RPG Maker Xp, arrays are not just a large set of numbers within bar brackets "[]" In GML, arrays have what are called indexes. These are sort of like the numbers in RMXP arrays, but each index has a value of it's own. For example:

Code: [Select]
new_array[1] = 2
new_array[2] = 3

This sets the index within the []'s to the number that is after the "=" sign. Arrays are automatically defined after []'s are placed. So in the above set, we defined an array called new_array and created 2 indexes for it. (1 and 2). Knowing this I guess what I asked in my first post is realtively impossible without the use of other variables and/or alot of work. But technically I guess the indexes are the #'s that are contained in the array, you just have to know which have already been defined already.

Arrays, when defined are local variables and can only be used in the object that they are created in, unless you add to syntax global. before the array name. For example:

Code: [Select]
new_array_1[0] = 1 //creates a local array with an index that is set to 1.
global.new_array_2[0] = 1 //creates a global array with an index that is set to 1.

// local arrays can also be set using the following syntax:

self.new_array_3[0] = 1 // creates a local array with an index that is set to 1

Ok, so to do what I asked for in my first post, you'd set up a code like this.
Code: [Select]
// This is assuming you have 10 indexes of the array "new_array",
// numbering 0-9, where 9 is the maximum number of indexes in the array.

i = 0;
while (i <= 9)
 if (new_array[i] = 1)
    // Expression that will be executed when the specific index of i = 1
i += 1; // continues on through the indexes of the array by adding 1 to i which is checked in the while loop above.

It is also important that you know that there are predefined arrays, such as alarm[]. If you were to put:
Code: [Select]
alarm[0] = 1

It would set the alarm of the object this code is placed in to 1. It would not create a local array called "alarm" because alarm is already predefined within Gamemaker.  Many of the background functions are predefined arrays as well.

Code: [Select]
background_index[0] = sky_1 //sets the first background to Sky_1

Many of the view functions are also arrays.
Code: [Select]
view_object[0] = Player_1 //bases the view on the object "Player_1"

Be careful to make sure that an array that you create is not predefined.
Title: Re: Help with Arrays
Post by: Arrow on September 08, 2007, 11:23:57 PM
Very nice. :D