AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   Solved calling function by its name from string (https://forums.alliedmods.net/showthread.php?t=314138)

DjSoftero 02-05-2019 05:16

calling function by its name from string
 
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

klippy 02-05-2019 05:52

Re: calling function by its name from string
 
Callfunc or private forwards (CreateOneForward).

DjSoftero 02-05-2019 06:11

Re: calling function by its name from string
 
Quote:

Originally Posted by KliPPy (Post 2638242)
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

klippy 02-05-2019 08:26

Re: calling function by its name from string
 
Really doesn't matter, those are command handlers, use whichever one is more intuitive to you.

DjSoftero 02-05-2019 10:05

Re: calling function by its name from string
 
Quote:

Originally Posted by KliPPy (Post 2638264)
Really doesn't matter, those are command handlers, use whichever one is more intuitive to you.

okay, thanks again.


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

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