Game Development Community

How to read and analyze single variables in a text file?

by Thomas Reynolds · in Torque Game Engine · 03/05/2011 (5:08 pm) · 7 replies

First off, I'm new to these forums, so please tell me if I've posted in the wrong section or something.


On to my issue:

How can I make a script evaluate every variable in a text file? I need the script to look at every number individually. I've looked for string commands to help, but can't find anything to really help.

I've read up on FileIO, and got line reading working fine.

Here is my script:
function ReadTerrainFile(%file)
{
	%file = expandFilename(%file);
	if(isFile(%file))
	{
		%fileObject = new FileObject();
		echo(" ");
		if(%fileObject.OpenForRead(%file))
		{
				for(%l=0;%l<16;%l++)
				{
					%line = %fileObject.readline();
					echo(%line);
				}
			echo(" ");
			echo("The reading of the file "@%file SPC "is now complete.");
		}
		else 
		{
			echo("ReadTerrainFile() - Error opening the terrain file "@%file);
		}
		%fileObject.delete();
	}
	else
	{
		echo("ReadTerrainFile() - Error locating the terrain file "@%file);
	}
}

This outputs the following to the console when you specify a file:
==>ReadTerrainFile("Add-Ons/Client_TreyTerrain/TerrainData/SampleTerrainData.txt");
 
0000000100001000
0000000011111110
0000001100000001
1111100000011110
0011110000000110
0111100000011100
0111110011100000
0011110111111000
0001100111111111
0011101111111100
0001101111111000
0000001111111100
0000000111111110
0001111111111100
0000011111100100
0000001110000010
 
The reading of the file Add-Ons/Client_TreyTerrain/TerrainData/SampleTerrainData.txt is now complete.

Optimally, I would like to be able to look at every value in each line individually, and determine whether it is a "1" or a "0", then do more actions accordingly.

I know I could do this by setting every value in the SampleData to a variable, but I don't want to do that because the TerrainFile being read will eventually be much larger than 16 by 16, and it would add a lot of characters to the file.

Any help you guys can give would be great, thanks in advance

-Thomas

About the author

Recent Threads

  • Help with databases

  • #1
    03/05/2011 (10:23 pm)
    Add a space in between each value in the text file, then use getWord to query the individual values from script.
    #2
    03/06/2011 (4:22 am)
    Will getWord automatically evaluate each argument in order, or do I need to specify to go on?
    It would be very helpful for me to see an example script.
    #3
    03/06/2011 (5:55 am)
    You have to use it in a loop like this:

    function ReadTerrainFile(%file)
    {
    	%file = expandFilename(%file);
    	if(isFile(%file))
    	{
    		%fileObject = new FileObject();
    		echo(" ");
    		if(%fileObject.OpenForRead(%file))
    		{
    				for(%l=0;%l<16;%l++)
    				{
    					%line = %fileObject.readline();
    					echo(%line);
    
    		   		   for(%i=0;%i<16;%i++)
    				   {
                        %val = getWord(%line,%i);
    					// ... do your processing here
    			 	   }
    
    				}
    			echo(" ");
    			echo("The reading of the file "@%file SPC "is now complete.");
    		}
    		else 
    		{
    			echo("ReadTerrainFile() - Error opening the terrain file "@%file);
    		}
    		%fileObject.delete();
    	}
    	else
    	{
    		echo("ReadTerrainFile() - Error locating the terrain file "@%file);
    	}
    }

    but make sure that you leave a space in between data values in your file. Or it wont work.
    #4
    03/06/2011 (6:42 am)
    Thanks a bunch, let me try it out.



    Edit: I think I'm doing something wrong:
    function ReadTerrainFile(%file)
    {
    	%file = expandFilename(%file);
    	if(isFile(%file))
    	{
    		%fileObject = new FileObject();
    		echo(" ");
    		if(%fileObject.OpenForRead(%file))
    		{
    				for(%l=0;%l<16;%l++)
    				{
    					%line = %fileObject.readline();
    					echo(%line);
    				}
    			echo(" ");
    			echo("The reading of the file "@%file SPC "is now complete.");
    				for(%m=0;%m<16;%m++)
    				{
    				%terVal = getWord(%line,%m);
    					if(%terVal = w) 
    						echo("The value is zero");
    					else if(%terVal = 1) 
    						echo("The value is one");
    				}
    		}
    		else 
    		{
    			echo("ReadTerrainFile() - Error opening the terrain file "@%file);
    		}
    		%fileObject.delete();
    	}
    	else
    	{
    		echo("ReadTerrainFile() - Error locating the terrain file "@%file);
    	}
    }
    Which returns the following
    ==>ReadTerrainFile("Add-Ons/Client_TreyTerrain/TerrainData/SampleTerrainData.txt");
     
    w w w w w w w 1 w w w w 1 w w w
    w w w w w w w w 1 1 w 1 1 1 1 w
    w w w w w 1 1 1 w w w w w w w 1
    1 1 1 1 1 w w w w w w 1 1 1 1 w
    w w 1 1 1 1 w w w w w w w 1 1 w
    w 1 1 1 1 w w w w w w 1 1 1 w w
    w 1 1 1 1 1 w w 1 1 1 w w w w w
    w w 1 1 1 1 w 1 1 1 1 1 1 w w w
    w w w 1 1 w w 1 1 1 1 1 1 1 1 1
    w w 1 1 1 w 1 1 1 1 1 1 1 1 w w
    w w w 1 1 w 1 1 1 1 1 1 1 w w w
    w w w w w w 1 1 1 1 1 1 1 1 w w
    w w w w w w w 1 1 1 1 1 1 1 1 w
    w w w 1 1 1 1 1 1 1 1 1 1 1 w w
    w w w w w 1 1 1 1 1 1 w w 1 w w
    w w w w w w 1 1 1 w w w w w 1 w
     
    The reading of the file Add-Ons/Client_TreyTerrain/TerrainData/SampleTerrainData.txt is now complete.
    The value is one
    The value is one
    The value is one
    The value is one
    The value is one
    The value is one
    The value is one
    The value is one
    The value is one
    The value is one
    The value is one
    The value is one
    The value is one
    The value is one
    The value is one
    The value is one

    Help?
    #5
    03/06/2011 (8:09 am)
    You un-nested the "for loops" and if you want to compare values then you have to use == (for numbers) or $= (for strings).

    Try this:
    function ReadTerrainFile(%file)
    {
    	%file = expandFilename(%file);
    	if(isFile(%file))
    	{
    		%fileObject = new FileObject();
    		echo(" ");
    		if(%fileObject.OpenForRead(%file))
    		{
    				for(%l=0;%l<16;%l++)
    				{
    					%line = %fileObject.readline();
    					echo(%line);
    				   for(%m=0;%m<16;%m++)
    				   {
    				      %terVal = getWord(%line,%m);
    				      echo("The value is ",%terVal);
    				   }
    				}
    			echo(" ");
    			echo("The reading of the file "@%file SPC "is now complete.");
    
    		}
    		else 
    		{
    			echo("ReadTerrainFile() - Error opening the terrain file "@%file);
    		}
    		%fileObject.delete();
    	}
    	else
    	{
    		echo("ReadTerrainFile() - Error locating the terrain file "@%file);
    	}
    }
    #6
    03/06/2011 (8:41 am)
    Aha!

    Well, thanks a lot for your help
    #7
    03/06/2011 (9:04 am)
    no problem.