Saturday, 21 April 2012

Object Storage 3

With the basic file read operation in place, we can now expand it's functionality.

As groups and objects are stored in the same array, we need to identify which is which.  Groups will be identified with a field called isGroup in the MainObjectType UDT.
type MainObjectType
   name$
   icon
   isKnown
   isGroup
endtype
As always, we provide a return function.
function ObjectIsGroup( thisObject )
   if ObjectValid( thisObject )
      thisBool = MainObject [ thisObject ].isGroup
   else
      thisBool = -1
   endif
endfunction thisBool
The code for this is almost identical to the ObjectIsKnown() function, which can be duplicated and only the function name and array field need to be changed.

We can identify groups without needing to change the data too much, by simply making the icon number for groups a negative value in standard.txt, which now looks like this.
tool,-1
resource,-2
device,-3
job,-4
process,-5
storage,-6
tree,7
stone,8
stone axe,9
log,10
kindling,11
spark,12
flame,13
fire,14
Now when the value is transferred to the Object array, in the line.
MainObject[ i ].icon = val( thisString$ )
We use the abs() command to get the absolute, or positive version of the value.
thisValue = val( thisString$ )
MainObject[ i ].icon = abs( thisValue )
This has been split into two parts to simplify the code and prepare for the next step, which is a check to see if the value is negative.
if thisValue < 0
   MainObject[ i ].isGroup = 1
else
   MainObject[ i ].isGroup = 0
endif
This goes immediately after the isKnown field is set and before the endif.

To test this works, we modify the test code.  Changing the print in the line.
print( str(i) + " : " + ObjectName( i ) + " : " + str( ObjectIcon( i) ) )
To printc, and adding the following lines immediately after.
print( " - Group: " + str( ObjectIsGroup( i ) ) )
Which now shows which objects are groups with a 1 at the end of the line.
The icon values stored in the array are unchanged.

Now the line just added is replaced with the following lines.
if ObjectIsGroup( i ) > 0
   print( ", Group: ")
else
   print( ", " + chr(34) + "" + chr( 34 ))
endif
Which sets us up for the next step - adding descriptions for the objects.
Which will appear inside those quotes.

No comments:

Post a Comment