Raised This Month: $ Target: $400
 0% 

Call public function by string


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
Backstabnoob
BANNED
Join Date: Feb 2009
Location: Iwotadai Dorm
Old 12-29-2013 , 19:19   Call public function by string
Reply With Quote #1

I'm creating a shopmenu and I want to make it as easy to configure as possible. As a result, I'm creating a global constant array, where you specify all the items as well as the function name which gets called once the item is selected, with passed data. What is the best method of calling this function?


Code:
enum _: itemData {    _ItemName[ 32 ],    // ...    _ItemFuncToCall[ 32 ] } new const g_Items[ ][ itemData ] = {    // ...    { "Some Item", /* ... */ "func_name" } } // the best way to call this with passing data array? public func_name( Data[ ] ) {    // some code }


set_task is a possibility but I dislike the delay. There must be a better way.

Edit: Ability to stop execution of the function where this was called from would also be handful. It's possible with fakemeta forwards, but do I really need to use fakemeta forwards for one single plugin?

Last edited by Backstabnoob; 12-29-2013 at 19:22.
Backstabnoob is offline
mottzi
Veteran Member
Join Date: May 2010
Location: Switzerland
Old 12-29-2013 , 19:55   Re: Call public function by string
Reply With Quote #2

have a look here: https://forums.alliedmods.net/showthread.php?t=224430
the shop-part of the plugin.
mottzi is offline
Send a message via MSN to mottzi
Backstabnoob
BANNED
Join Date: Feb 2009
Location: Iwotadai Dorm
Old 12-29-2013 , 20:00   Re: Call public function by string
Reply With Quote #3

He uses callfunc which is silly.
Backstabnoob is offline
fysiks
Veteran Member
Join Date: Sep 2007
Location: Flatland, USA
Old 12-29-2013 , 21:17   Re: Call public function by string
Reply With Quote #4

Quote:
Originally Posted by Backstabnoob View Post
He uses callfunc which is silly.
If it works, it's not silly.

I think you can create a forward. I'm not sure how they work as I've not used them myself but there are many plugins that do this (API-type plugins).
__________________
fysiks is offline
ConnorMcLeod
Veteran Member
Join Date: Jul 2006
Location: France (95)
Old 12-30-2013 , 01:26   Re: Call public function by string
Reply With Quote #5

Cache the function index and use callfunc_begin_i, it should slightly faster.

I'm using this native which allow to add any function that doesn't take parameters from a plugin :

PHP Code:
native kz_add_menuitem(itemName[64], const szCmdCallBack[], reshow 0); 



And menu is here :
__________________
- tired and retired -

- my plugins -
ConnorMcLeod is offline
Kia
AlliedModders Donor
Join Date: Apr 2010
Location: In a world of madness
Old 12-30-2013 , 10:01   Re: Call public function by string
Reply With Quote #6

Quote:
Originally Posted by Backstabnoob View Post
He uses callfunc which is silly.
Why is it silly?
__________________
Kia is offline
Backstabnoob
BANNED
Join Date: Feb 2009
Location: Iwotadai Dorm
Old 12-30-2013 , 10:16   Re: Call public function by string
Reply With Quote #7

Quote:
Originally Posted by Kia View Post
Why is it silly?
I thought it was reduntant in the caller plugin, but apparently it isn't. You should, as stated, use callfunc_begin_i instead with -1 as the plugin ID.


@Connor: Thanks. If I'm correct, callfunc_end returns what the function called does, right? So check if callfunc_end == PLUGIN_HANDLED and prevent the rest of the code execution if that's the case?
Backstabnoob is offline
Kia
AlliedModders Donor
Join Date: Apr 2010
Location: In a world of madness
Old 12-30-2013 , 10:17   AW: Re: Call public function by string
Reply With Quote #8

Quote:
Originally Posted by Backstabnoob View Post
I thought it was reduntant in the caller plugin, but apparently it isn't. You should, as stated, use callfunc_begin_i instead with -1 as the plugin ID.


@Connor: Thanks. If I'm correct, callfunc_end returns what the function called does, right? So check if callfunc_end == PLUGIN_HANDLED and prevent the rest of the code execution if that's the case?
Oh okay.

if(callfunc_begin_i(-1, "myfunc") == 1) // successful
...
else // failed
...
__________________
Kia is offline
Backstabnoob
BANNED
Join Date: Feb 2009
Location: Iwotadai Dorm
Old 12-30-2013 , 10:20   Re: Call public function by string
Reply With Quote #9

Nope, you need to use the function index instead. See Connor's code.

Also, you have the arguments the other way around, it's supposed to be callfunc_begin_i( iFunctionIndex, -1 )
Backstabnoob is offline
Kia
AlliedModders Donor
Join Date: Apr 2010
Location: In a world of madness
Old 12-30-2013 , 10:23   Re: Call public function by string
Reply With Quote #10

Oops.
__________________
Kia 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 10:14.


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