thisFile = openToRead( thisTextName$ )In this case, the filename is the one created previously with the .txt extension and stored in thisTextName$. Local variable thisFile holds the resulting file reference.
We will be reading the file a line at a time using the readLine() command. This takes the file reference as it's parameter.
thisString$ = readLine( thisFile )The first line is read into a temporary string variable thisString$, initialising it ready for the read loop.
repeat if thisString$ <> "" inc thisArraySize , 1 tempArray[ thisArraySize ] = thisString$ endif thisString$ = readLine( thisFile ) until fileEof( thisFile ) > 0This uses the repeat/until structure, checking for the end of the file using the fileEOF() command at then end of the loop.
Within the loop, the temporary string is checked to make sure it is not empty.
If it is not, then the array size variable thisArraySize is incremented by 1 with the inc command and the contents of the temporary string are copied into the temporary array at that position.
Before the loop completes, the next line is read into thisString$ and the end of file check is performed. If the end of file has not been reached, the loop repeats.
When the loop exists,. the file is closed with the closeFile() command.
closefile( thisFile )In order to test that this was successful, we will need to temporarily return the size of the array. This is done by adding the variable thisArraySize after the endfunction command.
endfunction thisArraySizeThe function now looks like this.
// Data Load Functions function LoadObjectData( thisName$ ) // Pass the name without extension thisTextName$ = thisName$ + ".txt" thisImageName$ = FindImageFile( thisName$ ) if getFileExists( thisTextName$ ) and thisImageName$ <> "" // Both data files exist dim tempArray[ 1000 ] as string thisArraySize = 0 thisFile = openToRead( thisTextName$ ) thisString$ = readLine( thisFile ) repeat if thisString$ <> "" inc thisArraySize , 1 tempArray[ thisArraySize ] = thisString$ endif thisString$ = readLine( thisFile ) until fileEof( thisFile ) > 0 closefile( thisFile ) // dim tempArray[ 0 ] endif endfunction thisArraySizeThe dim command at the end is commented out so it does not run, this is there purely as a reminder for when the function is finished.
For now, we are exiting the function with tempArray[] fully populated so that we can check the information read.
To do this we add a couple of lines to the STATE_SETUP block in the main loop and comment out the existing line.
case STATE_SETUP // Setup Code testNum = LoadObjectData( "standard" ) setGameState( STATE_INTRO ) // Workaround to ensure frame rate ok before menu is called // if timer() > 2.0 then setGameState( STATE_MENU ) endcaseThis now calls the function just created, storing the result - the size of the array - in local variable testNum.
It then changes the game state so that on the next loop, the STATE_INTRO block will be used.
The variable testNum is local to the main loop so will keep its value, which will be picked up by the following code.
case STATE_INTRO // Intro Code print(" Lines : " + str( testNum ) ) for i=1 to testNum print( str(i) + " : " + tempArray[ i ] ) next i print( " ----- ") endcaseThis prints out the value in testNum and then performs a loop, printing the contents of tempArray[].
Because the game state is not changed again, this block is re-used every loop to keep the values on screen.
No comments:
Post a Comment