Can someone explain me the right way of using forward/stock/public...
#1

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

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.
Reply
#3

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?)
Reply
#4

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.
Reply
#5

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

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;
}
Reply
#7

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

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

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?
Reply
#10

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.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)