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

[Lua][Turtle] Making a program that needs parameters

Started by jewelshisen, 09 January 2013 - 06:55 PM
jewelshisen #1
Posted 09 January 2013 - 07:55 PM
How can i make a Turtle program that needs a parameter set when you run it? Something kind of like the GPS Host program that needs you to input the x y z values.
theoriginalbit #2
Posted 09 January 2013 - 07:58 PM
use the … operator? ( i think its an operator, just had a mental blank )

example:

local runtimeArgs = { ... }

this puts all the arguments into a table.
jewelshisen #3
Posted 09 January 2013 - 08:01 PM
Ah! Thank you! I'm still learning lua and CC so it is taking me some effort. Can you also just store it as a normal variable? For instance if you only need one argument?
theoriginalbit #4
Posted 09 January 2013 - 08:04 PM
yes you can… you can do this
local args = ...

however I suggest that you do this (for reasons you will be able to tell


local args = { ... }
if #args ~= 1 then
  print( "Usage: program <param1>" )
  error()
end

-- then if you just want to get it out of the table
args = args[1]
jewelshisen #5
Posted 09 January 2013 - 08:06 PM
Ah thank you! Tiny question though: What does ~= mean??
theoriginalbit #6
Posted 09 January 2013 - 08:08 PM
not equal to… its equivalent to
if not #args == 1 then
both are acceptable coding practise…
jewelshisen #7
Posted 09 January 2013 - 08:09 PM
Got it! Thanks a ton!
theoriginalbit #8
Posted 09 January 2013 - 08:11 PM
I good resource for learning is here it covers everything Lua.
theoriginalbit #9
Posted 09 January 2013 - 08:36 PM
something else notable with the '…' is this:



local function doSomething( ... )
  print( table.concat( { ... }, " " ) )
end

doSomething( "Hello" )
doSomething( "Hello", "How" )
doSomething( "Hello", "How", "Are" )
doSomething( "Hello", "How", "Are", "You" )

this means that the function can accept any amount of arguments.