Calling functions
#1

Hi, can someone explain the sense behind putting a forward before a public function? Because it makes no sense to me.
When do I need to put the forward before a public function and why?
I am used to just calling the specific method/function from another method, for example:
public void Test() {
AnotherTest();
}

public void AnotherTest() {
//...
}
How does it work here? How do I call a function from another function?
And also: Here return 1 is success and 0 is failure? It seems that 1 is failure and 0 is success in other languages.
Reply
#2

So here some things to say:

public functions need a forward, so that the compiler knows the parameter of this function.
But you dont need always public functions, only if you wanna use it in Timers or a function like CallLocal/Remotefunction
..all other things you can do with:

PHP код:
stock func() //stock means...if u dont use this function, it gets ignored from the compiler (u don't need it..if u use the func)
{
}
//So a public function:
forward func(i);
public 
func(i)
{

The return value in your own functions..you can decide what it means...you can return anything...
But in Callbacks like (for example) OnPlayerText return 1 means the message gets send global..return 0 means...it doesn't get send..so every callback handles the return value..on their own way..so you have read in the wiki..if he handles it..or if not
Reply
#3

About the returns: it makes sense to return 0 as success if any other value is to be interpreted as an error code. For normal boolean logic on the other hand it makes sense to return true as success and false as failure. In such instances I find the use of the actual true and false keywords preferable over the use of the numerical 1 and 0.
Reply
#4

Thanks Kaliber and Vince So basically, here in PAWN some callbacks use return 0 as false and some use 0 as true? And I need to check the wiki to see what it returns? Sorry if I misunderstood.
And if I didn't misunderstand you, then in functions that I create, what should they return for false and what should they return for true?
Oh and also, return false should only be returned when there is only an error with the syntax of the command, right? Instead of returning false I can return a string too, am I right? For example, if the user is attempting to use a command for admins only and he is not an admin, my command should return true and not false? And if he got an error in the syntax I can return something like "Usage: blablabla"?
Reply
#5

Whatever a function returns is completely up to you. It depends on what the function is for, of course.

Example:
PHP код:
IsAPoliceOfficer(playerid
The only possible answer this function can give you back is either 'yes' or 'no' (translated to true or false). Where true means that the playerid is an officer and false is not. This only seems logical. You can interpret false as true and true as false when using the function:
PHP код:
if(!IsAPoliceOfficer(playerid)) {
    
// Execute code for when the player is not a police officer
    // It's up to you to choose if that means the police functionality is available here
}
else {
    
// Otherwise.. Do something else

An example where other return values are used:
PHP код:
// parameter 'vehicleid' has the value of whatever GetPlayerVehicleID(playerid) returns
GetCustomVehicleArrayID(vehicleid) {
    for(new 
iMAX_CUSTOM_VEHICLESi++) {
        if(
vehicleid == CustomVehicle[i]) {
            return 
i// returns the index of the vehicle in CustomVehicle
        
}
    }
    return -
1// If the index isn't found, the vehicle doesn't belong to CustomVehicle

And there are many more. You should definitely read more about the different function initializers (public, stock, no keyword...): https://sampwiki.blast.hk/wiki/Keywords:Initialisers
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)