Jump to content

Recommended Posts

I'm trying to make my first mod. I'd like to add a user command, which allow players roll the dice without cooldown, for example: "/r 3D6". Here is my modmain.lua.

GLOBAL.AddModUserCommand("trpg_dice_plus", "r", {
    aliases = { "r" },
    prettyname = nil,
    desc = nil,
    permission = GLOBAL.COMMAND_PERMISSION.USER,
    slash = true,
    usermenu = false,
    servermenu = false,
    params = { "dice" },
    paramsoptional = { true },
    vote = false,
    canstartfn = function(command, caller, targetid)
        return true
    end,
    localfn = function(params, caller)
        if params.dice ~= nil then
            local dice, sides = string.match(params.dice, "(%d+)[dD](%d+)")
            if dice ~= nil and sides ~= nil then
                TheNet:Announce(string.format("骰出了%s个D%s", dice, sides))
                return
            end

            sides = tonumber(params.dice)
            if sides ~= nil then
                TheNet:Announce(string.format("骰出了%s个D%s", 1, sides))
                return
            end
        end

        TheNet:Announce("语法错误!")
    end,
})

I'm just testing AddModUserCommand() so I use a simple announcement text instead random number generator. But when I send the message "/r 1D4", the game crashed. I find this from client_log.txt:

[00:01:19]: [string "scripts/usercommands.lua"]:114: stack overflow
LUA ERROR stack traceback:
scripts/usercommands.lua:114 in (upvalue) getcommandfromhash (Lua) <113-120>
   hash = 114
scripts/usercommands.lua:123 in () ? (Lua) <122-124>
   name = r
=(tail call):-1 in ()  (tail) <-1--1>
=(tail call):-1 in ()  (tail) <-1--1>
scripts/usercommands.lua:123 in () ? (Lua) <122-124>
   name = r
=(tail call):-1 in ()  (tail) <-1--1>
=(tail call):-1 in ()  (tail) <-1--1>
scripts/usercommands.lua:123 in () ? (Lua) <122-124>
   name = r
=(tail call):-1 in ()  (tail) <-1--1>
=(tail call):-1 in ()  (tail) <-1--1>
scripts/usercommands.lua:123 in () ? (Lua) <122-124>
   name = r
=(tail call):-1 in ()  (tail) <-1--1>

What happened? How should I solve this?

I just modified my code like this:

GLOBAL.AddModUserCommand("r", "r", {
    prettyname = nil,
    desc = nil,

And then the crash was fixed, now the mod runs as I expected.

Nevertheless, I still don't understand how it works, and I am not sure if I will encounter this again in the future.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
  • Create New...