Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get Lua to print(functionname.variable)

Tags:

function

lua

I am trying to get this to work but I am not sure if Lua supports this kind of variables

function newUser(accountName, password)
    accountName = accountName
    password = password
end

testUser = newUser("testName" , "testPassword")

print(testUser.password)

Does the testUser.password work with Lua?

like image 382
Developer Avatar asked Oct 18 '13 13:10

Developer


1 Answers

newUser is a function, so testUser gets the function's return value, that is, nothing. A simple and direct way to fix the problem is to return a table:

function newUser(accountName, password)
    local t = {}
    t.accountName = accountName
    t.password = password
    return t
end

EDIT: Or better, following your style as @lhf suggested:

function newUser(accountName, password) 
    return { accountName = accountName, password = password } 
end
like image 186
Yu Hao Avatar answered Sep 29 '22 07:09

Yu Hao