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

How to specify with numbers?

Started by WarriorDB, 09 June 2014 - 06:57 AM
WarriorDB #1
Posted 09 June 2014 - 08:57 AM
I was recently making simple programs that will make my life easier so I won't have to go to lua and type "turtle.forward()" for example, I need to know how to specify numbers for my program which has "turtle.forward()" so I can do for example forward(2) in my console for it to move 2 blocks forward.
Blue #2
Posted 09 June 2014 - 07:59 PM
To read and use the user's input,use this code:


local input=read() --reads the user's input
local number=tonumber(input) --converts the string into a value
turtle.foward(number) --moves the turtle foward
Edited on 09 June 2014 - 06:00 PM
CometWolf #3
Posted 09 June 2014 - 08:08 PM
He wants direct input Glass, not read().

You could just use the included go program :P/>

go forward 2
What you need is program arguments. By default they're stored in a numerically indexed table called arg, where each key represents one word. Using the above example of "go forward 2", arg[1] would be "forward" and arg[2] would be "2". I forgot wether args are automatically converted, or if it's all passed as strings, so just experiment.

Edit: The above is only true when dealing with function arguments, not program arguments.
Edited on 09 June 2014 - 10:37 PM
flaghacker #4
Posted 09 June 2014 - 09:00 PM
1)No it's not automatically converted, all strings.
2)It's not stored in the table "args", you can call it anything you want. "args" or "tArgs" are just common names. You have to do this:

local args = {...}
--or
local shkbvgy = {...}
--or
local a = {...}
Just put it near the top of the program.
Edited on 09 June 2014 - 07:01 PM
CometWolf #5
Posted 09 June 2014 - 10:38 PM
Ah right, my bad. The arg table is only automatically created in the case of functions, and not whole programs.
MKlegoman357 #6
Posted 09 June 2014 - 10:41 PM
2)It's not stored in the table "args"…

Actually, it is. Whenever you are dealing with vararg (those three dots) Lua automatically creates a local variable called 'args' (note: not 'arg', like CometWolf said). It's a numerically indexed table. This table also has a field 'n' (args.n) which is equal to the number of arguments passed to that function.

EDIT: My bad, it is called 'arg' and it is not created when dealing with programs.
Edited on 09 June 2014 - 08:45 PM
CometWolf #7
Posted 09 June 2014 - 10:44 PM
(note: not 'arg', like CometWolf said)
I tested this earlier to make sure, it's called arg.
cptdeath58 #8
Posted 10 June 2014 - 12:28 AM
the table "arg" or any other table is not created automatically until specified within the program using a specific command (such as local arg = read() or local arg = "blah") that being said you need to go to the API and edit it from there or create a new one. But since the API "turtle" is read only, the only thing you could do is

--get args before
shell.run( "go", "forward ", (table specified for this function) )
--or
shell.run( "go forward "..(table specified for this function) )
or create a new API all together.

[EDIT] if you wanted it to go in any other direction you would change "forward" to another direction
or

--get args before
repeat
 turtle.forward()
 t = t + 1
until t == (table specified for this function)
Edited on 09 June 2014 - 10:33 PM
CometWolf #9
Posted 10 June 2014 - 12:35 AM
the table "arg" or any other table is not created automatically until specified within the program using a specific command (such as local arg = read() or local arg = "blah")
Like we've both already stated, it in fact is in the case of functions. Please don't make corrections based on things you do not know.

local test = function(...)
  for i,v in ipairs(arg) do
    print(v)
  end
end
test("print"," ","this")

Also, why would he create an API or edit the turtle API? All he wants to do is to control the turtle directly from the shell, using arguments. Which it just so happens, is what go does.
cptdeath58 #10
Posted 10 June 2014 - 12:49 AM
From the shell oh!
I thought he meant in a program (face palm)
my bad