1619 posts
Posted 27 October 2012 - 05:10 PM
Ok, I feel like a noob considering I've been using ComputerCraft for so long but I don't know how to use arguments. Tutorial please?
DLC
137 posts
Location
the Netherlands
Posted 27 October 2012 - 05:21 PM
function addDerp(original)
return original.."derp"
end
print(addDerp("herpa"))
Here 'original' is the argument. Or did you mean something else?
1619 posts
Posted 27 October 2012 - 05:46 PM
I think this will work. What I'm trying to do is make a rednet opening program where you type 'open right/up/down/left'.
8543 posts
Posted 27 October 2012 - 06:04 PM
If you want to take arguments in a program, just use the ellipsis notation for variable arguments:
args = {...}
--args now contains a space/tab separated list of all arguments passed to your program.
if #args > 0 and peripheral.getType(args[1]) == "modem" then
--if there is at least one argument passed and the first argument is the name of a side with a modem
rednet.open(args[1])
--then open the modem
else
--otherwise complain that they are doing it wrong.
print("Usage: open <side>")
end
2005 posts
Posted 27 October 2012 - 06:45 PM
You may want to use something more different from "arg" than "args" if you're going to be doing functions with variable number of arguments, though. They look pretty similar. A lot of CC programs use "tArgs" to avoid any confusion.
8543 posts
Posted 27 October 2012 - 07:19 PM
Pray tell, why would one need to avoid "arg" in Lua 5.1?
2005 posts
Posted 27 October 2012 - 11:22 PM
Um…I thought we were mostly on 5.2, but I guess it's true that you don't really ever need to use the arg table…I never do…almost never.