←  Ask a Pro

ComputerCraft | Programmable Computers for Minecraft

»

Function returns to another function

Geko4515's Photo Geko4515 31 Aug 2013

Topic: Function returns to another function


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.
Quote

Bubba's Photo Bubba 31 Aug 2013

Split into new topic.
Quote

floppyjack's Photo floppyjack 31 Aug 2013

What you need to do is the following:
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.
Quote

kreezxil's Photo kreezxil 31 Aug 2013

I think i just learned something new here. Thanks floppyjack!
Quote

floppyjack's Photo floppyjack 31 Aug 2013

 kreezxil, on 31 August 2013 - 08:18 AM, said:

I think i just learned something new here. Thanks floppyjack!
No problem! :)
Quote

GopherAtl's Photo 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:
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
Quote

Geko4515's Photo Geko4515 31 Aug 2013

Thanks GopherAtl and floppyjack, That clarifies a bunch.

I ended up just doing temp variables rather than a table

local x, z = getCord("x,z")
check = checkCord(x, z, newy)
Quote