Developer
Developer

Reputation: 764

Get Lua to print(functionname.variable)

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?

Upvotes: 5

Views: 283

Answers (1)

Yu Hao
Yu Hao

Reputation: 122493

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

Upvotes: 5

Related Questions