[英]Lua Arguments and Numbers
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
我知道這是sc腳的代碼,但是我收到的是“格式'2的錯誤參數(期望的數字,沒有值)“。 這與這段代碼相關,該代碼以所有其他代碼為前綴:
function InputConsole(...)
Console_Input(string.format(unpack(arg)))
end
最后,這是針對《命令與征服叛徒》的游戲(您是否希望查找API等)。 任何對我做錯事的幫助將不勝感激。
此代碼可能的問題是在函數InputConsole()
使用了不推薦使用的arg
功能。 Lua 5.0使用arg
作為使用參數列表中的...
標記訪問聲明為可變參數的函數的實際參數的方法。
編輯:但是可能並不意味着正確。
實際的問題看起來就像習慣用法從PlayerId
切換到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
如果它的嚴格性值得關注,則在調用Console_Input()
之前,先說fmt = tostring(fmt)
或可能的assert(type(fmt)=="string")
Console_Input()
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.