BS2 CodingHelp ARRAYS

From FSG Wiki

Revision as of 17:23, 11 April 2008; view current revision
←Older revision | Newer revision→
Jump to: navigation, search

BS2 ARRAYS

An array consist of a series of variables that can contain any number of variable of the same name. For example, if you want to keep to coordinate of 4 points in the BS2 sandbox. Instead of creating 4 variables called for example, "MyCoord" for the X's and 4 for the Y's, you can use Arrays to define them. You would use this normally:

MyCoordX1 = 10
MyCoordY1 = 10
MyCoordX2 = 50
MyCoordY2= 50
MyCoordX3= 90
MyCoordY3= 90
MyCoordX4= 150
MyCoordY5= 150

You should use this to create an array:

MyCoordX[1] = 10
MyCoordY[1] = 10
MyCoordX[2] = 50
MyCoordY[2] = 50
MyCoordX[3] = 90
MyCoordY[3] = 90
MyCoordX[4] = 150
MyCoordY[4] = 150

This may not look like a big difference, but the main difference is when you want to access them again. So now let's just say i want to draw a point of Earth at the 4 coordinates.

This would be the normal way:

DRAW ELEMENT:Earth POINT MyCoordX1 MyCooordY1
DRAW ELEMENT:Earth POINT MyCoordX2 MyCooordY2
DRAW ELEMENT:Earth POINT MyCoordX3 MyCooordY3
DRAW ELEMENT:Earth POINT MyCoordX4 MyCooordY4

And this would be with Arrays:

FOR i FROM 1 TO 4 <DRAW ELEMENT:Earth POINT MyCoordX[i] MyCooordY[i]>

You see the difference, now just imagine saving 100 points instead of 4, that would make 100 lines just to draw them back. With arrays it will still just 1 line, because only the "1 TO 4" part would change to "1 TO 100". But with this example you would still have to define all 100 points coordinate by yourself, so lets take a better example ! And this time it is only showing using arrays !

Let's take an example on my starfield screensaver. It use arrays to store all stars coordinate. So let's start by looking at the trigger that assign value to the arrays:

Stars = 250
FOR i FROM 1 TO Stars DO {    
 CoordX[i] = (0 RAND (WIDTH + 50)) 
 CoordY[i] = (0 RAND HEIGHT) 
  
 DRAW ELEMENT:Star POINT CoordX[i] CoordY[i]
}
  • First i assign the maximum number of stars, wich is 250.
  • Then i start a loop, going from 1 to 250.
  • Then i start to assing random X and Y coordinate until the array is completed
  • The arrays use the "i" as index. Therefor, they will be from 1 to 250.

Now that all star are assigned a x,y coordinate, a colour and speed, we can now draw them on the screen. Again to do this, it will only take one line of code:

FOR i FROM 1 TO Stars <DRAW ELEMENT:Star POINT MyCoordX[i] MyCooordY[i]>

So this is it, instead of using 250 lines, multiplied by 2 for x and y value, this make 500 lines of code to assign value. I think you can see that using arrays can be very efficient, but it more when playing around with lots of same variables. The main purpose of arrays are to reduce the code size and prevent some painful copy and paste.


Conclusion

Arrays are best used when playing with lots of variables. But even with as low as 2 vars of the same kind, it think you should use an array, as if you ever get to use more, then you dont have to change all your code, just the upper limit of the array.

Simply declare an array like you would do for a variable, but add the [] and the index in between. The index is the number you can retrieve your variable with. Just for a quick example this is what BS2 do when using delaring an arrary:

MyArray[50] = 100

BS2 will create a variable called "MyArray50" and put 100 as its value. This can be verified using my BS2 Editor !

Have fun with arrays now :)

Personal tools