Jump to content

[TUT] Create a simple command handler system


Recommended Posts

Please note: this tutorial is for those who are used to the normal scripting method

In this tutorial we will be using tables, but not normal tables, metatables. Metatables are like normal tables, however once an action is applied to the table, a function is called from within the metatable.

The command handlers will be added in this format: addCommandHandler(name, function, true)

This is what you would do to add a command:

function cmd.hi(player) 
    outputChatBox( getPlayerName(player)..' says hi!' ) 

This would make /hi do "User says hi!"


Any code from this point should be in another file.

(Mentioned link)

local meta = {} -- This is for our system  
CMD = {} -- This is for adding commands 
-- Create a function within the table called "meta" 
-- It is called __newindex because it is called when a new index is created. See link above 
function meta.__newindex(t, i, v) 
    local m = {} -- create another table 
    function m.__call() -- This gets called when ATable()  
        removeCommandHandler(i,v) -- remove command  
        rawset(CMD,i,nil) -- remove from table 
    -- get fn 
    function m.__tostring() return v end 
    function m.__index() return v end 
    setmetatable(m,m) -- set mta 
    rawset(CMD, i, m) -- add the table directly  
    addCommandHandler(i, v, true) -- add handler 
setmetatable(CMD, meta) 


Now, to add a command, all you have to do is this:

function CMD.hi() end 

to delete it


to get the function (to see if it exists)


Link to comment
  • 5 months later...
  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Create New...