Original

You are currently reviewing an older revision of this page.
Go to current version

Unlike its older siblings Microsoft Visual Basic and Microsoft Visual C#, Microsoft Small Basic does not support the explicit creation of classes and structures, although the built-in objects are very easy and intuitive to use. 

However, when creating video games in Small Basic, sometimes there may be a need to create a template for the various characters in game and then use objects that are based on that template. For example, in an airplane/flying type of game where the enemy fighters are all the same, how can we create and manipulate, say 25 enemy planes, without duplicating code? In a side-scrolling game where the main characters share many (if not most) of the same qualities, how can we create a template to base them on? In VB or C# we can uses classes. Here is a way of duplicating this functionality in Small Basic by using arrays and array indirection. Here is an example (feel free to copy and paste the code into Small Basic):
 

'Step 1: Setup a 1D array to represent the template for our game character. Each element 'in this template will represent a different property of the game character:
 

SpriteType["ScreenX"]=""
SpriteType["ScreenY"]=""
SpriteType["State"]=""

 

'Step 2: Create "new instances" of the "type" by creating
'references that will point to duplicates of our template. The copies are
'created using Array.GetAllIndices():
 

MrMario = Array.GetAllIndices(SpriteType)
MrLuigi = Array.GetAllIndices(SpriteType)
 

'Step 3: Initialize and use our new objects via indirection:

MrMario["State"] = "Alive"
MrLuigi["State"] = "Dead"
 

'We can enumerate over each individual object and get or set data:

TextWindow.WriteLine(MrMario["State"])
TextWindow.WriteLine(MrLuigi["State"])
 

'Let's store our objects in a list. This would be useful if our game has many game characters
'(like cars or planes) that are the same or very similar:

ListOfSprites[0] = MrLuigi
ListOfSprites[1] = MrMario
 

'Enumerate over our list and operate on our objects via indirection. This would be useful, 'say, when moving planes or cars around the screen. Because each object is based on the 'same template, we can eliminate code duplication when operating on our objects:
 

For I = 0 To 1
TextWindow.WriteLine(ListOfSprites[i]["State"])
EndFor