This is a read-only snapshot of the ComputerCraft forums,
taken in April 2020.
[Lua][Turtle] Making a program that needs parameters
Started by jewelshisen, 09 January 2013 - 06:55 PMPosted 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.
Posted 09 January 2013 - 07:58 PM
use the … operator? ( i think its an operator, just had a mental blank )
example:
this puts all the arguments into a table.
example:
local runtimeArgs = { ... }
this puts all the arguments into a table.
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?
Posted 09 January 2013 - 08:04 PM
yes you can… you can do this
however I suggest that you do this (for reasons you will be able to tell
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]
Posted 09 January 2013 - 08:06 PM
Ah thank you! Tiny question though: What does ~= mean??
Posted 09 January 2013 - 08:08 PM
not equal to… its equivalent to
if not #args == 1 then
both are acceptable coding practise…Posted 09 January 2013 - 08:09 PM
Got it! Thanks a ton!
Posted 09 January 2013 - 08:36 PM
something else notable with the '…' is this:
this means that the function can accept any amount of arguments.
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.