[0.8.9][NK] Chat Commands

johnycilohoklajohnycilohokla REGISTERED Posts: 283 Developer
edited December 2015 in Modding Tutorials
Required: ModScriptClass

Simple Chat Command

To create a new chat command you will need to call Eternus.GameState:RegisterSlashCommand from ModScriptClass:Initialize().
Eternus.GameState:RegisterSlashCommand takes 3 arguments:
  • Name (ex. "ExampleMod" will create /ExampleMod command)
  • Class Instance (ex. self)
  • Function Callback (ex. "Info" will call the Info function on the Class Instance[2nd argument])
Example:
function ExampleMod:Initialize()
Eternus.GameState:RegisterSlashCommand("ExampleMod", self, "Info")
end

-- Callback function
function ExampleMod:Info(userInput, args, commandName, player)
-- Print "Hello world from ExampleMod!" to the (local) chat window
player:SendChatMessage("Hello world from ExampleMod!\n")
end

Chat Command with arguments

The callback function accepts 4 parameter (args).
  • userInput - raw string
  • args - contains a list of all arguments
    To access an argument you can use the [ x ] brackets, x being the index, the first argument has index of 1, second one 2 and so on.
  • commandName - name of the command
  • player - player executing the command
Example:
local firstArg = args[1]

To check if an argument exist you can check for nil.

Example:
if args[1] ~= nil then
-- args[1] is valid
end
or
if args[1] then
-- args[1] is valid
end

To print all of the arguments you can use the pairs iterator combined with for loop.
-- Callback function 
function ExampleMod:PrintArguments(userInput, args, commandName, player)
for key,value in pairs(args) do
-- Print each argument to the (local) chat window
player:SendChatMessage("Argument #"..key.." = "..value .. "\n")
end
end

Server side command

Chat commands registered only on the server side will be executed on the server instead of client.

Example:
function ExampleMod:Initialize()
-- Server Side Commands
if Eternus.IsServer then
Eternus.GameState:RegisterSlashCommand("ExampleMod", self, "Info")
end
end

-- Callback function
function ExampleMod:Info(userInput, args, commandName, player)
-- Print "Hello world from ExampleMod!" to the (local) chat window from server
player:SendChatMessage("Hello world from ExampleMod!\n")
end
My Blog
jc.png
  yt.png  t.png  
Sign In or Register to comment.