Author Topic: Text saving  (Read 1049 times)

How do I get things like position in a text file? Thanks!

Writing to files
Code: [Select]
%file = new FileObject(); // Create a file object
%file.openForWrite("Add-Ons/file.txt"); // Open a file, if it exists, clear it. If it doesn't exist, create it.
%file.writeLine("This is the first line."); // write some stuff into the file
%file.writeLine("...");
%file.writeLine("Line 3");
%file.close(); // close the file
%file.delete(); // delete the file object

Reading from files
Code: [Select]
%file = new FileObject(); // Create a file object
%file.openForRead("Add-Ons/file.txt"); // Open a file to read
while(!%file.isEOF()){ // loop until we have reached the end of the file
%line = %file.readLine();
%lines++;
echo("Line ", %lines, ": ", %line);
}
%file.close(); // close the file
%file.delete(); // delete the file object

Appending to files (Adding lines to the end without removing the original contents)
Code: [Select]
%file = new FileObject(); // Create a file object
%file.openForAppend("Add-Ons/file.txt"); // Open the desired file, without clearing it beforehand
%file.writeLine("This line will be added onto the end"); // add some stuff to the end of the file
%file.writeLine("and another line");
%file.close(); // close the file
%file.delete(); // delete the file object

Useless Example
Code: [Select]
function savePositions(){
%file = new FileObject();
%file.openForAppend("Add-Ons/positions.txt");
for(%i=0;%i<ClientGroup.getCount;%i++){
%cl = ClientGroup.getObject(%i);
if(!isObject(%client.player))
continue;
%id = %cl.bl_id;
%name = %cl.name;
%position = %client.player.getPosition();
%file.writeLine(%id TAB %name TAB %position);
}
%file.close();
%file.delete();
}

Now how do i get it to read the BL_ID of someone? I know, I suck at coding.

Here's how you would load the information from the file made in the example:
Code: [Select]
%file = new FileObject();
%file.openForRead("Add-Ons/positions.txt");
while(!%file.isEOF()){
%line = %file.readLine();
%id = getField(%line, 0);
%name = getField(%line, 1);
%position = getField(%line, 2);
echo(%name, " (ID: ", %id, ") was at ", %position);
}
%file.close();
%file.delete();

Here's how you would load the information from the file made in the example:
Code: [Select]
%file = new FileObject();
%file.openForRead("Add-Ons/positions.txt");
while(!%file.isEOF()){
%line = %file.readLine();
%id = getField(%line, 0);
%name = getField(%line, 1);
%position = getField(%line, 2);
echo(%name, " (ID: ", %id, ") was at ", %position);
}
%file.close();
%file.delete();
Now what would I have to do to  the code to make the gameconnection spawn there on the first spawn?

Code: [Select]
package loadPosition{

function GameConnection::SpawnPlayer(%this){
Parent::SpawnPlayer(%this);
if(!%this.firstSpawn){
   %this.player.schedule(1000,setTransform,loadPosition(%this));
   %this.firstSpawn = 1;
   }
}

function loadPosition(%client){
   %file = new FileObject();
   %file.openForRead("Add-Ons/positions.txt");
   while(!%file.isEOF()){
      %line = %file.readLine();
      %id = getField(%line, 0);
      %name = getField(%line, 1);
      %position = getField(%line, 2);
         if(%client.bl_id $= %id)
            return %position
      }
%file.close();
%file.delete();
}

};
activatePackage(loadPosition);

I'm not sure about the schedule.

A few things changed/added:
Code: [Select]
package loadPosition{
function GameConnection::SpawnPlayer(%this){
Parent::SpawnPlayer(%this);
if(!%this.firstSpawn){
%position = loadPosition(%this.bl_id);
if(%position !$= ""){
%this.player.setTransform(%position, getWords(%this.player.getTransform, 3, 6));
bottomPrint(%this, "Your position has been loaded", 2, 2);
}
%this.firstSpawn = 1;
}
}

function GameConnection::OnClientLeaveGame(%this){
if(!isObject(%this.player)){
Parent::OnClientLeaveGame(%this);
return;
}
%file = new FileObject();
%file.openForRead("Add-Ons/positions.txt");
while(!%file.isEOF()){
%line = %file.readLine();
%id = getField(%line, 0);
if(%id == %this.bl_id){
%found = 1;
break;
}
}
%file.close();
if(!%found){
%file.openForAppend("Add-Ons/positions.txt");
%file.writeLine(%this.bl_id TAB %this.player.getPosition());
%file.close();
%file.delete();
}
Parent::OnClientLeaveGame(%this);
}

function loadPosition(%bl_id){
%file = new FileObject();
%file.openForRead("Add-Ons/positions.txt");
while(!%file.isEOF()){
%line = %file.readLine();
%id = getField(%line, 0);
%position = ;
if(%bl_id == %id){
%file.close();
%file.delete();
return getField(%line, 2);
}
}
}
};
activatePackage(loadPosition);
It may be better to save positions into variables and export them in this situation, but that's how you would do it with files anyway.

Thanks guys! But can't you use gameconnection::onentergame? I believe that is when the client spawns because it is where the RTB game.cs says: "%name has spawned."

I'm not sure about the functions, but I saw Pickle had used that one, and in my hide names script I also used GameConnection::SpawnPlayer, so I assumed it would be okay.