AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   Question about return (https://forums.alliedmods.net/showthread.php?t=278050)

redivcram 01-23-2016 17:22

Question about return
 
First, let's post the code...
PHP Code:

public PlayerKilled(victimattacker);
{
    
gIsFreeday[id] = false// line 3
    
gHasCrowbar[id] = false// line 4
    
    
CheckSimon(victim); // line 6
    
RewardEur(victimattacker); // line 7
}

public 
CheckSimon(id)
{
    if(!
gIsSimon[id])
        return 
PLUGIN_CONTINUE// line 13

    
gIsSimon[id] = false// line 15
    
gSimon 0;
    
ColorChat(0GREEN"[Jail] Simon je ubijen, neko bi trebao da ga zameni");


Now that's an example (I'm not gonna post it, It's for me only, don't worry, I know there are hundreds of jail plugins). When a player dies, in the function PlayerKilled It's supposed to set his variables to false (line 3 and 4), then call the function CheckSimon (line 6). But after doing CheckSimon, It's supposed to call RewardEur (line 7). Now. Inside the CheckSimon function. It's supposed to check if the user is simon. If not then It halts the function. If I use PLUGIN_HANDLED instead of PLUGIN_CONTINUE on line 13, will it also halt the previous function where CheckSimon was called? And does PLUGIN_CONTINUE actually halt this function but continues with CheckSimon?
I'm a bit confused so if you can't understand me go ahead.

fysiks 01-23-2016 20:02

Re: Question about return
 
A return value is the value that a function returns to the calling code. Also, returning in the middle of a function ends execution of that function only (not the code that called the function).

Returning PLUGIN_HANDLED or PLUGIN_CONTINUE is only relevant to the function that is called by the hook/forward (i.e. those values are only interpreted in the way you think when handled by the hook/forward). If you use these values in any other functions, they will just be numbers being returned by the function.

When you return a value in CheckSimon(), it will output it as it's result:
PHP Code:

new value_returned_by_checksimon CheckSimon() 

If you want to base code execution on the result of checking if someone is simon, you need to do that explicitly in the PlayerKilled function.

PHP Code:

public hookPlayerKilled(idattacker)
{
    if( 
is_user_simon(id) )
    {
        
// Victim is Simon
    
}
    else
    {
        
// Victim is not Simon
    
}
}

bool:is_user_simon(id)
{
    if( 
gIsSimon[id] == )
    {
        return 
true
    
}
    return 
false



redivcram 01-25-2016 08:01

Re: Question about return
 
Ok thanks. About the if statements... is it legal (for the plugin) to have an if statement without the else.. and multiple ifs so if the statement is false it just keeps going under the statement.

fysiks 01-25-2016 08:25

Re: Question about return
 
Quote:

Originally Posted by redivcram (Post 2386329)
About the if statements... is it legal (for the plugin) to have an if statement without the else.. and multiple ifs so if the statement is false it just keeps going under the statement.

Yes. If you notice in the code above, I did just that, an if without the else. You should notice that it is actually extremely common.


All times are GMT -4. The time now is 09:26.

Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.