|  | 
 An array is kind of like a cupboard with a bunch of spaces for cups. Each space is numbered and can be filled or empty. If you compared a cupboard with 10 cup holes to an array you would do this. 
 To access a cup you would do this. 
 Now an array can be filled with integers as above or strings or floats (floating integer). 
 Arrays are extremely useful for storing simular information. They are also easy to access and read/write to. 
 The above bit of code would set all 10 integers in the cups array to 0 or if it were a cupboard they would all be empty. Oh BTW in Blitz arrays are automatically global. Ok hopefully that is understandable. Now for types, a type is a storage area for a bunch of different but inter-related stuff. Kind of like a dresser. In a dresser you would throw shirts, pants, socks etc. Well a type is simular to that. 
 Ok now that tells the program what is in the type. Pant is an integer, Shirt is a string, and sock is a float. Even though it is defined it does not exist. Next we need a tool to use to open and work with the Type so we define a variable. 
 Notice I made this global, I did that so it could be accessed form anywhere in the program. Now lets make our type exist. 
 Now it exists in memory. Now we need something in it. 
 Now we have values in each of them. To use these values we do stuff like this. 
 The above is used in an if statement to decide if we should call the function DoSomething() 
 This would print "This is a string" to the screen. 
 Would print "The cost of this item is $77.13" to the screen. Ok now to get rid of a type is really simple. 
 It is gone. :) Ok these examples were not very game oriented but they were the most appropriate way I could think of to explain arrays and types. Now for a bit of realism with regard to games. :) 
 Now lets say we want 100 aliens we can store them in an array. 
 Now we want them all created and X and Y filled with random values. 
 What that does is make 100 aliens and store them in an array for easy access. Now lets draw them on the screen. 
 Ok now your ship is shooting them down. When it does your just turn alive off and it will not be drawn any longer. 
 and of course to delete them 
 Ok that is all the basics of arrays and types. There is one other thing cool about types. You can make a bunch of the same thing. Then you can access them all with a single loop without it being in an array like I did above. 
 SPECIAL NOTES (If whats below is too confusing don't worry about it, coding in the above manner will work fine) I have a minor but could be important addition that was brought up recently. Every language I have ever used, uses a 0 to index-1 format for arrays. In the above tutorial I explained it that way (Helps build good standard coding practices). For example 
 Please note the index is 0 to 9 in the above example. However Blitz has an unusual feature in the fact that it's arrays are actually one larger than they are dimensioned (Dim) to be. So in fact Dim Cups(10) creates 11 elements in the cups array. Blitz Basic is very unique in this aspect since it is the only language I have ever seen that does it. Anyway using the same code as above we could do this : 
 and it would still be correct just having 11 cups. REMEMBER this feature is totally unique to Blitz Basic and I don't know if it is a permanent part of Blitz or just a bug not yet fixed. You will NOT find it in any other language that I know of. So you should keep in mind that every language uses the above tutorial with regard to arrays (only the syntax is different) but ONLY Blitz Basic uses the 0 to index. IMHO I would use the 0 to index format if my program was very short on memory (which should never happen because Windows swaps memory out to disk as needed). Since this is unique it could really cause bugs if your switching back and forth between Delphi, C++ and Blitz. Still it does not hurt to know that it is possible. :) 
 For a printable copy of this article, please click HERE. 
 This site is Copyright© 2000-2004, BlitzCoder. All rights reserved.   |