Author Topic: Saving/Loading Script[Solved]  (Read 3989 times)

I made this script saving weather the client is muted, the pos of the player and all of the tools the player has. It saves and loads properly. My problem is giving the items to the player. This is the code.

Code: [Select]
$Gamemode::PlayerMaxTools=5;

function loadstuff(%client)
{
%file = new FileObject();
%filename = "config/server/MedievalRPG/stats/" @ %client.bl_id @ ".txt";
%file.openForRead(%filename);
%client.player.position = %file.readLine();
$RPG::Variables[%client.bl_id @ "_Mute"] = %file.readLine();
$RPG::Variables[%client.bl_id @ "_MuteReason"] = %file.readLine();
%client.player.cleartools();
addItem(%client.player,%file.readLine());
addItem(%client.player,%file.readLine());
addItem(%client.player,%file.readLine());
addItem(%client.player,%file.readLine());
addItem(%client.player,%file.readLine());
}
function addItem(%player,%image)
{
   for(%i = 0; %i < $Gamemode::PlayerMaxTools; %i++)
   {
      %tool = %player.tool[%i];
      if(%tool == 0)
      {
         %player.tool[%i] = %image;
         %player.weaponCount++;
         messageClient(%client,'MsgItemPickup','',%i,%image);
         break;
      }
   }
}

package loadOnPlayerSpawn
{
function GameConnection::onPlayerSpawn(%client)
{
Parent::onPlayerSpawn(%client);
if(isFile("config/server/MedievalRPG/stats/" @ %client.bl_id @ ".txt"))
{
loadstuff(%client);
} else { warn("Failed to find stats for " @ %client.Name); }
}
};
activatePackage(loadOnPlayerSpawn);

package onplayerenter {
function GameConnection::onClientEnterGame(%client)
{
Parent::onClientEnterGame(%client);
if(isFile("config/server/MedievalRPG/stats/" @ %client.bl_id @ ".txt"))
{
loadstuff(%client);
} else { warn("Failed to find stats for " @ %client.Name); }
}
};
activatePackage(onplayerenter);
function saveStats(%client) {
%file = new FileObject();
%filename = "config/server/MedievalRPG/stats/" @ %client.bl_id @ ".txt";
%file.openForWrite(%filename);
%file.writeLine(%client.player.position);
%file.writeLine($RPG::Variables[%client.bl_id @ "_Mute"]);
%file.writeLine($RPG::Variables[%client.bl_id @ "_MuteReason"]);
%file.writeLine(%client.player.tool[0]);
%file.writeLine(%client.player.tool[1]);
%file.writeLine(%client.player.tool[2]);
%file.writeLine(%client.player.tool[3]);
%file.writeLine(%client.player.tool[4]);
%file.close();
%file.delete();
}

function saveAllClientStats()
{
%count = clientGroup.getCount();
for(%i = 0; %i < %count; %i++)
{
%cl = clientGroup.getObject(%i);
saveStats(%cl);
}
}

The tools clear and I hear the tool-pickup sound but the tool list won't show up and
Code: [Select]
talk(fcbn("Pie Crust").player.tool[0]); returns 0.
« Last Edit: July 29, 2013, 10:33:45 AM by Pie Crust »

I made this script saving weather the client is muted, the pos of the player and all of the tools the player has. It saves and loads properly. My problem is giving the items to the player. This is the code.

Code: [Select]
$Gamemode::PlayerMaxTools=5;

function loadstuff(%client)
{
%file = new FileObject();
%filename = "config/server/MedievalRPG/stats/" @ %client.bl_id @ ".txt";
%file.openForRead(%filename);
%client.player.position = %file.readLine();
$RPG::Variables[%client.bl_id @ "_Mute"] = %file.readLine();
$RPG::Variables[%client.bl_id @ "_MuteReason"] = %file.readLine();
%client.player.cleartools();
addItem(%client.player,%file.readLine());
addItem(%client.player,%file.readLine());
addItem(%client.player,%file.readLine());
addItem(%client.player,%file.readLine());
addItem(%client.player,%file.readLine());
}
function addItem(%player,%image)
{
   for(%i = 0; %i < $Gamemode::PlayerMaxTools; %i++)
   {
      %tool = %player.tool[%i];
      if(%tool == 0)
      {
         %player.tool[%i] = %image;
         %player.weaponCount++;
         messageClient(%client,'MsgItemPickup','',%i,%image);
         break;
      }
   }
}

package loadOnPlayerSpawn
{
function GameConnection::onPlayerSpawn(%client)
{
Parent::onPlayerSpawn(%client);
if(isFile("config/server/MedievalRPG/stats/" @ %client.bl_id @ ".txt"))
{
loadstuff(%client);
} else { warn("Failed to find stats for " @ %client.Name); }
}
};
activatePackage(loadOnPlayerSpawn);

package onplayerenter {
function GameConnection::onClientEnterGame(%client)
{
Parent::onClientEnterGame(%client);
if(isFile("config/server/MedievalRPG/stats/" @ %client.bl_id @ ".txt"))
{
loadstuff(%client);
} else { warn("Failed to find stats for " @ %client.Name); }
}
};
activatePackage(onplayerenter);
function saveStats(%client) {
%file = new FileObject();
%filename = "config/server/MedievalRPG/stats/" @ %client.bl_id @ ".txt";
%file.openForWrite(%filename);
%file.writeLine(%client.player.position);
%file.writeLine($RPG::Variables[%client.bl_id @ "_Mute"]);
%file.writeLine($RPG::Variables[%client.bl_id @ "_MuteReason"]);
%file.writeLine(%client.player.tool[0]);
%file.writeLine(%client.player.tool[1]);
%file.writeLine(%client.player.tool[2]);
%file.writeLine(%client.player.tool[3]);
%file.writeLine(%client.player.tool[4]);
%file.close();
%file.delete();
}

function saveAllClientStats()
{
%count = clientGroup.getCount();
for(%i = 0; %i < %count; %i++)
{
%cl = clientGroup.getObject(%i);
saveStats(%cl);
}
}

The tools clear and I hear the tool-pickup sound but the tool list won't show up and
Code: [Select]
talk(fcbn("Pie Crust").player.tool[0]); returns 0.
fcbn isn't a real function.

I declared fcbn as findclientbyname in another script. That isn't the point anyways. The point is that addItem() won't give my player an item even though I do hear the sound.

EDIT: Wouldn't it be easier to just quote fcbn rather than the whole OP?
« Last Edit: July 28, 2013, 12:14:09 AM by Pie Crust »

I'm not good with file objects, but
Code: [Select]
addItem(%client.player,%file.readLine());
addItem(%client.player,%file.readLine());
addItem(%client.player,%file.readLine());
addItem(%client.player,%file.readLine());
addItem(%client.player,%file.readLine());

Wouldn't this need to know what line to read?

As far as I noticed with
Code: [Select]
%client.player.position = %file.readLine();
$RPG::Variables[%client.bl_id @ "_Mute"] = %file.readLine();
$RPG::Variables[%client.bl_id @ "_MuteReason"] = %file.readLine();

whenever you read a line, it jumps to the second line. The file is similar to this:
Code: [Select]
6 50 20
1
Lol u mad bro?
881
383
0
0
0
From top to bottom:
PlayerPos
Muted?
Mute Reason
Tool1
tool2
tool3
tool4
tool5

Why not use variables and export();?
Like
$RPG::SavedVariable[%client.bl_id, "Tool1"] = "hammerItem";

Actually, variables are saved like that. An example:

$RPG::Variables[%client.bl_id @ "_Mute"]
$RPG::Variables["27690_Mute"] = %file.readLine()

The variable is exported to a .txt

%file.writeLine($RPG::Variables[%client.bl_id @ "_Mute"]);

And loaded.

$RPG::Variables[%client.bl_id @ "_Mute"] = %file.readLine();

The problem is with the tools. Not the variables. I even echoed some time ago. The loadstuff() reads the values perfectly. The problem is at addItem. I hear the sound of picking up tools but the inventory is empty. That is what I need help on.

Code: [Select]
881
383
0
0
0
Tool1
tool2
tool3
tool4
tool5

You give addItem an IMAGE, and you say that image is, for example '881', when the image should be, for example, 'GunImage'.

off topic: I've always loving hated fcbn. It's like this awful bastard brown townog to findClientByName that does nothing besides support laziness in scripters and hinder the learning process of anyone observing said lazy scripter by teaching them to substitute a function name that makes sense,  like findClientByName, with a function name that doesn't make sense but is easier to type, like fcbn.

on topic: there's too many things that could be potentially wrong with this script. You're going to have to do some debugging yourself. Add echo statements in each function to track the progress of your loading. For example, echo("Item Added: " @ (%line = %file.readLine())); addItem(%client.player, %line);

If those echos show up, then it's the code in your addItem function that's faulty. If they don't, addItem isn't even being called. Did you check to see if the files were even being written? I find asking for help debugging on the forum much like asking for conceptual help: you have to try to do it yourself before we'll help you, otherwise we're just spoon feeding you. Debugging skills are invaluable in the programming world.

off topic: I've always loving hated fcbn. It's like this awful bastard brown townog to findClientByName that does nothing besides support laziness in scripters and hinder the learning process of anyone observing said lazy scripter by teaching them to substitute a function name that makes sense,  like findClientByName, with a function name that doesn't make sense but is easier to type, like fcbn.

yes but fcbn is useful when debugging, for it's length. He ran it using eval and didn't actually code his script using it.

yes but fcbn is useful when debugging, for it's length. He ran it using eval and didn't actually code his script using it.
If by debugging you mean using eval then sure, but really only in the case where you don't have enough space on your chat line. That's easily forgivable though, I'm pointing that message at people who eval things like fcbn("aaa").player.unhideNode("ALL"); Most people's excuse is that it's easier to type than findClientByName, but I've typed that function innumerable times to the point where it's literally muscle memory and comes out at somewhere around 300wpm.

on topic: there's too many things that could be potentially wrong with this script. You're going to have to do some debugging yourself. Add echo statements in each function to track the progress of your loading. For example, echo("Item Added: " @ (%line = %file.readLine())); addItem(%client.player, %line);

If those echos show up, then it's the code in your addItem function that's faulty. If they don't, addItem isn't even being called. Did you check to see if the files were even being written? I find asking for help debugging on the forum much like asking for conceptual help: you have to try to do it yourself before we'll help you, otherwise we're just spoon feeding you. Debugging skills are invaluable in the programming world.

Again, the files ARE getting written and read. The problem is when it hits addItem(). When I put in the console, for example, "addItem(findclientbyname("Pie Crust"),383);", I should get the printer. I ehar the sound when you pick up tools but the inventory is empty. (Taking in count I put cleartools() before.)

If those echos show up, then it's the code in your addItem function that's faulty.

That is exactly the problem. The entire addItem() gets run. I had it like this before:
Code: [Select]
function addItem(%player,%image)
{
echo("1");
for(%i = 0; %i < $Gamemode::PlayerMaxTools; %i++)
{
echo("2");
%tool = %player.tool[%i];
if(%tool == 0)
{
echo("3");
%player.tool[%i] = %image;
echo("4");
%player.weaponCount++;
echo("5");
messageClient(%client,'MsgItemPickup','',%i,%image);
echo("6");
break;
}
}
echo("7");
}

And the echoes appeared in the console. I can't figure out why I'm not getting the tools.
I read in other topics that you need to send a special message to the client and increase the weaponcount. It's all there. I tried with a friend, same as me. We hear the sound but the tool doesn't appear.
« Last Edit: July 28, 2013, 10:39:02 AM by Pie Crust »

Doesn't the player persistence save items?

I looked at player persistance and at event_addItem. I compared them and they have pretty much what I have. That's why I'm confused.