Raised This Month: $13 Target: $400
 3% 

How does "return" work?


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
supertrio17
Senior Member
Join Date: May 2020
Location: Serbia
Old 06-27-2020 , 13:53   How does "return" work?
Reply With Quote #1

Hey, so I started coding with Pawn like 3-4 weeks, and I think that I'm pretty good for that small amount of time, but there is one thing that I don't really understand.

How does return work actually work? So if I set like PLUGIN_HANDLED in else statement, it said it would disable from going to other plugins, how does that "go" to other plugins.

In this code here, made by OciXCrom, what is returning, and where does it go (sorry if I sound like a total dumbass), and what does "?" mean?
PHP Code:
return cs_get_user_team(iVictim) == cs_get_user_team(iAttacker) ? HAM_SUPERCEDE HAM_IGNORED 
Can anyone explain to me what does it do (more in-depth than disables from going to other plugins)
And how I can be useful.

Sorry if a thread like this already exists.
__________________
Contact! || Discord:
Mr_Boopsy_#2066

Last edited by supertrio17; 06-27-2020 at 13:54.
supertrio17 is offline
fysiks
Veteran Member
Join Date: Sep 2007
Location: Flatland, USA
Old 06-27-2020 , 15:41   Re: How does "return" work?
Reply With Quote #2

In general, a return value is the value returned by the function. If the function is being called from a hook provided by a module then there are special constants to signify to the hooking module what do do. This depends on the module for what the different values do. For AMX Mod X module, you have PLUGIN_* constants then for fakemeta you have FMRES_* constants and for Hamsandwich you have HAM_* constants.

Each of the modules have their own meaning for the return values and you can look at return value definitions (in their include file for constants, usually *_const.inc) and if that isn't enough, you can search specifically for the one you want on the forum (I've seen threads that talk about most of them if not all).

For Fakemeta and Hamsandwich, if you hook a function as a pre-hook then certain return values will allow you to modify the function before it is sent to the game engine or prevent the function from occurring at all. For example, if you hook Ham_TakeDamage as pre, you can change how much damage the engine thinks was taken by a player.

Here is one such explanation of PLUGIN_* values which is basically what it says in amxconst.inc.

The question mark is the ternary operator. Search for it online (and I'm sure it's been explained here as well).
__________________

Last edited by fysiks; 06-27-2020 at 15:49.
fysiks is offline
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
supertrio17
Senior Member
Join Date: May 2020
Location: Serbia
Old 06-27-2020 , 18:13   Re: How does "return" work?
Reply With Quote #4

Quote:
Originally Posted by HamletEagle View Post
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.
Well, to be honest, I always get around it somehow, and I realized that it may be way easier to do things like that. And it's not really Private Work (as paid work), it's just an easier way for someone to contact me for help with a plugin.
__________________
Contact! || Discord:
Mr_Boopsy_#2066
supertrio17 is offline
Bugsy
AMX Mod X Moderator
Join Date: Feb 2005
Location: NJ, USA
Old 06-28-2020 , 00:34   Re: How does "return" work?
Reply With Quote #5

I don't think you are experienced enough to be providing help.
__________________
Bugsy is offline
supertrio17
Senior Member
Join Date: May 2020
Location: Serbia
Old 06-28-2020 , 07:13   Re: How does "return" work?
Reply With Quote #6

And how did you get to that conclusion
__________________
Contact! || Discord:
Mr_Boopsy_#2066
supertrio17 is offline
HamletEagle
AMX Mod X Plugin Approver
Join Date: Sep 2013
Location: Romania
Old 06-28-2020 , 08:26   Re: How does "return" work?
Reply With Quote #7

Quote:
Originally Posted by supertrio17 View Post
And how did you get to that conclusion
Let me guess: by your topics and posts and because you stated you started coding 3 weeks ago. At this point you should really focus on learning and understanding how things work.
__________________
HamletEagle is offline
AnimalMonster
Member
Join Date: May 2020
Old 06-29-2020 , 22:32   Re: How does "return" work?
Reply With Quote #8

Quote:
Originally Posted by HamletEagle View Post
Let me guess: by your topics and posts and because you stated you started coding 3 weeks ago. At this point you should really focus on learning and understanding how things work.
To be honest about me, i started 3 weeks ago to code pawn and when i tried to code my first time i was guiding myself through others' plugins and somehow i learned very fast things. It's amazing how the things work, just guiding yourself throguh plugins is the easiest way ever to learn something
AnimalMonster is offline
supertrio17
Senior Member
Join Date: May 2020
Location: Serbia
Old 06-30-2020 , 10:58   Re: How does "return" work?
Reply With Quote #9

Quote:
Originally Posted by HamletEagle View Post
Let me guess: by your topics and posts and because you stated you started coding 3 weeks ago. At this point you should really focus on learning and understanding how things work.
Well, that's why I'm making this thread, I don't know how return works.
__________________
Contact! || Discord:
Mr_Boopsy_#2066
supertrio17 is offline
Reply


Thread Tools
Display Modes

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 15:09.


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