Reputation: 21
I am trying to do a simple program in lua that aims to return a string with certain values based on the user input, however I am having issues scripting this.
For example, if I compile
person1 = {
name = "bob" ,
age = 70 ,
hair = "black" ,
};
person2 = {
name = "dan",
age = 40 ,
hair = "blonde" ,
};
describe = function(parent)
print ( "hello " .. parent.name .. " your are " .. parent.age .. " years old
and your hair color is " .. parent.hair )
end
print ("who are you") ;
answer = io.read ();
describe (answer)
I would expect that if I wrote person1
as the input the script would return a string that reads:
hello bob you are 70 years old and your hair color is black
However it instead returns an error.
The question is, what can I do to fix this? What is the right way to use user input in Lua?
Upvotes: 1
Views: 982
Reputation: 25926
You would have to pass the object
to the function, not the name. Or search for the object in the global scope:
person1 = {
name = "bob" ,
age = 70 ,
hair = "black" ,
};
person2 = {
name = "dan",
age = 40 ,
hair = "blonde" ,
};
describe = function(parent)
parent = _G[parent]
print ( "hello " .. parent.name .. " your are " .. parent.age .. " years old and your hair color is " .. parent.hair )
end
print ("who are you") ;
answer = io.read ();
describe (answer)
Working example: http://ideone.com/UJxnpx
Upvotes: 1