This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
DannySMc's profile picture

Getting rest of arguments back and passing them around?

Started by DannySMc, 13 May 2015 - 08:47 AM
DannySMc #1
Posted 13 May 2015 - 10:47 AM
So I had a homescreen function and I wish to return some data from the page as to which program I wanted to run…

function home(page)
	-- do something
	if page then
		print("worked")
	else
		print("nope")
	end
	return runtest, "hello"
end

function runtest(text)
	print(text)
	return true
end

So I wish to run the home screen, and it should return what to run next, this then will pick up the function and the arguments and then pass them to the function. I then also need to pass the arguments back.

So my question how do you grab the first argument and then the rest after in a table? and then unpack them? to the function?

I also have a feeling this is like coroutines, where you pass arguments back and forth to coroutines?

is like:

local foo, args = home()
and args will be a table of the rest of the args?

local foo, {args} = home()
or is it this?

local foo, ... = home()

or is it that? or different? also can someone even maybe explain how this would work with a coroutine? xD

Moderator: Please do not move this topic, it's completely separate and I added the bit on the bottom as the other post wasn't getting replies anymore.
theoriginalbit #2
Posted 13 May 2015 - 11:06 AM
This is not possible. You can however do this


local args = { home() }
local foo = table.remove(args, 1)

What this does is it packs all the return values from the home function into a table. We then use table.remove to remove the item at the first index (which is the first return value), and table.remove returns the value that it removed, that way we can both remove it from the table and be able to keep it for our uses.
Bomb Bloke #3
Posted 13 May 2015 - 11:08 AM
I also have a feeling this is like coroutines, where you pass arguments back and forth to coroutines?

Erm, it's rather more like "coroutines are like functions". You wouldn't use a coroutine for what you're talking about here. Pretty much the one time you do want to make use of coroutines is when you want to pause and resume a function. That's it.

Anyway, another option is to simply pass your arguments back from "home()" in the format in which you want them. Eg:

local function runtest(text)
	-- Whatever
end

local function home()
	return runtest, {"hello"}
end

local func, args = home()

func(unpack(args))