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

Solved calling function by its name from string


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
DjSoftero
Veteran Member
Join Date: Nov 2014
Location: Lithuania
Old 02-05-2019 , 05:16   calling function by its name from string
Reply With Quote #1

hello, let's say I have a functions name in string and I want to call it from there. is there any way to do it besides using the set_task?

PHP Code:
#include <amxmodx>
#include <amxmisc>

new const commands[6][32] = {
    
"/test""test_function",
    
"/change""change_function",
    
"/revert""revert_function"
};

public 
plugin_init()
    
register_clcmd("say""HookSay");

public 
HookSay(id) {
    new 
message[32];
    
read_args(messagecharsmax(message));
    
remove_quotes(message);
    
    for(new 
i=0i<sizeof(commands[]); i+=2)
        if(
equali(messagecommands[i]))
            
set_task(0.1commands[i+1], id//need a different method here
        
    
return PLUGIN_CONTINUE;    
}

public 
test_function(id)
    
client_print(idprint_chat"testttttt")

public 
change_function(id)
    
client_print(idprint_chat"changeeeeee")

public 
revert_function(id)
    
client_print(idprint_chat"reverttttttt"
also, would there be any benefit if I were to do what I stated above insted of:

Spoiler

because I plan on having a lot of register_clcmd(); hooks and I think it would be better if I were to use it once instead of 20+ times or so.

oh and I know there is callfunc, but it seems like my optimization would be pointless if I were to use that lol

Last edited by DjSoftero; 02-05-2019 at 10:05. Reason: grammar
DjSoftero is offline
klippy
AlliedModders Donor
Join Date: May 2013
Location: Serbia
Old 02-05-2019 , 05:52   Re: calling function by its name from string
Reply With Quote #2

Callfunc or private forwards (CreateOneForward).
__________________
klippy is offline
DjSoftero
Veteran Member
Join Date: Nov 2014
Location: Lithuania
Old 02-05-2019 , 06:11   Re: calling function by its name from string
Reply With Quote #3

Quote:
Originally Posted by KliPPy View Post
Callfunc or private forwards (CreateOneForward).
Thanks! But perhaps you know which one is more efficient? I'm leaning towards Callfunc as it requires less side functions for it to work, but that's not always the case in terms of performance

Last edited by DjSoftero; 02-05-2019 at 06:12.
DjSoftero is offline
klippy
AlliedModders Donor
Join Date: May 2013
Location: Serbia
Old 02-05-2019 , 08:26   Re: calling function by its name from string
Reply With Quote #4

Really doesn't matter, those are command handlers, use whichever one is more intuitive to you.
__________________
klippy is offline
DjSoftero
Veteran Member
Join Date: Nov 2014
Location: Lithuania
Old 02-05-2019 , 10:05   Re: calling function by its name from string
Reply With Quote #5

Quote:
Originally Posted by KliPPy View Post
Really doesn't matter, those are command handlers, use whichever one is more intuitive to you.
okay, thanks again.
DjSoftero 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 09:29.


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