Run function from function parameter?

Hello, I have an issue that’s quite difficult to explain.

Let’s say I have a function. It’s called vx.AddChatCmd(). I can do vx.AddChatCmd("/hello", hellofunc). I already have a way of detecting the command, I just want to know how to run the function in the function’s second param (in this case, it’s “hellofunc”)

How would I achieve this?

Store the function in a table, and call it from the table.

For example:



local tab = {}
tab[1] = function(...)
PrintTable({...})
end

tab[1]("test",1,2,3)


[lua]
– Command creation example with no arguments.

command = {};
command.all = {};

function command.Add( name, func )
if ( command.all[name] ) then return; end;

command.all[name] = func;

end;

function command.execute(name)
if ( not command.all[name] ) then return; end;

command.all[name](); -- this is the part where the stored command function is executed.

end;
[/lua]

you don’t sound too experienced so i don’t want to confuse you with an ellipsis ( the … )

I’m having a hard time understanding what either of these two do lol…
this is my function:



function vx.AddChatCmd( cmdname, func )
	table.insert(cmds, cmdname)
end


So I have the commands stored in the table. I don’t know where to progress now.

Here:

[lua]
vx = {};
vx.commands = {};

function vx:AddChatCmd( cmdname, onrun)
if ( self.commands[cmdname] ) then return; end;

self.commands[cmdname] = onrun;

end;

function ChatCmds( client, text, public )
for cmd,func in pairs(vx.commands) do
if ( string.Right( text, string.len(text) - 1 ) == cmd) then
func(client);
print( “Command '” … text … “’ was executed successfully.” );
return true;
end;
end;
end;

vx:AddChatCmd( “help”, function(client)
print( “You (” … client:Nick() … “) have executed the help command!”);
end);

hook.Add( “PlayerSay”, “AllCmds”, ChatCmds );
[/lua]

feel free to add me to steam

-snip- dammit you crafty ninja!

feel free to correct me im kind of faded i forgot to include a command prefix so you could technically just type #help tHelp 7help anything lol