About Store Forum Documentation Contact



Post Reply 
Custom config saving/loading
Author Message
Barthap Offline
Member

Post: #1
Custom config saving/loading
Hello,

Everyone know (or not wink ) that Esenthel ConfigSave() and ConfigLoad() are saving/loading only predefined settings/variables to the file. Users had to create second, custom config file to save their own data.

But there is very simple method, how to save/load custom data with standard Esenthel config in one file.
For saving, we just have to call standard Esenthel ConfigSave() method, then open config as FileText and write own data. Reading config is similiar.

Here's the sample code
PHP Code:
//somewhere in game code our sample variables
Flt someFloatVariable;
Int customIntVariable;

//somewhere in Player class, name example
Str name;

//our saving method
Bool CustomSave(Str name)
{
    
//just prepare data for saving
    
Str player_name;
    if(
Players.elms())
        
player_name Players[0].name;
    else 
        
player_name "anonymous";


    
ConfigSave(name); //standard Esenthel config-save method

   
FileText f// FileText object

   
if(f.append(name)) // open at the end of file
   
{
      
f.endLine(); //make some space between default and custom data

      
f.put    ("FltVar       = "someFloatVariable); //save float data
      
f.put    ("SomeIntVar   = "customIntVariable); //save int    data
      
f.putName("PlayerName   = "player_name  );     //save player name to config

      
return true// return success
   
}
   return 
false// return fail
}

//and loading
Bool CustomLoad(Str name)
{
   
//temporary variables
    
Str player_name "anonymous"//if player name can't be loaded, it will be 'anonymous'

   
ConfigLoad(name); //Standard Esenthel config loading
   
   
FileText f;

   if(
f.read(name)) // if file opened successfully
   
{

      for(;
f.level();) // process file within its level
      
{
              if(
f.cur("FltVar"      ))someFloatVariable=f.getReal();
         else if(
f.cur("SomeIntVar"     ))customIntVariable=f.getInt ();
         else if(
f.cur("PlayerName"  ))player_name        =f.getName();  
      }

      
//some post-operations
      
if(Players.elms())
          
Players[0].name player_name//set player name


      
return true// return success
   
}
   return 
false// return failure

The int, float and player_name are just examples. You can put there your own config. You can find out more about writing/reading data reading the FileText.h header file. The comments say everithing wink

Now instead of ConfigSave() and ConfigLoad() we use CustomSave() and CustomLoad(). Done grin

Barthap
02-04-2012 03:09 PM
Find all posts by this user Quote this message in a reply
yvanvds Offline
Member

Post: #2
RE: Custom config saving/loading
Thanks, Barthap!

Another handy addition to the code snippets :-)
02-05-2012 09:37 PM
Find all posts by this user Quote this message in a reply
dragonfly3 Offline
Member

Post: #3
RE: Custom config saving/loading
Ditto! Much thanks for the contribution :-)
02-06-2012 02:49 AM
Visit this user's website Find all posts by this user Quote this message in a reply
Post Reply