Author Topic: Saving variables using export function  (Read 643 times)

I need to be able to save variables/info that won't erase after I shutdown my server. I know how to create and
have it write in a .txt file, but from what I'm hearing, this way is more efficient to use.

This is the demonstration we'll work with:
Code: [Select]
function serverCMDaddMoney(%client, %target, %amount)
{
%targ = findClientByName(%target);

        //Just checking things here...
if(!%client.isSuperAdmin)
{
return;
}
if(!isObject(%targ))
{
%client.chatMessage("Invalid client!");
return;
}
if(%amount <= 0)
{
%client.chatMessage("Invalid amount!");
return;
}

%targ.money = %targ.money + %amount;
announce("\c3"@%targ.name@" \c6has gained \c2$"@%amount);
}
As shown, the function has the ability to add 'money' to a client. Now all I need is for it to save
so that they'll still have their money when the server restarts (or when they leave and come back).

You would use their BLID.
example:
Code: [Select]
package rageQuit
{
    function serverCmdRageQuit(%client)
    {
        messageall('', "\c5" @ %client.name SPC "\c6has ragequit.");
        $Ragequit::isRaging[%client.bl_id] = 1;
        %client.delete("You have ragequit.");
    }
    function GameConnection::autoadmincheck(%client)
    {
        if($Ragequit::isRaging[%client.bl_id])
        {
            messageall('', "\c6Someone is back from raging, Have you raged enough,\c5" @%client.name@ "\c6?");
            $Ragequit::isRaging[%client.bl_id] = 0;
        }
        return parent::autoadmincheck(%client);
    }
};
activatePackage(rageQuit);
Also you need a global variable.

That still does not show how you can export the variables.

Oops, didn't see the text file part. sry.

Code: [Select]
$SomeNamingScheme = "asdf";
$SomeNamingScheme1234 = "random examples";
$SomeNamingScheme_foo = "bar";
export("$SomeNamingScheme*", "config/path/to/file.cs");

Anything matching the pattern $SomeNamingScheme* will be saved. To load them, just use exec("config/path/to/file.cs");

* is a wild card so anything before the wild card is what will save.

$Pref::* will save all $Pref:: variables like $Pref::Server::WelcomeMessage and $Pref::Client::SomeThingHere

ex:
export("$Pref::Server::*", "config/server/prefs.cs");

Just be aware you can't save local variables