This tutorial will introduce you to the concept of the index.
What is an Index?
An index is a number that refers to something.
Wah?
An index is a number that refers to something.
For example, let's say we have two unfortunate people, Bob and George. We can stick Bob in garbage can 1 and George in garbage can 2. When the lid of the garbage cans are closed, we can't really tell who is in each garbage can, but we can refer to each garbage can as 1 or 2.
1 is the index of the first garbage can.
2 is the index of the second garbage can.
Both Bob and George are pretty stinky. This doesn't matter. :)
Indexes in PlayBasic
PlayBasic uses the hell out of some indexes. The first being in arrays.
Array Index
An array is like several garbage cans lined up side by side.
The array index is the number which refers to a specific element (or garbage can) in an array.
Let's stick Bob and George in the garbage cans again, shall we?
LoadFont "Verdana", 1, 16, 0 ; Don't worry about this line for now
Dim GarbageCans$(2) ; create the array of garbage cans
GarbageCans$(1) = "Bob"
GarbageCans$(2) = "George"
Print "In garbage can 1: " + GarbageCans$(1)
Print "In garbage can 2: " + GarbageCans$(2)
Sync
WaitKey
Both Bob and George are pretty stinky. This doesn't matter. :)
Font Index
PlayBasic uses indexes to keep track of resources such as images, fonts, and sprites that have been loaded.
In the previous example, you saw this line :
LoadFont "Verdana", 1, 16, 0
This loads the font named Verdana into font index 1 at a size of 16 with no style 0.
1 is the default font.
Getting Indexes
PlayBasic has functions to get a free (unused) index so you don't have to keep track of the indexes you've already used.
For fonts, the function is GetFreeFont().
You can store the index in a variable, and not even have to know the number used as an index.
myFontIndex = GetFreeFont()
Storing the index in a variable also gives you a easy to remember name to use rather than a number.
bigFont = GetFreeFont()
smallFont = GetFreeFont()
Loading Stuff into Indexes
When you get a free index using a function like GetFreeFont(), you are getting an empty index.
Because it's empty, you need to stuff Bob or George in it… just kidding… sort of.
You need to then load something into it.
bigFont = GetFreeFont()
LoadFont "Verdana", bigFont, 50, 0
smallFont = GetFreeFont()
LoadFont "Verdana", smallFont, 10, 0
Using Indexes
You can use indexes for whenever you want to refer to something loaded into an index, such as the example above.
To use a font for displaying text, you use the font index to tell PlayBasic which font to use.
bigFont = GetFreeFont()
LoadFont "Verdana", bigFont, 50, 0
smallFont = GetFreeFont()
LoadFont "Verdana", smallFont, 12, 0
SetFont bigFont
Print "Big Font"
SetFont smallFont
Print "Small Font"
Sync
WaitKey
It Gets Easier
For most things that use indexes, PlayBasic has a combo function that gets a free index and loads something into it.
For fonts, the function is LoadNewFont()
bigFont = LoadNewFont( "Verdana", 50, 0 )
smallFont = LoadNewFont( "Verdana", 12, 0 )
SetFont bigFont
Print "Big Font"
SetFont smallFont
Print "Small Font"
Sync
WaitKey
Conclusion
Congratulations. You know all you ever need to know about indexes in PlayBasic. And you got to stuff a couple of unfortunate people in a couple of even more unfortunate trash cans.
| Categories: Beginner Tutorials : Programming Tutorials : Tutorials |