Raised This Month: $ Target: $400
 0% 

How does "return" work?


  
 
 
Thread Tools Display Modes
Prev Previous Post   Next Post Next
HamletEagle
AMX Mod X Plugin Approver
Join Date: Sep 2013
Location: Romania
Old 06-27-2020 , 15:48   Re: How does "return" work?
Reply With Quote #3

It's a bit weird you are able to do private work but ask basic questions. Don't get me wrong, there is absolutely nothing wrong with asking basic questions and trying to learn, but if you do private work you should at least know the basics. Anyway, none of my business.

There are 2 different things happening with return. First, if it is used in a function then it stops the execution of the function right there.
PHP Code:
some_function()
{
    if()
    {
         
some_code_1
         
return or return something
    
}

     
some_code_2

If the if branch is taken and the return executes then some_code_2 is never executed because the function exists because of the "return" and the control returns to the function that called "some_function".

The other use of return is with certain forwards, like the ones from ham for example. You can return a certain value to tell the module what to do. For example, if in a ham hook you return HAM_SUPERCEDE 2 things will happen: your function(the one registered in the ham hook) will stop and the actual event that you hooked will be blocked too.
On the other hand, if you returned HAM_IGNORED, your function will stop because it hits a return(obviously), but the underlying event will still happen and plugins loaded after yours will be able to catch it too.

Concrete example: you hooked Ham_TakeDamage like this
PHP Code:
RegisterHam(Ham_TakeDamage"player""CBasePlayer_TakeDamage"0
and then do
PHP Code:
public CBasePlayer_TakeDamage()
{
     
some_code_1
     
return HAM_SUPERCEDE
     some_code_2

some_code_2 is never executed because it is after the return. Also, the TakeDamage game function is also blocked which means the player will not take the damage.

PHP Code:
public CBasePlayer_TakeDamage()
{
     
some_code_1
     
return HAM_IGNORED
     some_code_2

some_code_2 is still not executed, but the player will take the damage because the event you hooked is not blocked(the event in this case is a player taking damage).
__________________

Last edited by HamletEagle; 06-27-2020 at 15:50.
HamletEagle is offline
 



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 17:10.


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