Author Topic: Variable Questions  (Read 1299 times)

Could someone please answer the following questions about variables(no not the eventing kind, the scripting kind)?
  • How to add and subtract variables
  • How to save and load variables
  • How to set variables
  • How to check variables
  • How to display variables in a bottom print

Thats all for now.
« Last Edit: August 31, 2011, 09:38:40 PM by jes00 »

%var += 5;
%var -= 5;
%var *= 5;
%var /= 5;
%var = 5;

if(%var == 5)
{
   //do stuff
}

messageclient(%client, 'bottomprint', %var, 10);

To save and load I prefer

export("$var*", "config/var.cs");
exec("config/var.cs");
« Last Edit: August 28, 2011, 07:26:21 PM by Nexus »

%var += 5; So this one is higher then or equal to?
%var -= 5; And this one is lower then or equal to?
%var *= 5; What is this one?
%var /= 5; No idea what this one is.
%var = 5; And this one is equal to

if(%var == 5)
{
   //do stuff
}

messageclient(%client, 'bottomprint', %var, 10);

To save and load I prefer

export("$var*", "config/var.cs"); Does this create a .txt file in config?
exec("config/var.cs");
I'm guessing you replace %var with the variable?

Quote
%var += 5; So this one is higher then or equal to? This adds 5 to %var. Its a shorthand way of type '%var = %var + 5' If%var is equal to 15, after this, %var will be equal to 20
%var -= 5; And this one is lower then or equal to? Same as above, but subtracting 5
%var *= 5; What is this one? Same as above, but multiplying by 5
%var /= 5; No idea what this one is. Dividing by 5
%var = 5; And this one is equal to Setting %var to be equal to 5

if(%var == 5)
{
   //do stuff
}

messageclient(%client, 'bottomprint', %var, 10);

To save and load I prefer

export("$var*", "config/var.cs"); Does this create a .txt file in config? It creates a text file names var.cs, inside config folder. Inside, will be every variable whose name begins with $var. For an example, type export("$Pref*","config/prefs.cs");
exec("config/var.cs");

For the tests

if(%var < 5) if the variable is 4.99999 or less
if(%var > 5) if the variable is 5.00001 or more
if(%var >= 5) if the variable is 5 or greater
if(%var $= "some string"); if the variable matches the string

I'm guessing you replace %var with the variable?
And for saving I replace $var with the variable?


There are many ways to save and load variables.

% is for a temporary variable that gets discarded after whatever used it is finished with it. Such as:
-A console entry will discard it after evaluating everything in the entry.
-A function will discard it after it's completed.
-An executed file will discard it after the file is finished executing.

$ is for a permanent variable which will remain even after the method is complete.

So doing:

Code: [Select]
function test()
{
%test = 5;
}

test();
echo(%test);

Will echo blank, however, doing

Code: [Select]
function test()
{
$test = 5;
}

test();
echo($test);
will echo 5.


As for saving them, if you're using these kinds of variables, it's a good idea to store them with a keyword at the beginning of it, like:

$RPG::YOURVARNAMEHERE

such as

$RPG::Time


You would save all variables beginning with $RPG:: into a single file like this:

export("$RPG::*","config/server/my_rpg.cs",0);

and to load it, use exec

exec("config/server/my_rpg.cs");



Another thing to know is 'arrays' or whatever they're actually called. With this you can store variables and have a conditional reference to it. Such as:

$RPG::Gold[%client.bl_id]+=5;

This will give %client 5 gold and store it based on ID. (which only show up on client objects [or called GameConnections])
Saving and loading would work the same way.



There are other ways for storing, saving, and loading variables. But this is probably the simplist way. Not sure if it would be good on RAM after several different users have accessed your server, as this loads everyone's data whether or not they're on the server.

I'm just wondering why you all are writing the same thing over and over and not just linking him to a tutorial. I think ibans tutorial explained everything.

So how would I
  • Save and load the players data only if they join the server.
  • Display the players variable.

So how would I
  • Save and load the players data only if they join the server.
  • Display the players variable.

What do you mean by display?

Saving and loading could be done like this:
If the server is going to be running non-stop then you could get away with using a variable like:
Code: [Select]
$Var[%client.bl_id] Note that this will be erased once the game is closed.
if not,
Then you could export the variables to a file corrosponding to the player's ID:
to save:

Code: [Select]
function SaveVars(%client)
{
      export("$var*", "config/Vars/" @ %client.bl_id @ ".cs");
}
to load:
Code: [Select]
function gameConnection::AutoAdminCheck(%client) //This is called once a client joins the server
{
      parent::AutoAdminCheck(%client);
      
      exec("config/Vars/" @ %client.bl_id @ ".cs");
}

EDIT: Fixed a silly mistake
« Last Edit: August 31, 2011, 01:43:54 PM by Fluff-is-back »


Like in a bottom print.
Code in the CityRPG or even I believe Team Deathmatch(?)

I suggest not following Fluff's code. It wont work and that'll only save $var, and not $var[ID HERE]

If you were however, to use that method:

Code: [Select]
function GameConnection::saveVars(%client)
{
export("$var" @ %client.bl_id @ "*", "config/Vars/" @ %client.bl_id @ ".cs");
}
to load:
Code: [Select]
package autoLoad
{
function gameConnection::AutoAdminCheck(%client) //This is called once a client joins the server
{
if(isFile("config/Vars/" @ %client.bl_id @ ".cs"))
exec("config/Vars/" @ %client.bl_id @ ".cs");
else
%client.configureDefaultVars();
return Parent::AutoAdminCheck(%client);
}
};
activatePackage(autoLoad);
and to remove data of a user who has left the server:
Code: [Select]
package autoSave
{
function GameConnection::onClientLeaveGame(%client)
{
%client.saveVars();
deleteVariables("$var" @ %client.bl_id @ "*");
return Parent::onClientLeaveGame(%client);
}
};
activatePackage(autosave);

Explanations for each function:
Saving:
Let's say the client's ID was 5344.
It would export anything as $var[5344,*], meaning, if you were to have something like $var[5344,faction] and $var[5344,points] it would save both to the file, instead of just one variable.
Auto load:
Package the autoadmin check function, which is called whenever any player joins.
If the player has a file, load it, otherwise, give him default values (you would have to make this function yourself as GameConnection::configureDefaultValues(%client)  )
And then for the autoadmin system to work properly, return whatever the default function is supposed to.
Auto save:
Package the function for when clients leave the server.
Save the player's data using the previous function made, saveVars.
Delete the no longer needed variables.
Then continue the onLeave function.

Like in a bottom print.
Depends what you want to display.

-Lot of talking-
So would this work?
Code: [Select]
/=========================================================================
// 7. Auto Save And Load
//=========================================================================
//////////////////////
// 7.1 Auto Save /////
//////////////////////
package AutoSave
{
function GameConnection::onClientLeaveGame(%client) //This is called when a client leaves the server
{
%client.saveVars();
deleteVariables("$Gold" @ %client.bl_id @ "*");
deleteVariables("$MP" @ %client.bl_id @ "*");
deleteVariables("$Health" @ %client.bl_id @ "*");
deleteVariables("$Class" @ %client.bl_id @ "*");
return Parent::onClientLeaveGame(%client);
}
};
activatePackage(AutoSave);

//////////////////////
// 7.2 Auto Load /////
//////////////////////
package AutoLoad
{
function gameConnection::AutoAdminCheck(%client) //This is called once a client joins the server
{
if(isFile("config/Vars/" @ %client.bl_id @ ".cs"))
exec("config/Vars/" @ %client.bl_id @ ".cs");
else
%client.configureMedievalRpgSaves();
return Parent::AutoAdminCheck(%client);
}
};
activatePackage(AutoLoad);
And to save a var
Code: [Select]
export("$VarName" @ %client.bl_id @ "*", "config/MedievalRpgSaves/" @ %client.bl_id @ ".cs");
Depends what you want to display.
For example: how much gold the player has.
« Last Edit: August 31, 2011, 12:56:16 PM by jes00 »