TGC Codebase Backup



External File String Parser, Into Array. by Jess T

21st Oct 2003 8:39
Summary

These Functions Read data from an external file into arrays for easy access throughout your game.



Description

These functions are designed to wrok together, but can be used seperately if needed (That is why they are seperate and not all one function)

The first Function reads the first character of every line in the external file to determine the data contained on that line. It then increases certain numbers etc which works out the amount of lines that have certain characters at the start of them.

These values are then used to create an array that has the exact amount of elements needed to store the parsed data.

The second Function parses the data in the external file into the array.
To do this, it reads all the data up to the comma (,) and then stores that data into the first element of the array. The data between that comma (,) and the next is then read and stored into the next element of the array.

NB: The data is stored in the array as strings, therefore, when you use the data to change values in your game, you need to put in the Val(string) command before using the values in the array as intergers. Otherwise you wil get errors.

Thanks for looking at my code, don't forget to check out the forums for information on my upcoming game; HellBorn: Wounded Prey. This will be entirely open-source and available to everyone who want's to use it.



Code
                                    ` This code was downloaded from The Game Creators
                                    ` It is reproduced here with full permission
                                    ` http://www.thegamecreators.com
                                    
                                    `.#################################################################################.
`# Functions created by JessTicular - Memeber of Team EOD, Metaphysics Interactive #
`#                   www.JessTicular.tk - Personal Web Page                        #
`#           www.metagameplay.com - Metaphysics Interactive Home Page              #
`#                      Give credit where its due, thanks                          #
`#      If you need help using this, email me at sweet_prozack@hotmail.com         #
`#  Include DarkBASIC somewhere in the subject, or the message will be deleted     #
`#                                                                                 #
`#                                      ENJOY!                                     #
``#################################################################################'


REMSTART *************************************

gamedata.txt is in format:


[CLASS]
Class,Name,Cost,amount of Attack,amount of Dexterety,amount of Strength,amount of Life,amount of Mana,amount of Stamina

`This is a comment line

[CLASS]
Class,Name,Cost,amount of Attack,amount of Dexterety,amount of Strength,amount of Life,amount of Mana,amount of Stamina

ENDFILE

**************************************************
Example:

`In format: Class,Name,Cost,amount of Attack,amount of Dexterety,amount of Strength,amount of Life,amount of Mana,amount of Stamina
[WEAPONS]
Weapon,Cracked Sword,50,12,2,0,0,0,0
Weapon,Hammer,5,3,0,1,0,0,0

[RINGS]
Ring,Thor's Ring,120,0,20,32,15,10,0

[POTIONS]
Potion,Healing Potion,5,0,0,0,40,0,0
Potion,Mana Potion,5,0,0,0,0,40,0
Potion,Stamina Potion,10,0,0,0,0,0,15

ENDFILE

**************************************************

And don't forget, you can add more info, delete some or change the whole file completely... That's what the first function is for, reading the amount of data you put in it, so it can set up the array correctly...

You could also do something like:
**************************************************
`In format: Object number,Type,X-value/radius,Y-value,Z-value,new X-value/new radius,new Y-value,new Z-value
[CAMERA]
0,Camera,0,50,0,100,50,100

[OBJECTS]
1,Cube,50,50,50,2000,50,2000
2,Sphere,600,20,0,0,20,10
3,Cylinder,0,100,0,10,100,20
4,Cone,50,50,50,2000,50,2000

[SIZES]
1,Cube,50,200,60,10,20,680
2,Sphere,20,0,0,100,0,0
4,Cone,10,10,10,50,50,50

ENDFILE

**************************************************

And you could use that to set all the starting positions for your objects, and then use the new values as waypoints or something... And then you could use the sizes etc...


REMEND : `*****************************************

`Set Window On

`Set's up temporary array's
Dim max_no_of_types(1)
Dim max_no_of_items(1)
no_of_variables = 9

`This function finds the max amount of elements needed per Class.
_find_array_amount_needed("gamedata.txt")

`This creates the main array
Dim item_data$(max_no_of_types(1)+1,max_no_of_items(0)+1,no_of_variables)

`This function reads all the data from the specified file into the corresponding array element.
`types refers to CLASS
`items refers to each item listed in the file.
`variables refers to how many variables there are for each item (will be the same for all items, therefore is a constant)
_weapon_file_read("gamedata.txt",max_no_of_types(1),max_no_of_items(1),no_of_variables)

`Deletes unused Array's to improve system performance.
Undim max_no_of_types(1)
Undim max_no_of_items(1)

End








Function _find_array_amount_needed(file$)

`Just initializing these variables
max_no_of_types(1) = 0
max_no_of_items(1) = 0
no_of_variables = 9

`Opens the specified file
Open To Read 1,file$

`Reads the first line of information into the specified string
Read String 1,string$

Sync On : Sync Rate 0

`Checks if "ENDFILE" is there.
While string$ <> "ENDFILE"

   `Selects the first character of the line read into string$
   Select Left$(string$,1)

      Case "["
         max_no_of_types(1) = max_no_of_types(1) + 1
         If  max_no_of_items(1) > max_no_of_items(0)
            max_no_of_items(0) = max_no_of_items(1)
         Endif
         max_no_of_items(1) = 0
         Read String 1,string$
      Endcase

      Case "`"
         Read String 1,string$
      Endcase

      Case ""
         Read String 1,string$
      Endcase

      Case Default
         max_no_of_items(1) = max_no_of_items(1) + 1
         Read String 1,string$
      Endcase

   Endselect

Endwhile

If  max_no_of_items(1) > max_no_of_items(0)
   max_no_of_items(0) = max_no_of_items(1)
Endif

`Prints the info to the screen to get rid of this just delete from line to line
`--------------------------------------------------------------------------------
   Print max_no_of_types(1)
   Print max_no_of_items(0)
`--------------------------------------------------------------------------------

If File Open(1) = 1 Then Close File 1

Sync
Endfunction









Function _weapon_file_read(file$,no_of_types,no_of_items,no_of_variables)

`Opens the file to be read from
Open To Read 1,file$

`This initializes the item number
item_number = 1

Sync On : Sync Rate 0
Do

`Reads an entire line of information from the specified file into the string variable
Read String 1,string$

`Parses to see if its a description tag, a comment tag, a blank line or item data...
If Left$(string$,1) = "[" Or Left$(string$,1) = "`" Or Left$(string$,1) = ""
   Read String 1,string$
Else
   item$ = string$
Endif

If string$ = "ENDFILE" Then Exitfunction

`This initializes the values for the For...Next loop
b = 1
d = 1
If item$ = string$

`Parses to find each part of the info of the item
   For a = b To Len(item$)
      If Mid$(item$,a) <> ","
         item_data$(1,item_number,d) = item_data$(1,item_number,d) + Mid$(item$,a)
      Else
         b = a + 1
         inc d
      EndIf
   Next a

`Prints the info to the screen to get rid of this just delete from line to line
`--------------------------------------------------------------------------------
   If item_data$(1,item_number,2) <> ""

      Print "The ";item_data$(1,item_number,1);" '";item_data$(1,item_number,2);"';"
      Print "Costs ";Val(item_data$(1,item_number,3));" Gold Pieces;"
      Print "Adds An Attack Value Of ";Val(item_data$(1,item_number,4));";"
      Print "Adds A Dexterity Value Of ";Val(item_data$(1,item_number,5));";"
      Print "Adds A Strenght Value Of ";Val(item_data$(1,item_number,6));";"
      Print "Adds A Life Value Of ";Val(item_data$(1,item_number,7));";"
      Print "Adds A Mana Value Of ";Val(item_data$(1,item_number,8));";"
      Print "Adds A Stamina Value Of ";Val(item_data$(1,item_number,9));";"
      Print

   Endif
`--------------------------------------------------------------------------------

   `Increments the item number so each value goes into a seperate part of the array...
   inc item_number

Endif

If item_number > no_of_items Then Close File 1 : Exitfunction


Sync
Loop

If File Open(1) = 1 Then Close File 1

EndFunction