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

[LUA] [ERROR] Trying to make a program with multiple functions, getting nowhere

Started by JonaK, 01 December 2012 - 11:11 AM
JonaK #1
Posted 01 December 2012 - 12:11 PM
Okay, here's my code:

Spoiler

local function usage()
print("Your options are:")
print("mine {depth} -- Mines however many blocks specified")
print("left {blocks} -- Turns left and moves X spaces")
print("right {blocks} -- Turns right and moves X spaces")
print("forward {blocks} -- Moves forward X spaces")
print("back {blocks} -- Moves back X spaces")
print("up {blocks} -- Moves up X spaces")
print("down {blocks} -- Moves down X spaces")
print("The usage is 'minesuite [option] {args}'")
end
local args={...}
if args ~= 2 then
return usage()
end
if args[1] == mine then
term.clear()
term.setCursorPos(1,1)
print("Please put fuel in slot one")
sleep("5")
term.clear()
term.setCursorPos(1,1)
print("Press enter to continue")
read()
for d=1,tonumber(args[2]) do
if turtle.getFuelLevel() < 5 then
turtle.refuel(1)
end
turtle.digDown()
turtle.down()
end
for u=1,minedist do
if turtle.getFuelLevel() < 5 then
turtle.refuel(1)
end
turtle.up()
end
end
if args[1] == left then
print("Please remember to put fuel in the first slot!")
sleep(2)
if turtle.getFuelLevel() < 5 then
turtle.refuel(1)
end
turtle.turnLeft()
for lfwd=1,tonumber(args[2]) do
turtle.forward()
end
end
if args[1] == right then
print("Please remember to put fuel in the first slot!")
sleep(2)
if turtle.getFuelLevel() < 5 then
turtle.refuel(1)
end
turtle.turnRight()
for rfwd=1,tonumber(args[2]) do
turtle.forward()
end
end
if args[1] == forward then
print("Please remember to put fuel in the first slot!")
sleep(2)
if turtle.getFuelLevel() < 5 then
turtle.refuel(1)
end
for ffwd=1,tonumber(args[2]) do
turtle.forward()
end
end
if args[1] == back then
print("Please remember to put fuel in the first slot!")
sleep(2)
if turtle.getFuelLevel() < 5 then
turtle.refuel(1)
end
for bbck=1,tonumber(args[2]) do
turtle.back()
end
end
if args[1] == up then
print("Please remember to put fuel in the first slot!")
sleep(2)
if turtle.getFuelLevel() < 5 then
turtle.refuel(1)
end
for uup=1,tonumber(args[2]) do
turtle.up()
end
end
if args[1] == down then
print("Please remember to put fuel in the first slot!")
sleep(2)
if turtle.getFuelLevel() < 5 then
turtle.refuel(1)
end
for down=1,tonumber(args[2]) do
turtle.down()
end
end

I am using Tekkit and am running this from a disk. Sometimes there is no output, and sometimes it just displays the help. There definitely is fuel in the Mining turtle.

What am I doing wrong?

Thanks,

~JonaK
OmegaVest #2
Posted 01 December 2012 - 12:27 PM
First off, for your args, you need to use #args to check how many arguments there are. After that, no idea.
Geforce Fan #3
Posted 01 December 2012 - 04:36 PM
I think, without even reading your code, I know your problem. Are you trying to run multiple functions at and need no delay? TRUST ME it does NOT work.
1lann #4
Posted 01 December 2012 - 04:57 PM
I think, without even reading your code, I know your problem. Are you trying to run multiple functions at and need no delay? TRUST ME it does NOT work.
Nope you got it completely off. I don't see whats wrong with running multiple functions with no delay. My programs run hundreds of functions with no delays, works perfectly fine. Also the title says trying to make a program with multiple functions, when did he ever say running multiple functions.

Anywho the problem with your program JonaK is that when you do a comparison with a string, you must put quotes. Ex.

if args[1] == mine then
should be

if args[1] == "mine" then
ChunLing #5
Posted 02 December 2012 - 01:43 AM
Yep.

Also, for this to work, I recommend putting your functions which will carry out the various actions into a table, indexed by the strings which will call the function. Then you just check and "if t_func[args[1]] then t_func[args[1]]() end"
JonaK #6
Posted 12 December 2012 - 06:16 PM
Wow, I just checked this. I guess I had email notifications turned off….

I think, without even reading your code, I know your problem. Are you trying to run multiple functions at and need no delay? TRUST ME it does NOT work.
Nope you got it completely off. I don't see whats wrong with running multiple functions with no delay. My programs run hundreds of functions with no delays, works perfectly fine. Also the title says trying to make a program with multiple functions, when did he ever say running multiple functions.

Anywho the problem with your program JonaK is that when you do a comparison with a string, you must put quotes. Ex.

if args[1] == mine then
should be

if args[1] == "mine" then
That worked perfectly, thanks!

Yep.

Also, for this to work, I recommend putting your functions which will carry out the various actions into a table, indexed by the strings which will call the function. Then you just check and "if t_func[args[1]] then t_func[args[1]]() end"
I don't know how to do this, but thanks anyway.

I'll post my final code in a little bit.