Thursday, 19 April 2012

Object Storage

Now that we have read the object file, we need somewhere to put it.

This will get quite complex, so I'll start with the basics and expand on it as more data is added.

Firstly we need to know how many objects were loaded. This will be a new field called topObject in the GameType UDT, which now looks like this.
type GameType
   state
   level
   topObject
endtype
As always we create a return function for this.
function TopObject()
endfunction game.topObject
And set the initial value in the Initialise() function.
game.topObject = 0
And also after the closefile() command in the LoadObjectData() function.
game.topObject = 0
The array to store the game data will be called MainObject[].

It's structure is defined in another UDT.
type MainObjectType
   name$
   icon
   isKnown
endtype
Initially, this just includes fields for the object name and it's icon number, as well as the field isKnown, which indicates if the object is known about.

The array is dimmed with zero elements in the Initialise() function.
dim MainObject[0] as MainObjectType
Though it is empty, we can still set up the basic functions which will eventually be needed to access the array.  These are very similar to the ones used by the WorkObject[] array.

The validation function.
// Main Object Functions

function ObjectValid( thisObject )
   thisBool = ( thisObject > 0 and thisObject <= TopObject() )
endfunction thisBool
A function to locate an object by its name.
function FindObjectByName( thisName$ )
   thisName$ = upper( thisName$ )
   for thisObject = 1 to TopObject()
      if upper( ObjectName( thisObject )) = thisName$ then exit
   next thisObject
   if thisObject > TopObject() then thisObject = 0
endfunction thisObject
which compares the value passed against each array object's name field, after both have been converted to upper case. This ensures that the case used does not affect the search.

And the return functions for the array
// Return Functions - Main Object

function ObjectName( thisObject )
   if ObjectValid( thisObject )
      thisString$ = MainObject [ thisObject ].name$
   else
      thisString$ = ""
   endif
endfunction thisString$

function ObjectIcon( thisObject )
   if ObjectValid( thisObject )
      thisIcon = MainObject [ thisObject ].icon
   else
      thisIcon = -1
   endif
endfunction thisIcon

function ObjectIsKnown( thisObject )
   if ObjectValid( thisObject )
      thisBool = MainObject [ thisObject ].isKnown
   else
      thisBool = -1
   endif
endfunction thisBool
Everything is now in place to start filling the array with data.

As a final step, we add a print statement just after the games State line in PrintDiagnostics() function.
print("Objects : " + str( TopObject() ) + chr(10) )
So we can see at any time how many objects are loaded.

No comments:

Post a Comment