Monday 9 April 2012

Object List - Set up

We now have a source for the images for our objects, and have created a single object.

The Idea of the game is to have multiple object on the work area that can be combined and so to do this we need to keep a list of the objects currently in play.

As you might expect, this is done using another array.

Though an array is a list of objects, these are used one at a time, referenced by the array index value and so I prefer to name the array so that the code reads in way that shows we are talking about a single object.

The array will be called workObject[], so that in the code we will access an object using workObject[ index ]. This is purely symantics, but I feel it makes the code more like English so reads better.

Since we don't know how many objects will be used at any one time, initially we will assign an arbitrary value as a maximum.  If at a later time we decide we need more, we can change this.

As with other values we might want to change, a constant is defined at the start of the program

#constant MAX_WORKOBJECTS 100

Since we will need to store multiple values for each of these objects, such as the object value and the sprite used for the image, the array will use a User Defined Type (UDT) for it's contents.

This is again defined at the start of the program, after the definitions for the panel (which are a permanent part of the program) and before the set up for the finger sprite and test icon (which are not).

type WorkObjectType
   isFree
   objectID
   sprite
endtype

Here I have included an additional field called isFree, which will indicate if the entry in the array is free for use.  This way, if an object is removed from the middle of the list, this can be set to 1 and we don't have to worry about moving the other objects to fill the gap.

Now we dim the array using the constant as the size, and specifying the UDT definition to show the content type.

dim workObject[ MAX_WORKOBJECTS ] as WorkObjectType

It's a good idea when creating an array to perform a loop, counting through the array, setting the initial values for all the fields.

for thisObject = 1 to MAX_WORKOBJECTS
workObject[ thisObject ].isFree = 1
workObject[ thisObject ].objectID = 0
workObject[ thisObject ].sprite = 0
next thisObject

Finally we set up a global variable called numWorkObjects to hold the current number of objects in play

global numWorkObjects = 0

This sets the initial value to zero as we initially have no objects in the list.
The array is ready to use.

While we can just use the array as is, to keep the program tidy and save on duplication, we are going to have a couple of functions to check index values to prevent errors in the future.  As always, these go after the do/loop part of the program.

The first one simply returns the index of the top object in play.

function TopWorkObject()
endfunction numWorkObjects

Which it simply gets from the global variable numWorkObjects.

The next one is a validation routine, which checks to see if an array reference is valid.

function WorkObjectValid( thisObject )
   thisBool = ( thisObject > 0 and thisObject <= TopWorkObject() )
endfunction thisBool

This uses a local variable thisObject which will receive the parameter passed ( the array index) and checks to see if it is between 1 and the number of objects we currently have in play - which comes from TopWorkObject().

This uses a particular style of programming called Boolean Algebra, which is slightly more efficient than using and if/else/endif loop but works along similar lines.

The condition - the bit which would normally go after the if statement - is put in brackets, and is passed to a variable in the normal way using = .

This is evaluated in the same way as an it would be in an if statement, but the result will either be true - which returns a 1, or false - which returns a 0.

So if the value passed to the function is greater than 1 and less than or equal to the top work object (ie the value in numWorkObjects), then the variable thisBool receives a 1, otherwise it receives a 0.

The endfunction returns this boolean value (hence the name thisBool) back to the instruction which called it.
The function is then used as the condition for an if statement elsewhere in the program and because of the name used, reads very much like English. For example

if WorkObjectValid( myObject )
   { instructions to perform if object is valid }
endif

I like to keep my code easy to read.

No comments:

Post a Comment