Variables 如何将空变量传递给Lua中的函数

Variables 如何将空变量传递给Lua中的函数,variables,lua,function-parameter,Variables,Lua,Function Parameter,我试图将空值传递给函数,但失败了。这是我的设置 function gameBonus.new( x, y, kind, howFast ) -- constructor local newgameBonus = { x = x or 0, y = y or 0, kind = kind or "no kind", howFast = howFast or "no speed" } return setme

我试图将空值传递给函数,但失败了。这是我的设置

function gameBonus.new( x, y, kind, howFast )   -- constructor
    local newgameBonus = {
        x = x or 0,
        y = y or 0,
        kind = kind or "no kind",
        howFast = howFast or "no speed"
    }
    return setmetatable( newgameBonus, gameBonus_mt )
end
我只想传递“kind”,并希望构造函数处理其余的内容。喜欢

 local dog3 = dog.new("" ,"" , "bonus","" )
或者我只想通过“多快”

我尝试了使用
和不使用,给出了错误:

“,”附近出现意外符号


nil
是在Lua中表示空的类型和值,因此不应传递空字符串
“”
或什么都不传递,而应传递
nil
,如下所示:

local dog3 = dog.new(nil ,nil , "bonus", nil )
请注意,可以省略最后一个
nil

以第一个参数
x
为例,表达式

x = x or 0
相当于:

if not x then x = 0 end

也就是说,如果
x
既不是
false
也不是
nil
,则将
x
设置为默认值
0

是我还是调用了错误的函数?您可以调用
dog.new
,但函数名为
gameBones.new
function gameBonus.new( x, y, kind, howFast )   -- constructor
  local newgameBonus = type(x) ~= 'table' and 
    {x=x, y=y, kind=kind, howFast=howFast} or x
  newgameBonus.x = newgameBonus.x or 0
  newgameBonus.y = newgameBonus.y or 0
  newgameBonus.kind = newgameBonus.kind or "no kind"
  newgameBonus.howFast = newgameBonus.howFast or "no speed"
  return setmetatable( newgameBonus, gameBonus_mt )
end

-- Usage examples
local dog1 = dog.new(nil, nil, "bonus", nil)
local dog2 = dog.new{kind = "bonus"}
local dog3 = dog.new{howFast = "faster"}
function gameBonus.new( x, y, kind, howFast )   -- constructor
  local newgameBonus = type(x) ~= 'table' and 
    {x=x, y=y, kind=kind, howFast=howFast} or x
  newgameBonus.x = newgameBonus.x or 0
  newgameBonus.y = newgameBonus.y or 0
  newgameBonus.kind = newgameBonus.kind or "no kind"
  newgameBonus.howFast = newgameBonus.howFast or "no speed"
  return setmetatable( newgameBonus, gameBonus_mt )
end

-- Usage examples
local dog1 = dog.new(nil, nil, "bonus", nil)
local dog2 = dog.new{kind = "bonus"}
local dog3 = dog.new{howFast = "faster"}