Raised This Month: $ Target: $400
 0% 

Question about return


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
redivcram
Veteran Member
Join Date: Jul 2014
Location: Serbia
Old 01-23-2016 , 17:22   Question about return
Reply With Quote #1

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.

Last edited by redivcram; 01-23-2016 at 17:23.
redivcram is offline
fysiks
Veteran Member
Join Date: Sep 2007
Location: Flatland, USA
Old 01-23-2016 , 20:02   Re: Question about return
Reply With Quote #2

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

__________________

Last edited by fysiks; 01-23-2016 at 20:07.
fysiks is offline
redivcram
Veteran Member
Join Date: Jul 2014
Location: Serbia
Old 01-25-2016 , 08:01   Re: Question about return
Reply With Quote #3

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.
redivcram is offline
fysiks
Veteran Member
Join Date: Sep 2007
Location: Flatland, USA
Old 01-25-2016 , 08:25   Re: Question about return
Reply With Quote #4

Quote:
Originally Posted by redivcram View Post
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.
__________________
fysiks is offline
Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


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


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