AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   How does "return" work? (https://forums.alliedmods.net/showthread.php?t=325574)

supertrio17 06-27-2020 13:53

How does "return" work?
 
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.

fysiks 06-27-2020 15:41

Re: How does "return" work?
 
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).

HamletEagle 06-27-2020 15:48

Re: How does "return" work?
 
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).

supertrio17 06-27-2020 18:13

Re: How does "return" work?
 
Quote:

Originally Posted by HamletEagle (Post 2707583)
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.

Bugsy 06-28-2020 00:34

Re: How does "return" work?
 
I don't think you are experienced enough to be providing help.

supertrio17 06-28-2020 07:13

Re: How does "return" work?
 
And how did you get to that conclusion :stupid:

HamletEagle 06-28-2020 08:26

Re: How does "return" work?
 
Quote:

Originally Posted by supertrio17 (Post 2707642)
And how did you get to that conclusion :stupid:

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.

AnimalMonster 06-29-2020 22:32

Re: How does "return" work?
 
Quote:

Originally Posted by HamletEagle (Post 2707647)
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

supertrio17 06-30-2020 10:58

Re: How does "return" work?
 
Quote:

Originally Posted by HamletEagle (Post 2707647)
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.


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

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