how do I add a command to my mod?

ddyven

Member
Jul 24, 2024
3
0
180
ddyven.glitch.me
Pronouns
he/him
right, so I'm starting to understand how these mods work and it seems like they're all coded in lua. However, I've seen multiple mods with commands in it and I'm trying to figure out how to create a command for my mod and make its functionality and syntax, but I can't do it by myself.
anyone could help me pls??
 
you need to do a
LUA:
hook_chat_command("", "", )
the first blank you put the command, the second one you put the description and the one before the ) you put a local function like this on_"command name here"_command you need to to make the
LUA:
local function on_"command name here"_command()
    
end
 
when I try to run the mod it says there's an unexpected symbol near 'local' but I don't know what it is. I think I'm doing smth wrong
here's the code I put:
hook_chat_command("cmd", "syntax", local function on_"cmd"_command()
function
end)
 
when I try to run the mod it says there's an unexpected symbol near 'local' but I don't know what it is. I think I'm doing smth wrong
here's the code I put:
hook_chat_command("cmd", "syntax", local function on_"cmd"_command()
function
end)
You put the local function in the hook_chat_command it's supposed to be its own line
Like hook_chat_command("command", "description", on_cmd_command)

Local function on_cmd_command()

End
 
Alr, so there's 2 ways to do this, both giving the same result. I'm gonna show both of them, then explain them:

Code:
local function on_cmd_command(msg)
        djui_chat_message_create("You ran a command, good job! You also gave me this message: " .. msg)
        return true
end

hook_chat_command("cmd", "This CMD does something, go find out!", on_cmd_command)

Code:
hook_chat_command("cmd", "This CMD does something, go find out!", function(msg)
        djui_chat_message_create("You ran a command, good job! You also gave me this message: " .. msg)
        return true
end)

The first way is the way Mariodoesvr was trying to communicate. The function does not need to be local, it can be a regular function. The local keyword in lua means that it can't be used in other scopes, so if you need to use a function in another file, be sure to remove the local keyword. If you see mods making every single function at the top of the file local, you don't need to do this. The performance gain is minimally and really isn't necessary, mainly use local's to keep your code organized.

The second way is what you were trying to do. Instead of creating a function somewhere else, and passing it into hook_chat_command, you can instead create the function there. A local keyword is not needed, nor can it be done, because you aren't creating a variable. Whether or not option 1 or 2 is better depends on your personal preference, and how much code is in your function.

A couple of things that were not mentioned: The function passed to hook_chat_command receives a msg (message) parameter. It doesn't have to be msg, since that's just a variable, name it whatever you want. The message parameter is the message the user typed after the command.

Another thing is in these examples I'm returning true. By returning true, this tells coop that the command succeeded, and avoids the Unknown command, type /help for help text from appearing.

If you have any more questions feel free to ask!
 
  • Like
Reactions: owokitty
Alr, so there's 2 ways to do this, both giving the same result. I'm gonna show both of them, then explain them:

Code:
local function on_cmd_command(msg)
        djui_chat_message_create("You ran a command, good job! You also gave me this message: " .. msg)
        return true
end

hook_chat_command("cmd", "This CMD does something, go find out!", on_cmd_command)

Code:
hook_chat_command("cmd", "This CMD does something, go find out!", function(msg)
        djui_chat_message_create("You ran a command, good job! You also gave me this message: " .. msg)
        return true
end)

The first way is the way Mariodoesvr was trying to communicate. The function does not need to be local, it can be a regular function. The local keyword in lua means that it can't be used in other scopes, so if you need to use a function in another file, be sure to remove the local keyword. If you see mods making every single function at the top of the file local, you don't need to do this. The performance gain is minimally and really isn't necessary, mainly use local's to keep your code organized.

The second way is what you were trying to do. Instead of creating a function somewhere else, and passing it into hook_chat_command, you can instead create the function there. A local keyword is not needed, nor can it be done, because you aren't creating a variable. Whether or not option 1 or 2 is better depends on your personal preference, and how much code is in your function.

A couple of things that were not mentioned: The function passed to hook_chat_command receives a msg (message) parameter. It doesn't have to be msg, since that's just a variable, name it whatever you want. The message parameter is the message the user typed after the command.

Another thing is in these examples I'm returning true. By returning true, this tells coop that the command succeeded, and avoids the Unknown command, type /help for help text from appearing.

If you have any more questions feel free to ask!
That's a much better explanation
 
Alr, so there's 2 ways to do this, both giving the same result. I'm gonna show both of them, then explain them:

Code:
local function on_cmd_command(msg)
        djui_chat_message_create("You ran a command, good job! You also gave me this message: " .. msg)
        return true
end

hook_chat_command("cmd", "This CMD does something, go find out!", on_cmd_command)

Code:
hook_chat_command("cmd", "This CMD does something, go find out!", function(msg)
        djui_chat_message_create("You ran a command, good job! You also gave me this message: " .. msg)
        return true
end)

The first way is the way Mariodoesvr was trying to communicate. The function does not need to be local, it can be a regular function. The local keyword in lua means that it can't be used in other scopes, so if you need to use a function in another file, be sure to remove the local keyword. If you see mods making every single function at the top of the file local, you don't need to do this. The performance gain is minimally and really isn't necessary, mainly use local's to keep your code organized.

The second way is what you were trying to do. Instead of creating a function somewhere else, and passing it into hook_chat_command, you can instead create the function there. A local keyword is not needed, nor can it be done, because you aren't creating a variable. Whether or not option 1 or 2 is better depends on your personal preference, and how much code is in your function.

A couple of things that were not mentioned: The function passed to hook_chat_command receives a msg (message) parameter. It doesn't have to be msg, since that's just a variable, name it whatever you want. The message parameter is the message the user typed after the command.

Another thing is in these examples I'm returning true. By returning true, this tells coop that the command succeeded, and avoids the Unknown command, type /help for help text from appearing.

If you have any more questions feel free to ask!
ohh alright thank you both of you 😊
 

Users who are viewing this thread