SA-MP Forums Archive
Can someone explain me the right way of using forward/stock/public... - Printable Version

+- SA-MP Forums Archive (https://sampforum.blast.hk)
+-- Forum: SA-MP Scripting and Plugins (https://sampforum.blast.hk/forumdisplay.php?fid=8)
+--- Forum: Scripting Help (https://sampforum.blast.hk/forumdisplay.php?fid=12)
+--- Thread: Can someone explain me the right way of using forward/stock/public... (/showthread.php?tid=459549)



Can someone explain me the right way of using forward/stock/public... - aLTeR - 23.08.2013

Can someone explain me, when should i use forward,stock,public...?
Explain by examples with explanation please.


Re: Can someone explain me the right way of using forward/stock/public... - Konstantinos - 23.08.2013

Read the Pawn Language Guide, it's very useful. And just to let you know, use stock for custom functions and public for callbacks that are required by timers or callremote/localfunction etc.


Re: Can someone explain me the right way of using forward/stock/public... - aLTeR - 23.08.2013

Sorry but i need examples, like:
SetPlayerJob(playerid) (What Type function should be?)
DestroyCustomTextdraws(playerid) (What Type function should be?)
OnPlayerPressEnter(playerid) (What Type function should be?)
CreateObjects() (What Type function should be?)


Re: Can someone explain me the right way of using forward/stock/public... - Konstantinos - 23.08.2013

It has nothing to do with the code inside or the function's name, but its usage.

If you use SetTimer, SetTimerEx, CallRemoteFunction, CallLocalFunction, then you must use forward/public. However, if you use just a function to prevent from writing the code all the time, then you should use a stock.


Re: Can someone explain me the right way of using forward/stock/public... - aLTeR - 23.08.2013

Still don't understand, im asking you to explain, what difference between them, because i saw when forwards are using not only for timers.


Re: Can someone explain me the right way of using forward/stock/public... - Dragonsaurus - 23.08.2013

Forward is used when you create a new function.
You can use it if you want to create one like "SaveStats", so you should use:
pawn Код:
forward SaveStats(playerid);
public SaveStats(playerid)
{
     // Stuff here...
     return 1;
}
On the other hand, stocks don't need forwards, so you can just use:
pawn Код:
stock SaveStats(playerid)
{
     // Some other stuff...
     return 1; // Not necessary...
}
You can also use stock if you want to return some variable/string, cause forward and public won't allow you to do that:
pawn Код:
stock CreateString(playerid)
{
     new string[256];
     format(string, sizeof(string), "My ID is %d", playerid);
     return string;
}



Re: Can someone explain me the right way of using forward/stock/public... - aLTeR - 23.08.2013

Whay for function SavePlayerData(playerid) , is using forward? Whay not stock, or simple public?


Re: Can someone explain me the right way of using forward/stock/public... - Konstantinos - 23.08.2013

Read this: Public functions - "When should I use a public function?"


Re: Can someone explain me the right way of using forward/stock/public... - aLTeR - 23.08.2013

Whay SaveData function is using like this:
forward SaveData(playerid);
public SaveData(playerid) {}

But not like this?
stock SaveData(playerid) {}
Or this?
SaveData(playerid) {}
Or this?
forward SaveData(playerid);
SaveData(playerid) {}

Should i use always forward, when function not returns any value?
And when it returns, then stock?


Re: Can someone explain me the right way of using forward/stock/public... - Konstantinos - 23.08.2013

Quote:
In general, functions do not need the public keyword. Don't add public to a function just because it looks nice. If you are an experienced scripter, it's quite easy to understand when a function should be public:
A function should be public whenever "the server" must be able to call the function at run time.
Always add public to your function when it is called by
- a timer, (see also SetTimer and SetTimerEx),
- CallLocalFunction,
- CallRemoteFunction.

Otherwise, use stock.

Re-read it again and again until you understand it.