29.09.2012, 07:22
Synchronizing variables in includes [Not suggested for newbies]
Hi there, before we begin with this tutorial, let me explain what it's all about: You want to build the function in an include "GivePlayerApples(playerid, apples)", which save apples to a variable. Sure, you define the variable by using "new pApples;", then edit the function to "stock GivePlayerApples(playerid, apples) { pApples = apples;}", and you're done. Then you build "stock GetPlayerApples(playerid) { return apples; }" which returns the apples that the player has. Fine, it would work on every script IF you GivePlayerApples in the same script. But, what if you've included the include in two different scripts? For example, you've included it in a script named "script1", then you've included it in a script named "script2"? GivePlayerApples will give the player apples only in the script you've used it in. The other script will not even know that you've used GivePlayerApples, therefore the values of the two variables named "apples" will be different. And that's what this tutorial shows you how to prevent. Let's begin now.
As I said, firstly you need to define the variables in which you want to store data in your include.
Now, you need to build the function that is going to edit the variable. It's like the "set" function in some OOP languages.
Then comes the public function itself:
Now, it's time for the stock function that you're actually going to use in your scripts!
Well that's about all I can say for setting a variable's value. How can you get it? The same way.
First you forward the public function:
Then you build it:
And then you build the usable stock itself:
And that's pretty much all of it. Your include is ready! Now, the pApples variable is synchronized between the includes/scripts and there's no matter which scripts you're including the include in. The variable will be the same. You can just use "GetPlayerApples" and "SetPlayerApples" in your scripts, after you've included the include.
Hi there, before we begin with this tutorial, let me explain what it's all about: You want to build the function in an include "GivePlayerApples(playerid, apples)", which save apples to a variable. Sure, you define the variable by using "new pApples;", then edit the function to "stock GivePlayerApples(playerid, apples) { pApples = apples;}", and you're done. Then you build "stock GetPlayerApples(playerid) { return apples; }" which returns the apples that the player has. Fine, it would work on every script IF you GivePlayerApples in the same script. But, what if you've included the include in two different scripts? For example, you've included it in a script named "script1", then you've included it in a script named "script2"? GivePlayerApples will give the player apples only in the script you've used it in. The other script will not even know that you've used GivePlayerApples, therefore the values of the two variables named "apples" will be different. And that's what this tutorial shows you how to prevent. Let's begin now.
As I said, firstly you need to define the variables in which you want to store data in your include.
pawn Code:
new pApples[MAX_PLAYERS]; //I don't really have to explain what that does, do I?
pawn Code:
forward Sync_GivePlayerApples(playerid, apples); // Since it must be a public function, it has to be forwarded first.
pawn Code:
public Sync_GivePlayerApples(playerid, apples) // This line defines the callback.
{
pApples[playerid] += apples; // This sets the pApples variable for the playerid to the apple count + new apples
return 1;
}
pawn Code:
stock GivePlayerApples(playerid, apples) // This line defines the stock
{
CallRemoteFunction("Sync_GivePlayerApples", "ii", playerid, apples); // This line calls all the "Sync_GivePlayerApples" functions, used in the whole server. Therefore, all the variables are being set at once. If you don't know how to use "CallRemoteFunctions", check this out https://sampwiki.blast.hk/wiki/CallRemoteFunction
}
First you forward the public function:
pawn Code:
forward Sync_GetPlayerApples(playerid);
pawn Code:
public Sync_GetPlayerApples(playerid)
{
return pApples[playerid]; // Returns the apples of the specified playerid
}
pawn Code:
stock GetPlayerApples(playerid)
{
return CallLocalFunction("Sync_GetPlayerApples", "i", playerid); // Returns the apples
}