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

[LUA] Arguments

Started by Dlcruz129, 27 October 2012 - 03:10 PM
Dlcruz129 #1
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
Jan #2
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?
Dlcruz129 #3
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'.
Lyqyd #4
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
ChunLing #5
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.
Lyqyd #6
Posted 27 October 2012 - 07:19 PM
Pray tell, why would one need to avoid "arg" in Lua 5.1?
ChunLing #7
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.