FunFair
FunFair

Reputation: 63

lua : passing parameter to other function problem

not sure did anyone ever face this kind of problem. here is my code

in main.lua :

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

highScore:InsertHighScore(userName, finishedTime)

in highScore.lua

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

it look simple and shouldn't be wrong, but in my console out put it show :

 table: 0x19e6340
 myName

after a day of testing, i found that before the 2 parameter that i pass, it actually passing another table to me, so do these changes on highScore.lua:

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

so now my "other code" can work nicely, but why it pass me a table before my parameter ?

Upvotes: 2

Views: 1714

Answers (1)

user149341
user149341

Reputation:

In Lua, a call to an object/table with a colon instead of a dot indicates that the object/table should be passed into the function as the first parameter (e.g, as a self). If you don't care about that, then call the function with a dot instead:

highScore.InsertHighScore(userName, finishedTime)

Upvotes: 4

Related Questions