This is a read-only snapshot of the ComputerCraft forums,
taken in April 2020.
Program with arguements
Started by sabian8, 24 September 2012 - 04:31 PMPosted 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
Posted 24 September 2012 - 06:33 PM
put this in top of code:
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
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
Posted 24 September 2012 - 06:40 PM
Sorry I don't really understand please could you explain again?
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"
If we called program like this
the program would sleep 5 seconds.
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.
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/>/>
EDIT: Ninja'd :P/>/>
Posted 24 September 2012 - 06:52 PM
Thanks!
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?Posted 24 September 2012 - 07:17 PM
you typed "programName is"?
Posted 24 September 2012 - 07:22 PM
yeah.
Posted 24 September 2012 - 07:39 PM
Remove tonumber.yeah.
Posted 24 September 2012 - 07:50 PM
Remove tonumber.yeah.
does that only work with numbers (might be obvious…)?
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
Posted 25 September 2012 - 05:58 PM
Thanks everyone!
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.
Posted 25 September 2012 - 07:09 PM
what does it give as error?
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.
Posted 25 September 2012 - 07:23 PM
I don't know what is wrong with this code, can anyone help me please.tArgs = {...} chicken = tonumber(tArgs[1]) for i = 1, chicken do while not turtle.forward() do turtle.dig() end end
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.