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

Program with arguements

Started by sabian8, 24 September 2012 - 04:31 PM
sabian8 #1
Posted 24 September 2012 - 06:31 PM
I want to make a program that has arguments(I think they are called arguments, I mean where you can type things after the program name).Please help
sjele #2
Posted 24 September 2012 - 06:33 PM
put this in top of code:

tArgs = {...}

if the program name was testing.
and you called it like this: tesing print run

then tArgs[1] would be print
tArgs[2] would be run
sabian8 #3
Posted 24 September 2012 - 06:40 PM
Sorry I don't really understand please could you explain again?
sjele #4
Posted 24 September 2012 - 06:48 PM
Okey, lets make some code to make this easyer :P/>/>

this program is called sleepfor. It is called with sleepfor "time to sleep"


tArgs = {...} --stores the arguments you used when you called program. to the table tArgs
sleep(tArgs[1]) we sleep, with the valvue off tArgs[1]

If we called program like this


sleepfor 5

the program would sleep 5 seconds.
Matrixmage #5
Posted 24 September 2012 - 06:50 PM
put "tArgs = {…}" at the top and then the data that you put after the program name can be accessed through "tArgs[1]". Or you can assign a variable to store the data with "veriableName = tonumber(tArgs[1])". Hope this helps!

EDIT: Ninja'd :P/>/>
sabian8 #6
Posted 24 September 2012 - 06:52 PM
Thanks!
sabian8 #7
Posted 24 September 2012 - 07:14 PM
I was testing this out so I typed :

tArgs = {...}
chicken = tonumber(tArgs[1])
blah = "is"
if chicken == blah then
print("Is what?")
end
Its not doing anything, no error just doesn't do anything. Anyone know why?
Matrixmage #8
Posted 24 September 2012 - 07:17 PM
you typed "programName is"?
sabian8 #9
Posted 24 September 2012 - 07:22 PM
yeah.
cant_delete_account #10
Posted 24 September 2012 - 07:39 PM
yeah.
Remove tonumber.
Matrixmage #11
Posted 24 September 2012 - 07:50 PM

does that only work with numbers (might be obvious…)?
Fatal_Exception #12
Posted 25 September 2012 - 01:42 AM
It tries to turn whatever you give it into a number. If it can't, it'll return nil
sabian8 #13
Posted 25 September 2012 - 05:58 PM
Thanks everyone!
sabian8 #14
Posted 25 September 2012 - 06:28 PM

tArgs = {...}
chicken = tonumber(tArgs[1])
for i = 1, chicken do
while not turtle.forward() do
turtle.dig()
end
end
I don't know what is wrong with this code, can anyone help me please.

EDIT: Sorry I was being stupid and did this on a computer.

EDIT2: Tried it on a turtle didn't work. Please help.
Doyle3694 #15
Posted 25 September 2012 - 07:09 PM
what does it give as error?
OmegaVest #16
Posted 25 September 2012 - 07:21 PM

tArg = tonumber(...)
print("Going to try to move ", tArg, " times.")

for i = 1, chicken do
while true do
   if turtle.forward() then
      print("Moved ", i, " time(s)")
	  break
   else
	  turtle.dig()
   end
   sleep(0.2)
end
end

I think you can just use the … straight if it is one param. Also, I think you were probably getting false when you tried to use your while loop, but I don't know why. This should help to debug, though.
sjele #17
Posted 25 September 2012 - 07:23 PM

tArgs = {...}
chicken = tonumber(tArgs[1])
for i = 1, chicken do
while not turtle.forward() do
turtle.dig()
end
end
I don't know what is wrong with this code, can anyone help me please.

EDIT: Sorry I was being stupid and did this on a computer.

EDIT2: Tried it on a turtle didn't work. Please help.

What version of CC are you in? Might be a fuel problem if you are 1.4+.

If so. Fuel turtle. (google) or disable turtle fual in Config.