Raised This Month: $32 Target: $400
 8% 

public Keyword in Menu Handlers


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
adma
Senior Member
Join Date: Oct 2015
Old 01-20-2019 , 11:55   public Keyword in Menu Handlers
Reply With Quote #1

Hi,
The wiki tutorial on menus states:
Quote:
MenuHandler1 is a function whose signature matches the MenuHandler callback. It can be named something other than MenuHandler1. It must always be public...
This statement says that menu handlers must be public functions. From my understanding of the public keyword, this statement does not make sense to me. I understood that the public keyword is used to expose the function to Sourcemod and external plugins. Since a MenuHandler is passed when creating the menu, why would it be necessary to make the handler callback public?

Additionally: I've seen callbacks for commands (i.e. RegConsoleCmd) use the public keyword too. From my usage, I've found it unnecessary. So ultimately my question would be: in what cases is the public keyword necessary, outside of global forwards like OnClientPutInServer etc?

Thanks,
adma

Last edited by adma; 01-20-2019 at 12:00.
adma is offline
Neuro Toxin
Veteran Member
Join Date: Oct 2013
Location: { closing the void; }
Old 01-20-2019 , 15:08   Re: public Keyword in Menu Handlers
Reply With Quote #2

My understanding is that Sourcemod itself can only call public functions within your plugin. Hence any callbacks or forwards must be marked public.
__________________
Neuro Toxin is offline
klippy
AlliedModders Donor
Join Date: May 2013
Location: Serbia
Old 01-20-2019 , 15:26   Re: public Keyword in Menu Handlers
Reply With Quote #3

I may be wrong, but iirc I read that there was an update that made it so all functions are implicitly public and that's why you don't see a difference when you remove the keyword

Public functions won't throw "unused" warnings and also any parameters you don't use in the function body won't throw the warning either. Just make any function you provide to another function (creating callbacks) public.
__________________

Last edited by klippy; 01-20-2019 at 15:26.
klippy is offline
adma
Senior Member
Join Date: Oct 2015
Old 01-20-2019 , 20:40   Re: public Keyword in Menu Handlers
Reply With Quote #4

Quote:
Originally Posted by KliPPy View Post
I may be wrong, but iirc I read that there was an update that made it so all functions are implicitly public and that's why you don't see a difference when you remove the keyword

Public functions won't throw "unused" warnings and also any parameters you don't use in the function body won't throw the warning either. Just make any function you provide to another function (creating callbacks) public.
Hi, thanks for your reply.
Take this script:
PHP Code:
#include <sourcemod>

public void OnPluginStart() {
  
RegAdminCmd("sm_public"sm_publicADMFLAG_KICK___);
}

// this function is passed as a callback to RegAdminCmd
Action sm_public(int clientint args) {
  
// args is not used, compiler does not complain
  
func('a');
  
RequestFrame(func2);

  return 
Plugin_Handled;
}

// this function is not passed as a callback anywhere
void func(any data) { 
  
// data is not used, compiler complains
}

// this function is passed as a callback to RequestFrame()
void func2(any data) { 
  
// data is not used, compiler does not complain

The compiler only complains about the parameter 'data' not being used in the function 'func', as func is not used as a callback anywhere. Compare this to func2, which is passed as a callback to RequestFrame(). Even though the func2 definition does not use the parameter, the compiler does not complain. So, it seems in 1.9 the compiler knows if a function has been passed as a callback to another function somewhere, and makes it public (explains the unused parameter warning suppression). Therefore, I don't think its necessary to use public when defining callback functions anymore. Would there be any situation where this would have adverse effects, other than stylistic/readability reasons?

Thank you,
adma

Last edited by adma; 01-20-2019 at 20:42. Reason: comments
adma is offline
Neuro Toxin
Veteran Member
Join Date: Oct 2013
Location: { closing the void; }
Old 01-21-2019 , 02:02   Re: public Keyword in Menu Handlers
Reply With Quote #5

From what im seeing the compiler is making the functions in your example public.

My view is you should still include the scoping for readability: public, stock, ect
__________________
Neuro Toxin is offline
headline
SourceMod Moderator
Join Date: Mar 2015
Old 01-21-2019 , 04:13   Re: public Keyword in Menu Handlers
Reply With Quote #6

Quote:
Originally Posted by adma View Post
Would there be any situation where this would have adverse effects, other than stylistic/readability reasons?
Thank you,
adma
No
headline is offline
adma
Senior Member
Join Date: Oct 2015
Old 01-21-2019 , 04:22   Re: public Keyword in Menu Handlers
Reply With Quote #7

Quote:
Originally Posted by Neuro Toxin View Post
From what im seeing the compiler is making the functions in your example public.

My view is you should still include the scoping for readability: public, stock, ect
Yep, seems like the compiler looks at functions that have been passed as parameters and makes them public. IK its just semantics, but I like the idea of omitting public from callbacks. Allows reader to know if a function is handled externally (i.e. OnClientPutInServer) if that makes sense.
adma is offline
headline
SourceMod Moderator
Join Date: Mar 2015
Old 01-21-2019 , 04:56   Re: public Keyword in Menu Handlers
Reply With Quote #8

Quote:
Originally Posted by adma View Post
..but I like the idea of omitting public from callbacks. Allows reader to know if a function is handled externally (i.e. OnClientPutInServer) if that makes sense.
except it is still handled and called externally
headline is offline
adma
Senior Member
Join Date: Oct 2015
Old 01-21-2019 , 06:33   Re: public Keyword in Menu Handlers
Reply With Quote #9

Quote:
Originally Posted by Headline View Post
except it is still handled and called externally
Ah yes, woops. That makes sense to me now. At the time of writing, I was thinking about menus , where even though the callback is handled externally, you control when it's going to be used (after displaying menu). I mistook that for internal(?) calling of the callback.

Thanks for the info guys
adma 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 12:18.


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