Author Topic: Store values to file correlated with a BL_ID, read back later  (Read 976 times)

So I'm writing a simple money mod and I want it to save players' money to file once the server ends or somebody leaves, and be able to load it back up once they rejoin.

Store it to an array in form $money[%bl_id] = num, and use export("$money*", "config/money.cs");

Load with exec("config/money.cs");

You can also use default persistence.

http://forum.blockland.us/index.php?topic=174411.msg4480468#msg4480468

Add-On developers:  You can use player persistence to save any tagged field on the client or player object by using the RegisterPersistenceVar() function.  Here is some example code:
Code: [Select]
//load the persistence add-on now, if they have it enabled.
//you can also use ForceRequiredAddOn if your add-on doesn't work without persistence
LoadRequiredAddOn("Script_Player_Persistence");

if(isFunction(RegisterPersistenceVar))  //quick way to check if the persistence add-on loaded
{
   //usage: RegisterPersistenceVar(string <tagged field to match>, bool <match all>, string <datablock classname>);

   RegisterPersistenceVar("myVariable", false, "");             // save %client.myVariable and %player.myVariable to persistence file (if those values are set)

   RegisterPersistenceVar("myPrefix", true, "");                // save any tagged field on %client/%player starting with "myPrefix",
                                                                // eg %client.myPrefixFoo %client.myPrefix_24, etc.
                                                                // This is more expensive than a straight match and can have unintended consequences, so be careful

   RegisterPersistenceVar("myDatablockRef", false, "ItemData"); // save %client/%player.myDatablockRef to persistence but verifies that it is of class "ItemData"
                                                                // and translates the id number into a datablock name before saving. 
                                                                // Datablock id numbers will vary depending on the order and number of add-ons loaded,
                                                                // so use this if you need to save a datablock reference.
}

//optionally, you can package into the apply persistence function if you need to do custom stuff to fully restore the player's state
package <MyAddOn>PersistencePackage
{
   function GameConnection::applyPersistence(%client, %gotPlayer, %gotCamera)
   {
      Parent::applyPersistence(%client, %gotPlayer, %gotCamera);
     
      //Do custom stuff here...
   }
};
activatePackage(<MyAddOn>PersistencePackage);
Player Persistence files are saved as config/server/persistence/<blid>.txt


What exactly does the match all bool do?
Basically lets you register just once to save a set of data that all starts with a similar prefix. Look at the example in the comments next to it.

Basically lets you register just once to save a set of data that all starts with a similar prefix. Look at the example in the comments next to it.
Oh! I see. So if I did this at the beginning of my script:
Code: [Select]
LoadRequiredAddOn("Script_Player_Persistence");

if(isFunction(RegisterPersistenceVar))
{
  RegisterPersistenceVar("studs", false, "");
}

Any changes I would make to %client.studs would be persistent and saved? I don't have to manually load or save the variables?