[SOLVED by workaround]How to edit a line in a file?
by Vlad I · in Torque Game Builder · 05/15/2012 (4:10 pm) · 7 replies
I can read out and write in file. And have written some data into .sav file.
I want to replace one line in it. I want to replace Cage01_item for CageScorpions01_item. How do I go about it? I have no idea.
This is what I have in the .sav file:
1320
Item
Net01_item
4.385 9.046
1
1
yellow
1320
Item
Cage01_item
6.250 6.250
1
1
1320
Item
Key01_item
4.385 9.046
1
1
yellow
What's the procedure for this? Has anyone done it?
I want to replace one line in it. I want to replace Cage01_item for CageScorpions01_item. How do I go about it? I have no idea.
This is what I have in the .sav file:
1320
Item
Net01_item
4.385 9.046
1
1
yellow
1320
Item
Cage01_item
6.250 6.250
1
1
1320
Item
Key01_item
4.385 9.046
1
1
yellow
What's the procedure for this? Has anyone done it?
About the author
#2
Here is what I have for saving:
05/16/2012 (3:39 am)
Hey Justin! Your code looks similar to mine.Here is what I have for saving:
function endLevel()
{
//create a new filestreamobject to write to a file.
$stream = new FileStreamObject();
//lets open up the inventory.sav file or if it is not there make one.
$stream.open("~/data/inventory.sav","Write");
for(%count = 0; %count < 21; %count ++)
{
//let us loop through all the objects in our inventory and check if they are there or not.
if(isObject($inventoryItem[%count]))
{
//if they are there lets just write down their attributes to the file.
$stream.writeline($inventoryItem[%count].scenegraph);
$stream.writeline($inventoryItem[%count].class);
$stream.writeline($inventoryItem[%count].imagemap);
$stream.writeline($inventoryItem[%count].size);
$stream.writeline($inventoryItem[%count].usemouseevents);
$stream.writeline($inventoryItem[%count].usable);
$stream.writeline($inventoryItem[%count].colour);
//and delete the object when done.
$inventoryItem[%count].delete();
}
}
//we now close the old filestreamobject and delete it
$stream.close();
$stream.delete();
//and make a new filestreamobject
$stream = new FileStreamObject();
//we open or create the save.sav file depending whether or not it exists.
$stream.open("~/data/save.sav","Write");
//we write the and the inventorytotal to the file
$stream.writeline($inventoryTotal);
//we close and delete the filestreamobject
$stream.close();
$stream.delete();
//and we tell the engine that we have saved our game/inventory.
$saved = true;
}So then we can read out preferences for the items, which are in the .sav file :1320 Item //<Class Net01_item //<ImageMap - This image map name I want to replace for another 4.385 9.046 //<Size 1 //<UseMouseEvents true/false 1 //<Usable true/false yellow //< Colour, it's some dynamic field for use with another item //with the same assigned color. Currently this one is yellow
#3
05/16/2012 (3:53 am)
And this is what I use for loading :function loadInventory()
{
//now let us check if we have a save file or if the player has recently saved and not closed the game.
if($saved == true || isFile("~/data/save.sav"))
{
//create a new file stream object, this is used to read from files and write to files
$stream = new filestreamobject();
//open up a file, in this case it is the inventory.sav
$stream.open("~/data/inventory.sav", "read");
//now we will use a for loop to check what items have been saved in the inventory
for(%count = 0; %count < 22; %count ++)
{
//this is used as a safe check to see if the current line has an object saved on it
%scenegraphCheck = $stream.readline();
//if there is know object on the current line, we will skip the next 6 lines and check the other ones.
if(strlen(%scenegraphCheck) != 4)
{
//the checkcount has to be less than the number of lines you have for each object
for(%checkCount = 0; %checkCount < 11; %checkCount ++)
{
%scenegraphCheck = $stream.readline();
}
}
//here we check if we have reached the end of the opened file or if you have looped 21 times or if we have
//got no more objects to load to the inventory.
if($stream.isEOF() || %scenegraphCheck $= "" || %count == 21)
{
//we close and delete the stream object.
$stream.close();
$stream.delete();
//we make another one to read the user save data
$stream = new filestreamobject();
$stream.open("~/data/save.sav", "read");
//this is so we can record how much items we have in the inventory and were the initial index of loading them is (for gui only).
$inventoryTotal = $stream.readLine();
//we close and delete the stream object.
$stream.close();
$stream.delete();
//and finally we update the inventory.
updateInventory();
return;
}
//if we have found an inventory item in the inventory.sav file we simply recreate it using the reading from the
//saved attributes.
$inventoryItem[%count] = new t2dStaticSprite()
{
scenegraph = sceneWindow2D.getSceneGraph();
class = $stream.readline();
imagemap = $stream.readline();
size = $stream.readline();
usemouseevents = $stream.readline();
usable = $stream.readline();
colour = $stream.readline();
opened = $stream.readline();
key = $stream.readline();
shape = $stream.readline();
saveable = $stream.readline();
layer = 1;
reference = %count;
};
}
}
}
#4
I have an idea on how to maybe help solve your problem but I need to know if only one inventory object is having this problem or all 21? And did you figure this out already or is this topic still relevant?
05/29/2012 (9:43 am)
Now I am assuming you want to do the editing in game otherwise you could open it in a text editor and fix it. :PI have an idea on how to maybe help solve your problem but I need to know if only one inventory object is having this problem or all 21? And did you figure this out already or is this topic still relevant?
#5
Here is what I did:
Just a quick explanation what I wanted. I wanted the empty cage - which is an item in my inventory to be gone and replaced by filled with caught scorpions when all the scorpions are caught by player.
So instead I made a window appear of a Net with the scorpions in it and the player need to place them into the empty cage. Thus enabling me to use the empty cage on the object and have it deleted for good and make the new on filled with the scorpions appear on mouseDown and go to the inventory.
However, later I also found another logical workaround. I actually was able to remove any item from my inventory by the script by a function I overlooked at that time. It does the job by simply putting a name of the object you want to remove.
So all in all the problem is solved. Thank you.
I forgot to mark it as solved sorry :)
05/29/2012 (10:41 am)
Alpha-Kand I ditched it. I don't want to scrap a good level I spent my time working hard on it.Here is what I did:
Just a quick explanation what I wanted. I wanted the empty cage - which is an item in my inventory to be gone and replaced by filled with caught scorpions when all the scorpions are caught by player.
So instead I made a window appear of a Net with the scorpions in it and the player need to place them into the empty cage. Thus enabling me to use the empty cage on the object and have it deleted for good and make the new on filled with the scorpions appear on mouseDown and go to the inventory.
However, later I also found another logical workaround. I actually was able to remove any item from my inventory by the script by a function I overlooked at that time. It does the job by simply putting a name of the object you want to remove.
So all in all the problem is solved. Thank you.
I forgot to mark it as solved sorry :)
#6
05/29/2012 (11:50 am)
Okay then. Good luck.
#7
05/30/2012 (4:05 pm)
Well, this may help someone else, since you have already solved your problem:%result = replaceInFile("data1","data2");
if(%result)
echo("Success!");
else
echo("Failure!");function replaceInFile(%tag,%replace)
{
//Define some variables
//---------------------
%result = false;
%lc =-1;
%path = "Enter Path Here";
//---------------------
%f = new fileObject();
%f.openForRead(%path);
while(!%f.isEof())
{
%line[%lc++] = %f.readLine();
//If current line in the file equals to tag, then replace
if(%line[%lc] $= %tag)
{
%line[%lc] = %replace;
//Set result to true
%result = true;
}
}
%f.close();
%f.delete();
//If result is false, no need to continue
if(%result == false)
{
return 0;
}
%f = new fileObject();
//Overwrite file
%f.openForWrite(%path);
for(%i=0;%i<%lc;%i++)
{
%f.writeLine(%line[%i]);
}
%f.close();
%f.delete();
return true;
}
Torque Owner Justin Proffitt
Here, let me show you how I manage my information on file:
First, I create a scriptobject called "PlayerData" that stores all variables related to a profile, then I use these four functions to manage it:
//// File IO functions //// function createProfile( %profile, %name ){ //// Creates and saves a script object called "PlayerData" %fileHandler = new FileObject(); %file = "game/data/saves/" @ %profile @ ".cs"; %playerData = new scriptObject(PlayerData){ Name = %name; Profile = %profile; Credits = "0"; Operations = "0"; Incidents = "0"; Mass = "3"; Thrust = "6"; Power = "10"; Capacity = "75"; ScannerLevel = "1"; StealthLevel = "1"; }; %fileHandler.openForWrite(%file); %fileHandler.writeLine("/* " @ %name @ " */"); //Added to top for quick retrieval %fileHandler.writeObject( %playerData ); %fileHandler.close(); %fileHandler.delete(); return true; } function saveProfile( %profile ){ //// Saves PlayerData to a file echo("Saving..."); %fileHandler = new FileObject(); %file = "game/data/saves/" @ %profile @ ".cs"; if(%fileHandler.openForWrite(%file) && isObject(PlayerData)){ %fileHandler.writeLine("/* " @ PlayerData.Name @ " */"); %fileHandler.writeObject( PlayerData ); } else { %fileHandler.close(); %fileHandler.delete(); return false; } %fileHandler.close(); %fileHandler.delete(); echo("Save successful"); return true; } function loadProfile( %profile ){ //// Loads the data of a profile in the //// form of a scriptObject called "PlayerData" %fileHandler = new FileObject(); %file = "game/data/saves/" @ %profile @ ".cs"; // Delete old PlayerData if(isObject(PlayerData)){ PlayerData.delete(); } if(%fileHandler.openForRead(%file)){ %obj = ""; // Build the object declaration line by line // Then evaluate it as a string while(!%fileHandler.isEoF()){ %obj = %obj @ %fileHandler.readLine(); } eval(%obj); %fileHandler.close(); %fileHandler.delete(); return true; } else{ %fileHandler.close(); %fileHandler.delete(); return false; } } function deleteProfile( %profile ){ //// Deletes a profile file %fileHandler = new FileObject(); %file = "game/data/saves/" @ %profile @ ".cs"; %fileHandler.openForWrite(%file); %fileHandler.writeLine ("/* Empty */"); %fileHandler.close(); %fileHandler.delete(); }