0
function OnChat(PlayerId, Type, Message)

    if Message == "!usecarepackage" then
        if ReadKS1 == "CP" or ReadKS2 == "CP" or ReadKS3 == "CP" then
            InputConsole("msg %s has requested a care package!", Get_Player_Name_By_ID(pID))
            local pos = Get_Position(Get_GameObj(pID))
            pos:AssignY(pos:GetY()+1)
            building = Create_Object("SignalFlare_Gold_Phys3", pos)
            Attach_Script(building, "Test_Cinematic", "caredrop.txt")
            if building == nil then
                 InputConsole("ppage %d Cannot spawn Care Package here.", pID)
            elseif math.random(50, 64) == 50 then
                Attach_Script(building, "z_Set_Team", Get_Team(pID))
                Attach_Script(building, "M00_No_Falling_Damage_DME")
                --Add in rest of numbers and corresponding streak, such as UAV if math.random = 50
            end
        elseif ReadKS1 ~= "CP" or ReadKS2 ~= "CP" or ReadKS3 ~= "CP" then
            InputConsole("ppage %d You are not allowed to use that with your current streak selection", pID)
        end
    end

    return 1
end

我知道这是邋遢的代码,但我收到“错误的参数 #2 到‘格式’(预期数字,没有价值)”。这与以所有其他部分为前缀的这段代码有关:

function InputConsole(...)
    Console_Input(string.format(unpack(arg)))
end

最后,这是针对游戏命令与征服叛徒(如果您希望查找 API 等)。对我做错的任何帮助将不胜感激。

4

1 回答 1

0

此代码可能存在的问题是arg在您的函数中使用了已弃用的功能InputConsole()。Lua 5.0 被用作使用参数列表中的标记arg访问声明为可变参数的函数的实际参数的一种方式。...

编辑:但可能并不意味着正确。

实际问题看起来像成语 from PlayerIdto的切换pID。前者是函数的命名参数,OnChat()后者是函数体中使用的全局变量,无需进一步初始化。未初始化的全局变量是nil,所以nil被传递给InputConsole()并且错误消息告诉你真相。

没有解决它的旧答案

Lua 5.1 弃用了这种用法,Lua 5.2 完全删除了它。

从提供的代码片段中我不确定这个游戏中实际使用的是哪个版本的 Lua,但症状与缺少自动生成的arg表一致。

我会这样写函数:

function InputConsole(...)
    Console_Input(string.format(...))
end

但是您也可以添加local arg = {...}作为函数的第一行,并获得与 Lua 5.0 提供的几乎相同的效果,但代价是创建(和丢弃)临时表。差异是微妙的,主要与nil表格中的处理有关。

为了清楚起见,我更喜欢命名第一个参数,因为它不是真正可选的。

function InputConsole(fmt, ...)
    Console_Input(string.format(fmt, ...))
end

如果你可以指望那个参数是一个字符串,那可以进一步简化为

function InputConsole(fmt,...)
    Console_Input(fmt:format(...))
end

如果担心它的粘稠度,只需fmt = tostring(fmt)assert(type(fmt)=="string")调用Console_Input().

于 2013-04-03T20:43:12.933 回答