Lua:传递参数到其他函数的问题

不确定有没有人遇到过这种问题。这是我的代码:

在 main.lua:

local highScore = require("highScore")
local username = "myName"
local finishedTime = 12345

highScore:InsertHighScore(userName, finishedTime)

在 highScore.lua:

function InsertHighScore(name,time)
   print(name)
   print(time)
   -- other code
end

看起来很简单,不应该出错,但是在我的控制台输出中显示:

 table: 0x19e6340
 myName

经过一天的测试,我发现在我传递的第二个参数之前,它实际上向我传递了另一个表,因此在 highScore.lua 上进行以下更改:

function InsertHighScore(table,name,time)
   print(table)
   print(name)
   print(time)
   -- other code
end

现在我的“其他代码”可以正常工作,但是为什么它在我的参数之前向我传递一个表?

原文链接 https://stackoverflow.com/questions/7155906

点赞
stackoverflow用户88888888
stackoverflow用户88888888

在 Lua 中,使用冒号而不是点来调用对象/表表示该对象/表应作为第一个参数(例如,作为“self”)传递给函数。如果您不关心这一点,可以使用点来调用函数:

highScore.InsertHighScore(userName, finishedTime)
2011-08-23 03:14:02