Arrays

Variables may be put into arrays, which allows you to group data of the same kind together. Arrays can only have constant sizes, and can only be declared in global scope (outside of functions in system VC code.)

int numbers[15]; // Makes a list of 15 integers
str names[7]; // This one is a list of 7 strings

// The following is illegal:
int foo = 17;
// Verge does not allow this, because foo is a variable, not a constant.
int illegal[foo];

// However, using a define will work out expected:
#define FOO 17
// This is allowed, because FOO is 17 and won't change.
int legal[FOO];

Arrays are defined with the number of elements that they contain, but when accessing the elements you start counting with 0. So if you have 15 elements, they are numbered 0 through 14.

Once you've declared an array, you can set the elements of that array inside a function. So, using the above example (with numbers[15] declared), we can do stuff like this:

// This sets the first member of 'numbers' to 42.
numbers[0] = 42;
// This sets the last member of 'numbers' to 3.
numbers[14] = 3;

// The following line will do very very bad things
// because it is setting data outside of an array's bounds.
// A negative index is always outside of an array
numbers[-1] = 3;
// Likewise any index >= size of the array is invalid.
numbers[10000] = 123;

Note that the VC compiler can't check if an array access will be invalid ahead of time, so it will either cause runtime errors or fail silently. Therefore, you need to be careful to ensure you only access valid indexes of the array, or horrible things happen.

Arrays can also be created in multiple dimensions:

// This creates a 2-dimensional array of ints
int mock_screen[320][240];

// This creates a 3-dimensional array of strings.
string goats[100][100][100];

// This makes a 10-dimensional array of ints.
// making a multi-dim array this big is stupid, 
// but possible.
int tacos[5][4][17][22][79][34][11][19][7000][2];

This is how you assign elements inside of a multi-dimensional array.

// This sets element 0,0 of the array to 12
mock_screen[0][0] = 12; 
// This sets element 123,79 of the array to 43
mock_screen[123][79] = 43;
// This sets element 0,41,12 of this array to "Toothgnip".
goats[0][41][12] = "Toothgnip";

You cannot set an array instance equal to another array instance. You may only set array elements. To wit, this won't work:

int list_a[100];
int list_b[100];

void AutoExec()
{
    list_a = list_b; // Won't work!
}

So to get around this, you might consider using a for loop and copying items piecewise:

int i;
for(i = 0; i < 100; i++)
{
    list_a[i] = list_b[i];
}
Talkback

Post a new comment?

Talkback #5 written by anonymous on 2005-08-27.

A good way to refer to the dimensions of a 3D array: // X Y Z int coords[100][100][100] // width height depth // or length | / | / ---------#--------- X / | / | Z Y (crude, but what can I say)

Talkback #4 written by jrhee on 2005-04-04.

myArray.length would be nice...

Talkback #3 written by anonymous on 2004-09-26.

I haven't used this version of the engine yet, but I expect that (as in every other programming language I've used) you are not allowed to make arrays with fractional numbers of elements. A single-dimensional array is like a list of numbers. Like this: int list_a[10]; would create an arrangement of integers like this: 0 1 2 3 4 5 6 7 8 9 A two-dimensional array is like a grid of numbers. Like this: int list_b[5][5]; would create an arrangement of numbers like this: (0,0) (0,1) (0,2) (0,3) (0,4) (1,0) (1,1) (1,2) (1,3) (1,4) (2,0) (2,1) (2,2) (2,3) (2,4) (3,0) (3,1) (3,2) (3,3) (3,4) (4,0) (4,1) (4,2) (4,3) (4,4) which would be accessed like this: list_b[0][3] = 20; // Sets the element (0,3) - the element at the fourth column, first row - in the grid above equal to 20. Three and higher dimensions are difficult to type out in table format, but it's pretty much the same. Imagine a cube or 3D rectangle of elements, with each element given a unique coordinate based on its position along the length, width and height of the cube. If list_c is a 3D array, then list_c[3][5][10] is the element in the third column, fifth row, tenth layer up or down...or however you want to interpret it, as 'row', 'column' and 'layer' are all relative to how you want to imagine the cube being laid out.

Talkback #2 written by gamer83 on 2004-09-16.

I don't understand the elemts when creating 3d or 2d integers, can someone please explain?

Talkback #1 written by gamer83 on 2004-09-16.

I have a question, If your fixed point is 100 in tenths, your first array would then be number 0, and last would be 9 or 100?

Post a new comment?

Doc Nav

Your docs
View All Docs

If you log in, you can edit the documentation, or create your own documents and tutorials!

Ben McGraw's lovingly crafted this website from scratch for years.
It's a lot prettier this go around because of Jon Wofford.
Verge-rpg.com is a member of the lunarnet irc network, and would like to take this opportunity to remind you that regardless how babies taste, it is wrong to eat them.