Monday 9 April 2012

Object List - Tidy Returns

The main object functions are done, so it's time to tidy them up.

This is just a matter of adding functions to return the values from the array in the same format we use elsewhere.

The following functions are added to the end of the program.

// Return Functions - WorkObject Array

function WorkObjectIsFree( thisObject )
   if WorkObjectValid( thisObject )
      thisIsFree = workObject[ thisObject ].isFree 
   else
      thisIsFree = -1
   endif
endfunction thisIsFree

function WorkObjectID( thisObject )
   if WorkObjectValid( thisObject )
      thisObjectID = workObject[ thisObject ].objectID
   else
      thisObjectID = -1
   endif
endfunction thisObjectID

function WorkObjectSprite( thisObject )
   if WorkObjectValid( thisObject )
      thisSprite = workObject[ thisObject ].sprite
   else
      thisSprite = -1
   endif
endfunction thisSprite

These work in exatly the same way as the other return function we have used, but since they access an array, an additional check is used to make sure the value passed is a valid index for the array.

If it is not, then a value of -1 is returned, which indicates an error.

Now we change the references to the array fields used in the other functions to use these new functions.

In FirstFreeWorkObject(), we change the line

if workObject[ thisObject ].isFree > 0 then exit

to

if workObjectIsFree( thisObject ) > 0 then exit

In DelWorkObject(), we change the lines

if workObject[ thisObject ].sprite > 0
   thisSprite = workObject[ thisObject ].sprite


to

if WorkObjectSprite( thisObject ) > 0
   thisSprite = workObjectSprite( thisObject )

and change

while workObject[ TopWorkObject() ].isFree = 1 and TopWorkObject() >= 0

to

while WorkObjectIsFree( TopWorkObject() ) = 1 and TopWorkObject() >= 0

Some would argue that these changes are not necessary and add additional processing - the fact that it checks the object is valid numerous times - each time the array fields are accessed.

It's an small overhead I am willing to take to keep the code tidy.

Also in the case of statements such as

WorkObjectIsFree( TopWorkObject() ) = 1

the  = 1 is not actually necessary, since

WorkObjectIsFree( TopWorkObject() )
is still true (and in fact reads better), but there is an issue with AGK's true values which is awaiting a fix, so until that fix is done, I am going with the safe option.

Also we are only changing the places where the fields are read a this stage, not where they are changed.

No comments:

Post a Comment