SA-MP Forums Archive
Dude - 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: Dude (/showthread.php?tid=647542)



Dude - Juance - 06.01.2018

Hello everyone. I have a little doubt about how to declare functions to use in the Gamemode

Код:
forward MyFunction();
public MyFunction() {

// rest of code

}

stock MyFunction(){

// rest of code

}

MyFunction(){

// rest of code

}
What are their differences and what is the recommended way?


Re: Dude - jasperschellekens - 06.01.2018

Код:
MyFunction(){

// rest of code

}
read this:
[Tutorial] Stop the abuse of stock!


Re: Dude - ThePhenix - 06.01.2018

Quote:
Originally Posted by Juance
Посмотреть сообщение
Hello everyone. I have a little doubt about how to declare functions to use in the Gamemode

Код:
forward MyFunction();
public MyFunction() {

// rest of code

}

stock MyFunction(){

// rest of code

}

MyFunction(){

// rest of code

}
What are their differences and what is the recommended way?
Public functions allow you to define new entry points that the host application (the sa-mp server) can communicate with your script. The host application keeps track of all the public functions you create. You usually use public functions for callbacks but you can also declare public variables (maybe a plugin to communicate with your script?)

Stock functions are usually used in libraries, the compiler won't throw the warning 203 (symbol is never used: "MyFunction") if you're not using a certain stock function as it won't include it.

PHP код:
MyFunction(){
// rest of code

You would use a function like that if you're sure you will use it, if you wind up not using a function like that you would get a warning:

Код:
warning 203: symbol is never used: "MyFunction"



Re: Dude - Juance - 06.01.2018

Quote:
Originally Posted by ThePhenix
Посмотреть сообщение
PHP код:
MyFunction(){
// rest of code

So this is the best way to declare functions to use in commands?


Re: Dude - Dayrion - 06.01.2018

Quote:
Originally Posted by Juance
Посмотреть сообщение
So this is the best way to declare functions to use in commands?
For commands, yes.
Function declaration depends on what you are working. If it's an include, yes you can need stock keyword, if you need make the function public for a timer than you need public and forward it, etcc...


Re: Dude - Juance - 06.01.2018

And if I am working my gamemode with includes and I do not write commands in my gamemode but in external includes? there I have to use stock, or can I use only the name of the function too?


Re: Dude - Dayrion - 06.01.2018

Quote:
Originally Posted by Juance
Посмотреть сообщение
And if I am working my gamemode with includes and I do not write commands in my gamemode but in external includes? there I have to use stock, or can I use only the name of the function too?
Depends on what you do. I mean, you should read and learn what these keywords mean.


Re: Dude - ThePhenix - 06.01.2018

I mean, it all depends on how you want to organize your gamemode. Some people might tell you that it is not recommended to use the stock keyword but as I said it depends. The only thing the "stock" keyword does is inform the compiler that it must include the function if the function is used. If the function is not used then the pawn parser can simply ignore the function. As the PAWN language guide (which you should probably read by the way) says:

Quote:

Stock functions allow a compiler or interpreter to optimize the memory footprint and the file size of a (compiled) pawn program: any stock function that is not referred to, is completely skipped —as if it were lacking from the source file.

A typical use of stock functions, hence, is in the creation of a set of “library” functions. A collection of general purpose functions, all marked as “stock” may be put in a separate include file, which is then included in any pawn script. Only the library functions that are actually used get “linked” in.