Author Topic: How would i save variables to a server  (Read 2577 times)

im trying to create a money system using variables that would save to the server, so when the player joined back it would automatically load to the player thanks

Use global variables to store the data. When a client disconnects, export their variables to a file and delete them. When a client joins, execute the file to restore the variables.

Code: [Select]
export("$Variables*", "config/server/file.cs");

if it were a global variable it would be the same wallet for every player
any work around?

You can use square brackets to insert information into a variable name, like this:

Code: [Select]
$Variable[%client.bl_id] = %x;
Be careful, though, because wildcard use in the export and deleteVariables functions can affect clients other than the intended one if you use the client's blockland ID as simply as that, e.g. if you had clients with the IDs 100, 1000, and 1005.

You can use square brackets to insert information into a variable name, like this:

Code: [Select]
$Variable[%client.bl_id] = %x;
Be careful, though, because wildcard use in the export and deleteVariables functions can affect clients other than the intended one if you use the client's blockland ID as simply as that, e.g. if you had clients with the IDs 100, 1000, and 1005.
k i see kinda rare that all of them would be in 1 server though right?

That was only an example. You don't have to find a way to account for that, but if you don't, you run the risk of it causing a lot of problems.

That was only an example. You don't have to find a way to account for that, but if you don't, you run the risk of it causing a lot of problems.
any ideas if a fix?

You could use the number of digits in the ID followed by an underscore and the ID as the identifier.

ok so using the information you gave me, would somthing like this work?
(BTW this is just for first time joining)
Code: [Select]
function gameConnection::onClientEnterGame(%client)
{
if($Wallet[%client.bl_id] = 0)
   %client.Wallet = 1000;
else
   %client.Wallet = $Wallet[%client.Wallet];

}
and of course export when the player leaves the game and delete

I feel like badspot's player persistence addon would be good for this

I feel like badspot's player persistence addon would be good for this
sure would lol, haven't ever read that before

ok so using the information you gave me, would somthing like this work?
(BTW this is just for first time joining)
Code: [Select]
function gameConnection::onClientEnterGame(%client)
{
if($Wallet[%client.bl_id] = 0)
   %client.Wallet = 1000;
else
   %client.Wallet = $Wallet[%client.Wallet];

}
and of course export when the player leaves the game and delete
1. That'd need to be in a package.
2. You'd need to parent the function.
3. Use == or $= for a comparison.
4. If someone runs out of money and then rejoins then they'll get $1,000 for doing nothing.

I'd suggest something more like this:
Code: [Select]
package MyTrout
{
function gameConnection::onClientEnterGame(%client)
{
parent::onClientEnterGame(%client);

if(isFile("config/server/MyRPGData/" @ %client.BL_ID @ ".txt"))
{
exec("config/server/MyRPGData/" @ %client.BL_ID @ ".txt");
}

else
{
//They've never been to the server before, give them some starting money.
$MyRPGData[%client.BL_ID, "Wallet"] = 1000;

export("$MyRPGData" @ %client.BL_ID @ "*", "config/server/MyRPGData/" @ %client.BL_ID @ ".txt", false);
}
}

function gameConnection::onClientLeaveGame(%client)
{
export("$MyRPGData" @ %client.BL_ID @ "*", "config/server/MyRPGData/" @ %client.BL_ID @ ".txt", false);

parent::onClientLeaveGame(%client);
}
};
activatePackage(MyTrout);

EDIT: Remember, $Var["My", "Trout"] is the same as $VarMy_Trout.
« Last Edit: June 21, 2015, 10:48:56 PM by jes00 »

back to badspot's way, which is probably the way it should be done
Code: [Select]
RegisterPersistenceVar(%name, %matchAll, %matchDataBlock);
that's the function that should be used, what most of the things are I don't have the foggiest idea
probably use something like registerPersistenceVar("money", false, "client"); or something, not quite sure

here's the whole addon badspot made for this
Code: [Select]
package SupportPlayerPersistencePackage
{
   function RegisterPersistenceVar(%name, %matchAll, %matchDataBlock)
   {
      if(%matchAll)
      {
         //we need these in a list
         $Persistence::MatchAll_Count = mFloor($Persistence::MatchAll_Count);
         
         //see if it is already registered;
         for(%i = 0; %i < $Persistence::MatchAll_Count; %i++)
         {
            if($Persistence::MatchAll_Entry[%i] $= %name)
               return;
         }

         //not registered yet, add it
         $Persistence::MatchAll_Entry[$Persistence::MatchAll_Count] = %name;
         $Persistence::MatchAll_Count++;
      }
      else
      {
         //simple name match
         $Persistence::MatchName[%name] = true;
         $Persistence::MatchDatablock[%name] = %matchDataBlock;
      }     
   }

};
activatePackage(SupportPlayerPersistencePackage);

edit: found http://forum.blockland.us/index.php?topic=191184.msg5116924#msg5116924

if you want to save a variable money to the client, all you need to do is registerPersistenceVar("money", false, "");
« Last Edit: June 21, 2015, 11:33:31 PM by phflack »

This is not going to be a private add-on and I don't like forcing people to clog up they're server with other Add-ons

This is not going to be a private add-on and I don't like forcing people to clog up they're server with other Add-ons
Player persistence is default

Even if it wasn't default, support scripts could be bundled with your add-on
« Last Edit: June 22, 2015, 01:13:33 PM by Headcrab Zombie »