File object help [More Questions]

Author Topic: File object help [More Questions]  (Read 2766 times)

Alrighty, so I've been going about working on mods and such and started thinking about a good way to save a stuffton of variable for each player every time they leave/rejoin/update

Anywho, I need a function that can when called and get's the clients arg can save the information to a .dat filetype.


And no, I'm not here asking for full code or anything, haha, my two main questions are:

1. How to search a fileobject for a certain word/words and get the line it's on
2. How to write to a specific line, is it just writeLine(linenumberhere);?
« Last Edit: November 01, 2012, 02:03:18 PM by ¥ola »

file objects are a stream of data.
all writeline calls on an appending fileobject set data to the end.

better method:

$varprefix::blah = asdf;
$varprefix::noob = blah;

export("$varprefix::*", "file/path/of/config/file.cs");

to recall all data

exec("file/path/of/config/file.cs");

1. You have to open the file and read through it
2. You have to:
 2a. Open the file for read
 2ba. Either make a line buffer and read all the lines into that, then edit a specific line in the array, then write those lines back into the file, or
 2bb. Open the file for read, open a temporary file for write, read the file lines and then write them (after any editing) into the temporary file, delete the original file, copy the temp file into the name of the original file, then delete the temporary file

so basically it's not that easy

Wasn't really asking for an easy way, haha

I knew it wouldn't be easy, my main concern is I have to update this every time a player joins/leaves/updates

So with Lugnut's way wouldn't it cause a bit of lag re-executing 10 - 15 variables for 50 some players every time it's called?

You can use a script object for client data. They are easy to save, but i'd reccomend writing your own loading system for it.

You can use a script object for client data. They are easy to save, but i'd reccomend writing your own loading system for it.

Not at all good with SO's, any help there?

Not at all good with SO's, any help there?
Code: [Select]
new scriptObject(MyDataHolder);

function MyDataHolder::savePlayer(%this, %client)
{
%this.data[%client.BL_ID, "name"] = %client.getPlayerName();
%this.data[%client.BL_ID, "level"] = %client.playerLevel;
%this.data[%client.BL_ID, "currency"] = %client.money;
%this.data[%client.BL_ID, "otherVariable"] = %client.otherVariable;
%this.save("config/My_Addon/PlayerData.cs");
}

function MyDataHolder::loadPlayerData(%this, %client)
{
%client.playerLevel = %this.data[%client.BL_ID, "level"];
%client.money = %this.data[%client.BL_ID, "currency"];
%client.otherVariable = %this.data[%client.BL_ID, "otherVariable"];
}

function loadPlayerData()
{
while(isObject(MyDataHolder))
MyDataHolder.delete();
exec("config/My_Addon/PlayerData.cs");
}
« Last Edit: October 31, 2012, 05:19:03 PM by Trinick »

Code: [Select]
new scriptObject(MyDataHolder);

function MyDataHolder::savePlayer(%this, %client)
{
%this.name[%client.BL_ID] = %client.getPlayerName();
%this.level[%client.BL_ID] = %client.playerLevel;
%this.currency[%client.BL_ID] = %client.money;
%this.otherVariable[%client.BL_ID] = %client.otherVariable;
%this.save("config/My_Addon/PlayerData.cs");
}

function MyDataHolder::loadPlayerData(%this, %client)
{
%client.playerLevel = %this.name[%client.BL_ID];
%client.money = %this.currency[%client.BL_ID];
%client.otherVariable = %this.otherVariable[%client.BL_ID];
}

function loadPlayerData()
{
while(isObject(MyDataHolder))
MyDataHolder.delete();
exec("config/My_Addon/PlayerData.cs");
}
Not that I asked for it, lol, does this work standalone?

Not that I asked for it, lol, does this work standalone?
Yes, but it just tells you how you would do it, you're supposed to fill in your own variable names. Also, I reformatted the way it saves so that it'll save all client data together.

Yes, but it just tells you how you would do it, you're supposed to fill in your own variable names. Also, I reformatted the way it saves so that it'll save all client data together.
Alright, lot's of thanks

You should store each client's data in a separate file, otherwise you're gonna start lagging with each save or load.

Alright, messed around with Trinick's script and got exactly what I needed, thanks a bunch guys

Code: [Select]
new scriptObject(MyDataHolder);

function MyDataHolder::savePlayer(%this, %client)
{
%this.data[%client.BL_ID, "name"] = %client.getPlayerName();
%this.data[%client.BL_ID, "level"] = %client.playerLevel;
%this.data[%client.BL_ID, "currency"] = %client.money;
%this.data[%client.BL_ID, "otherVariable"] = %client.otherVariable;
%this.save("config/My_Addon/PlayerData.cs");
}

function MyDataHolder::loadPlayerData(%this, %client)
{
%client.playerLevel = %this.data[%client.BL_ID, "level"];
%client.money = %this.data[%client.BL_ID, "currency"];
%client.otherVariable = %this.data[%client.BL_ID, "otherVariable"];
}

function loadPlayerData()
{
while(isObject(MyDataHolder))
MyDataHolder.delete();
exec("config/My_Addon/PlayerData.cs");
}

Not quite what i meant.

Not quite what i meant.
Did you mean a scriptobject for each player? I figured that was also a possibility but then I decided on showing him how to do it that way, that way there's only 1 master object to deal with.

Did you mean a scriptobject for each player? I figured that was also a possibility but then I decided on showing him how to do it that way, that way there's only 1 master object to deal with.
You'd still want to break it down into parts at some point for storage. Keeping it all in one file means longer and longer loading times with each new person added. If the server is popular enough, it could get to there point where there's a significant lag spike with each load. Pecon's server stalls for like fifteen seconds during the startup because of a really simple IP logger script that saves them all to a single file.