Give yourself arrays
Posted: Mon Sep 26, 2011 10:33 am
Arrays are a pretty useful construct, and Bee Ant and I figure that talking about them might help you become more advanced in your skills. I'll start simple so that anyone that needs to catch up can.
For that reason we will use code written in the global code rather than the GE menu variable maker. To create an array of integers, type the following into the global script window.
This creates a global array of type int, named enemyHealth and gives it room for 5 integer numbers. You can create other types of arrays such as float, double, char and even Actor. We will deal with type actor last. You can also create 2d, 3d, 4d or any dimension of array, but 1d is the most flexible.
Arrays start counting(called the index) at zero. So that means that the last index is actually represented by 4. If you try to write outside the array you will get an out of bounds error.
You can set an array value by referencing it with a number after the name. Like this:
and you can extract it a similar way.
When you first create an array, its cells will be filled with random numbers(and not reliable random numbers either). So its smart to initialize them. I'll show you how.
So that is the basics of arrays. Lets see what Bee adds.
For that reason we will use code written in the global code rather than the GE menu variable maker. To create an array of integers, type the following into the global script window.
- Code: Select all
int enemyHealth[5];
This creates a global array of type int, named enemyHealth and gives it room for 5 integer numbers. You can create other types of arrays such as float, double, char and even Actor. We will deal with type actor last. You can also create 2d, 3d, 4d or any dimension of array, but 1d is the most flexible.
Arrays start counting(called the index) at zero. So that means that the last index is actually represented by 4. If you try to write outside the array you will get an out of bounds error.
You can set an array value by referencing it with a number after the name. Like this:
- Code: Select all
enemyHealth[1] = 500;
and you can extract it a similar way.
- Code: Select all
critterHealth = enemyHealth[1];
When you first create an array, its cells will be filled with random numbers(and not reliable random numbers either). So its smart to initialize them. I'll show you how.
- Code: Select all
int enemyHealth[5] = {1, 2, 3, 4, 5};
So that is the basics of arrays. Lets see what Bee adds.