Jump to content




Function returns to another function


6 replies to this topic

#1 Geko4515

  • Members
  • 5 posts

Posted 31 August 2013 - 06:13 AM

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.

#2 Bubba

    Use Code Tags!

  • Moderators
  • 1,142 posts
  • LocationRHIT

Posted 31 August 2013 - 07:51 AM

Split into new topic.

#3 floppyjack

  • Members
  • 18 posts

Posted 31 August 2013 - 08:12 AM

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.

#4 kreezxil

  • Members
  • 128 posts
  • LocationBowie, TX

Posted 31 August 2013 - 08:18 AM

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

#5 floppyjack

  • Members
  • 18 posts

Posted 31 August 2013 - 08:53 AM

View Postkreezxil, on 31 August 2013 - 08:18 AM, said:

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

#6 GopherAtl

  • Members
  • 888 posts

Posted 31 August 2013 - 08:58 AM

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


#7 Geko4515

  • Members
  • 5 posts

Posted 31 August 2013 - 01:40 PM

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)





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users