Function returns to another function
Geko4515 31 Aug 2013
Topic: Function returns to another function
Hello, I am working on something like the following:
when I call the function with a print I get the following:
print(getCords("x,z")) output: 51
seems okay but expected a space in between.
Now my problem is when I do function call inside another function
newy = 10
check = checkCords(getCords("x,z"),newy)
I print the parameters passed in and get: 5 10
Any assistance is greatly appreciated.
Hello, I am working on something like the following:
cords {x=5, y=1, z=1} function getCords(cord) --Returns x,y,z or if string specified just that cord if type(cord) == "string" then string.lower(cord) else cord = nil end if cord == nil then return cords.x, cords.y, cords.z elseif cord == "x,z" then return cords.x, cords.z elseif cord == "x,y" then return cords.x, cords.y elseif cord == "y,z" then return cords.y, cords.z elseif cord == "x" then return cords.x elseif cord == "y" then return cords.y elseif cord == "z" then return cords.z end end
when I call the function with a print I get the following:
print(getCords("x,z")) output: 51
seems okay but expected a space in between.
Now my problem is when I do function call inside another function
newy = 10
check = checkCords(getCords("x,z"),newy)
I print the parameters passed in and get: 5 10
Any assistance is greatly appreciated.
floppyjack 31 Aug 2013
What you need to do is the following:
unpack(table) returns a comma-seperated list of the contents of the table.
newy = 10 tCords = { getCords("x,z") } -- This puts the return values of getCords("x,z") in a table table.insert(tCords, newy) -- This adds newy to the table checkCords(unpack(tCords)) -- This unpacks the table and calls checkCords with the result
unpack(table) returns a comma-seperated list of the contents of the table.
floppyjack 31 Aug 2013
GopherAtl 31 Aug 2013
to clarify what was actually happening in the original code, value lists returned by functions can only be added to the end of other value lists.
explanation in code:
explanation in code:
function f() return 1,2 end print(f(),3) -- this will only print 13 - the value list returned by f() will be truncated, only the first value will be kept print(0,f()) -- this will print 012, as the whole list can be added to the end of a list