Public functions

From SA-MP Wiki

Jump to: navigation, search

A public function is a normal function with the public keyword in front of it. It is important to realize that callbacks are just public functions as well.

When should I use a public function?

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

Callbacks need the public keyword as well. (This also applies to Pawn functions you would like to call from a SA-MP server plug-in.)

If a function is not public while it should be, you will not receive any errors. You will only notice that the code does not work the way you want it to work. For example, a timer function will not be called.

Forwarding

All public functions must be forwarded. That is a rule of Pawn. If you do not forward a public function, you will receive warnings from the compiler. It's best to forward a function at the top of the script (but below the #include directives, though).

Suppose you have a public function:

public countdown()
{
    // code...
}

To forward it, you would add this to the top of the script:

forward countdown();


To forward, copy the function's prototype, add forward in front of it, and add a ; behind it. The prototype must be exactly the same. You will receive errors when it's not the same. Another example, this time with a parameter.

public AreaCheck(playerid)
{
    // code....
}

The forward statement would be:

forward AreaCheck(playerid);


Since it is so important that the code after public and forward is the same, it's best to copy and paste it. Here is a final example:

public Float:MashPotatoes(milk, Float:salt, bool:heated)
{
    // code...
}

It should be forwarded like this:

forward Float:MashPotatoes(milk, Float:salt, bool:heated);


Notice that there is no ; behind the line that starts with public, but there is a ; after a forward statement.

You may wonder why you don't have to forward callbacks. Well, you do have to forward them, but this is already done for you in a file you always include: a_samp.inc. Open that file (in the include folder) and scroll to the end. You will see that all callbacks are forwarded in there.

0.1 scripts

Some SA-MP 0.1 scripts (older scripts) heavily misused the public keyword. Their programmers may have added public to every function. In general, it's a better idea to remove the public keyword from them rather than forwarding. A quick search using Ctrl + F allows you to see if a function is used by a timer. If it is used by a timer, do not remove public, but forward the function instead.

Do not remove public when the function's name starts with 'On'! It is most likely to be a callback. They require the public keyword.

Personal tools
Navigation
Toolbox