Thursday, 19 April 2012

Data Files

The information used by the game - the objects and recipes, needs to come from somewhere.

In theory, we could create a long list of constants in the program, or we could assign the data to an array as we did with the menu text, but neither of these methods is really practical.

The best solution is to store it in a separate data file, which is loaded when the program starts.  This way, the data can be added to or changed without needing to change the program.

This also opens up the ability to have multiple sets of objects and recipes, each with their own theme.

To make this easier, the task is split into two parts.
  • Read the data from the file
  • Convert the data to a usableform
This is the modular programming concept again, we can create a routine to read the data without knowing what format that data will be in and we can create a routine to convert the data without knowing where it came from.

Since objects have both a name and an image associated with them, we will need a data file to store each.  To simplify matters, both files will have the same name, but a different file extension.
  • The name and description will be stored in a text file with the extension ".txt"
  • The images will be stored in an image file...
While we have been using a ".png" file for our images so far, it is possible that we may want to support other file types such as ".bmp" and ".jpg".   This means that the file extension could be on of several.

We can allow for this by having a function which checks for several file types and returns the one present or an empty string if none are.
function FindImageFile( thisName$ )
   thisString$ = ""
   if getFileExists( thisName$ + ".png" )
      thisString$ = thisName$ + ".png"
   elseif getFileExists( thisName$ + ".jpg" )
      thisString$ = thisName$ + ".jpg"
   elseif getFileExists( thisName$ + ".bmp" )
      thisString$ = thisName$ + ".bmp"
   endif
endfunction thisString$
This takes a name string thisName$ and checks for the existence of files based on that name with a variety of known extensions. If it finds one, it stores the full name in thisString$ for returning.

Combined with a check for a file with a ".txt" extension, we can now check that both data files exist before we commit to reading them.
function LoadObjectData( thisName$ )
   // Pass the name without extension
   thisTextName$ = thisName$ + ".txt"
   thisImageName$ = FindImageFile( thisName$ )
   if getFileExists( thisTextName$ ) and thisImageName$ <> ""
      // Both data files exist - Start Read

   endif
endfunction
This is the outline for our data loading function. It takes a name (without file extension) and verifies that both files exist before attempting to load the data.

The text data will be read into a temporary string array - the same one used before, tempArray[]. As arrays are global, we must ensure we don't use the Base96Sort() function - which also uses this array, while we load the data or it will mess everything up.
// Both data files exist - Start Read
dim tempArray[ 1000 ] as string
thisArraySize = 0 
Since the file could be any size, we use a high value in the dim command just to be sure. The local variable thisArraySize will be used to indicate how much of the array has actually been used.

As before, when we have done with the array, we will dim it to zero, freeing up the memory used.

Before we can go any further, we are going to need some files.

For the image file, make a copy of the objects.png file in the media folder and call the copy standard.png.  We are not using this right away, so it is just a place marker to pass the check above.

For the text file, create a file called standard.txt and in it put the following;

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

This is simply a list of objects and a number separated by a comma, which represent.
  • The Object Name
  • The Icon Number
This will be expanded upon, but as test data for the file read, it's all we need.

You can even use the AGK editor to create the file (with new/empty file), which will make editing it even easier as is will show as part of your project.
Remember to specify the source location within the media folder.

No comments:

Post a Comment