2 questions for public and stock
#1

Hi!
Код:
public OOCNews(color,const string[])
{
	foreach(Player, i)
	{
	    if(gPlayerLogged[i] == 1)
	    {
			SendClientMessage(i, color, string);
	    }
	}
}
1. question: What is better to use for this: public or stock? and why you choose that?
2. question: Do i need to put return 1; to all functions on the end, when it do work in that public/stock/function, for ex this public OOCNews...

Thank you!
Reply
#2

1. Nothing, simply "OOCNews(color,const string[])".
2. No, but if you return a function that doesn't return a value, you'll get a message after compiling.
Reply
#3

Quote:
Originally Posted by Kevln
Посмотреть сообщение
1. Nothing, simply "OOCNews(color,const string[])".
2. No, but if you return a function that doesn't return a value, you'll get a message after compiling.
1. Why simply that? That will be faster
2. Like you see, i dont write return in that public and i dont get a message after compiling... I add return 1; now, and i dont get any message...
Reply
#4

Public only for callbacks. That includes SA-MP default callbacks, functions called by timers and custom callbacks created by plugins. Information about stock: https://sampforum.blast.hk/showthread.php?tid=570635

Also no, you don't need to return a value in every function. Returning a value is only useful if it actually gets used somewhere. A function that returns nothing is called a "void" function or a subroutine in many languages.
Reply
#5

Quote:
Originally Posted by DusanInfinity
Посмотреть сообщение
1. Why simply that? That will be faster
2. Like you see, i dont write return in that public and i dont get a message after compiling... I add return 1; now, and i dont get any message...
Read point 2 again and run (make it work):
pawn Код:
return Function();

Function()
{
    new value;
    value = 5;
}
Public functions use more memory and should only be used in special cases, stock functions are ignored if a function is not used anywhere in your script. Nonetheless, neither is faster; use naked functions.
Reply
#6

1)
Public is used when you want to use that function as a timer or callback, or even if you want to call the function from outside the script using CallRemoteFunction.
In all other cases you don't put anything in front of the function name.

Stock is used for include libraries.
When you write an include that holds different functions but they aren't called directly by code inside that include, you would get warnings about functions being declared which aren't used anywhere.
Putting "stock" in front of it will make the compiler ignore the entire function if it's never used and won't even include it in the compiled amx file.
It basically says: "don't compile and ignore this function if never used".

2)
Using return at the end of a function is required if there is at least one return anywhere inside that function.
pawn Код:
SomeFunction(a, b)
{
    if (a > b) return 1000;
}
Functions like this REQUIRE you to add "return 1;" at the end.
Why?
Because it has at least one return statement inside it, you declare the entire function to always return a value, in EVERY situation.
The above function will only return something if a is bigger than b.
But what happens when a is smaller than b?
The code won't know what to do in that situation.

So you make it like this:
pawn Код:
SomeFunction(a, b)
{
    if (a > b) return 1000;

    return 0;
}
If a is bigger than b, it will return 1000.
If a is equal to b or smaller than b, the if-statement will fail and the compiler needs to know what to do in that case.
By putting "return 0;" (or whatever value you prefer) you declare to return 0 in ALL other situations where you if-statement fails (a = b or a < b).

Now all situations are covered and the compiler will won't generate errors/warnings.
Reply
#7

Quote:
Originally Posted by AmigaBlizzard
Посмотреть сообщение
1)
Public is used when you want to use that function as a timer or callback, or even if you want to call the function from outside the script using CallRemoteFunction.
In all other cases you don't put anything in front of the function name.

Stock is used for include libraries.
When you write an include that holds different functions but they aren't called directly by code inside that include, you would get warnings about functions being declared which aren't used anywhere.
Putting "stock" in front of it will make the compiler ignore the entire function if it's never used and won't even include it in the compiled amx file.
It basically says: "don't compile and ignore this function if never used".

2)
Using return at the end of a function is required if there is at least one return anywhere inside that function.
pawn Код:
SomeFunction(a, b)
{
    if (a > b) return 1000;
}
Functions like this REQUIRE you to add "return 1;" at the end.
Why?
Because it has at least one return statement inside it, you declare the entire function to always return a value, in EVERY situation.
The above function will only return something if a is bigger than b.
But what happens when a is smaller than b?
The code won't know what to do in that situation.

So you make it like this:
pawn Код:
SomeFunction(a, b)
{
    if (a > b) return 1000;

    return 0;
}
If a is bigger than b, it will return 1000.
If a is equal to b or smaller than b, the if-statement will fail and the compiler needs to know what to do in that case.
By putting "return 0;" (or whatever value you prefer) you declare to return 0 in ALL other situations where you if-statement fails (a = b or a < b).

Now all situations are covered and the compiler will won't generate errors/warnings.
Thank you so much! What you think about saving data? Im using Y_INI for saving accounts...
EDIT: And if i understood you, i dont need to use stock in gamemode, just in include
Reply


Forum Jump:


Users browsing this thread: 2 Guest(s)