How to read a text file from script?
by Highlander · in Torque Game Engine · 05/12/2007 (10:02 am) · 1 replies
I want to modify the game.cs script of the fps starter demo to read in weapon and ammo information for a particular player from a text file named after the player being spawned. I want it to look for a text file named [players name].txt and then load the weapons and ammo listed in it. For example,
Highlander.txt:
Crossbow,1,0 //where 0 is the mounting location, if any
CrossbowAmmo,10
The script simply reads these two lines in and adds them to inventory. If there are two parameters in a line, then it knows to mountImage as well.
Here the function I want to do it in:
function GameConnection::createPlayer(%this, %spawnPoint)
{
if (%this.player > 0) {
// The client should not have a player currently
// assigned. Assigning a new one could result in
// a player ghost.
error( "Attempting to create an angus ghost!" );
}
// Create the player object
%player = new Player() {
dataBlock = PlayerBody;
client = %this;
};
MissionCleanup.add(%player);
// Player setup...
%player.setTransform(%spawnPoint);
%player.setShapeName(%this.name);
// Starting equipment
%player.setInventory(Crossbow,1);
%player.setInventory(CrossbowAmmo,10);
%player.mountImage(CrossbowImage,0);
// Update the camera to start with the player
%this.camera.setTransform(%player.getEyeTransform());
// Give the client control of the player
%this.player = %player;
%this.setControlObject(%player);
}
Highlander.txt:
Crossbow,1,0 //where 0 is the mounting location, if any
CrossbowAmmo,10
The script simply reads these two lines in and adds them to inventory. If there are two parameters in a line, then it knows to mountImage as well.
Here the function I want to do it in:
function GameConnection::createPlayer(%this, %spawnPoint)
{
if (%this.player > 0) {
// The client should not have a player currently
// assigned. Assigning a new one could result in
// a player ghost.
error( "Attempting to create an angus ghost!" );
}
// Create the player object
%player = new Player() {
dataBlock = PlayerBody;
client = %this;
};
MissionCleanup.add(%player);
// Player setup...
%player.setTransform(%spawnPoint);
%player.setShapeName(%this.name);
// Starting equipment
%player.setInventory(Crossbow,1);
%player.setInventory(CrossbowAmmo,10);
%player.mountImage(CrossbowImage,0);
// Update the camera to start with the player
%this.camera.setTransform(%player.getEyeTransform());
// Give the client control of the player
%this.player = %player;
%this.setControlObject(%player);
}
Associate Fyodor -bank- Osokin
Dedicated Logic
function HelpFileList::onSelect(%this, %row) { %fo = new FileObject(); // create new object %fo.openForRead(%this.fileName[%row]); // open for reading %text = ""; while(!%fo.isEOF()) %text = %text @ %fo.readLine() @ "\n"; // read line by line until the End Of File %fo.delete(); // clear object HelpText.setText(%text); // using text }This is a "basics" of the file reading.Actually, create a FileObject and do the %obj.dump(); to see what it can do for more options (incl. saving).