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

LUA Question. I do not understand why my program isn't working.

Started by guamie, 02 February 2013 - 08:25 PM
guamie #1
Posted 02 February 2013 - 09:25 PM
when i try to run the following, I get an error for line 4, attempt to index ? (a nil value)
but line 4 is part of a function, why is it failing on me?

thanks in advance guys, you rock!


--turn on device
function itemOn()
t=peripheral.wrap("front")
t.turnOn()
end
-- turtle places miners
rednet.open("right")
count=1
while count <= 36 do
turtle.place()
rednet.broadcast("copy")
x,y,z=rednet.receive()
print(y)
if y=="copied" then
itemOn()
count = count + 1
end
x,y,z=rednet.receive()
print(y)
if y=="moved" then
sleep(.01)
end
end
-- turtle picks up miners
count=1
while count <= 36 do
if turtle.detect() then
turtle.dig()
count=count+1
else
turtle.sleep(.01)
end
end
turtle.forward()
while true do
turtle.turnRight()
end
Shnupbups #2
Posted 02 February 2013 - 09:26 PM
What is the peripheral? Does turnOn exist?
guamie #3
Posted 02 February 2013 - 09:39 PM
the peripheral is another turtle.

when i make a test program with just the function:


--turn on device
function itemOn()
t=peripheral.wrap("front")
t.turnOn()
end
itemOn()

this works, the second turtle starts, but when i try to incorporate it into the above program, it gives the error. What do you mean by does turnOn exist? I'm still really new to lua

EDIT:
I know it works because the second turtle has this as his startup file:

while true do
turtle.turnRight()
end

and I see the little bugger start spinnign.
Shnupbups #4
Posted 02 February 2013 - 09:53 PM
Well then I have no idea.
guamie #5
Posted 02 February 2013 - 09:55 PM
well…. crap… thanks for looking buddy here is hoping someone will see what ive done wrong
AnthonyD98™ #6
Posted 02 February 2013 - 10:20 PM
If you tell me what the program is designed to do I could most likely fix it.
guamie #7
Posted 02 February 2013 - 10:41 PM
the overall program is one turtle sets up 36 chests for 36 turtles to move to and run excavate, after excavte finishes, each of the 36 turtles returns to where turtle 37 started them from and turtle 37 picks them up. this was to make a 'quarry' kind of thing.

ok so… here's all the code im trying to use, and also the flow

This is the program being run on the main computer. the main computer is positioned to the left of the disk drive, and all drone turtles are plced below the drive to receive their instructions.. I'm still new to lua so I wrote each individual turtle (36 total) 2 programs each, one which moves it to position, one which returns it after. also there is a 3rd program, named dark. dark is just used to put the turtle into a wait mode until it receives a rednet signal 'go' which makes the turtle begin to dig.

--main computer to copy paths returns and dark to miners. must have disk to side
rednet.open("top")
fs.copy("disk/dark","dark")
fs.makeDir("paths")
fs.makeDir("returns")
count=1
while count <=36 do
fs.copy("disk/paths/"..count,"paths/"..count)
fs.copy("disk/returns/"..count.."r","returns/"..count.."r")
count = count + 1
end
count=1
while count <= 36 do
x,y,z=rednet.receive()
print(y)
if y=="copy" then
fs.copy("paths/"..count,"disk/startup")
fs.copy("returns/"..count.."r","disk/return")
fs.copy("dark","disk/dark")
rednet.broadcast("copied")
sleep(1)
fs.delete("disk/dark")
fs.delete("disk/return")
fs.delete("disk/startup")
count = count + 1
end
end
count=1
while count <=36 do
x,y,z=rednet.receive()
print(y)
if y=="waiting" then
count = count + 1
end
end
if count == 36 then
rednet.broadcast("go")
print("Excavation started.")
end

also, so its clear, the main turtle (we will jsut call him 37) sets up the computer, places a modem, and then turns on the computer so it runs the startup file on the disk, which copies all the paths, returns and the dark file. afterwards turtle 37 removes the computer disk and replaces it with a blank disk to facilitate moving the files from the computer to the drone turtle (1-36) when it received the rednet signal 'copy' after it copies the files, it sends a signal saying it is copied, then turtle 37 turns on the drone, the drone fuels itself, the moves out of its starting position and sends a signal 'moved' which then lets turtle 37 place the next turtle and the whole thing is supposed to repeat for all 36 turtles.

here is the portion of code used to start and copy files and turn on the drones that is on turtle 37. i didn't post the portion where he goes around and sets up the chests because the whole program is 1k lines of code.

--turn on device
function itemOn()
t=peripheral.wrap("front")
t.turnOn()
end
-- turtle places miners
rednet.open("right")
count=1
while count <= 36 do
turtle.select(16)
turtle.place()
sleep(.1)
rednet.broadcast("copy")
x,y,z=rednet.receive()
print(y)
if y=="copied" then
  itemOn()
  count = count + 1
end
x,y,z=rednet.receive()
print(y)
if y=="moved" then
  sleep(.1)
end
end
-- turtle picks up miners
count=1
while count <= 36 do
if turtle.detect() then
  turtle.dig()
  count=count+1
else
  turtle.sleep(.01)
end
end
turtle.forward()
while true do
turtle.turnRight()
end

and here is an example of one of the turtle programs to show what the drone does as far as saying it has moved out of the way


--Tunneler
function tunnel()
goForward()
turtle.digUp()
turtle.placeUp()
turtle.digDown()
end

-- Ensure turtle goes up
function goUp()
while not turtle.up() do
  upDig()
end
end
--Ensure turtle goes down
function goDown()
while not turtle.down() do
  downDig()
end
end
--place floor
function pFloor()
if turtle.detectDown() then
goForward()
else
turtle.placeDown()
goForward()
end
end
-- Ensure turtle moves forward
function goForward()
while not turtle.forward() do
  forwardDig()
end
end
-- Dig forward
function forwardDig()
while turtle.detect() do
  turtle.dig()
end
end
-- Dig up
function upDig()
while turtle.detectUp() do
  turtle.digUp()
end
end
-- Dig Down
function downDig()
while turtle.detectDown() do
  turtle.digDown()
end
end
fs.copy("disk/dark","dark)"
fs.copy("disk/return","return")
turtle.turnRight()
turtle.suck()
turtle.refuel(8)
turtle.drop()
turtle.turnLeft()
turtle.turnLeft()
--Turtle1
goDown()
rednet.broadcast("moved")
goDown()
goForward()
goForward()
turtle.turnLeft()
goForward()
goDown()
turtle.turnRight()
turtle.broadcast("waiting")
shell.run("dark")

EDIT:

adding the program dark


x,y,z=rednet.receive()
print(y)
if y=="go" then
shell.run("excavate 10")
end
shell.run("return")
guamie #8
Posted 03 February 2013 - 02:25 AM
bump
drats666 #9
Posted 03 February 2013 - 04:58 AM
could it be that t.turnOn()is not defined? so when it tries to reference it, it doesn't find it so it reports nill or doesn't exist? looking through your code i dont see any declaration of t.turnOn() perhaps you missed something from the original source that was required.
guamie #10
Posted 03 February 2013 - 05:16 AM
Hey,

I see what you're saying, but if you take that part of the code, and run it by itself it works just fine. I took the code from the wiki if I remember right…. On my phone so it makes it hard to look up references.

If you see my second post in the thread I show how I tested the function with two turtle, one with only that function, and one with a startup file that just spins it in place, it works with no problem.
drats666 #11
Posted 03 February 2013 - 05:57 AM
hmmm not sure doesn't look like the whole code is here still, only thing i can think of off the top of my head right now is that when running dark by itself or the one specific function by itself that it has specific inputs which doesn't get passed along when you embedd it into another program. rather had to say without seeing the code that specifically declares the function t.turnon() though i am still learning lua myself. for better help maybe you should post both the scripts you are running (the one on the seeder turtle and the one running on the 36 others)

however after doing some research i found a previous post that may be of some help to you. http://www.computerc...n-on-computers/
the solution to that post was that the wrap function for connect to another pc/turtle had to start once the two turtles were infront of eachother. perhaps you are having the same issue, check to make sure your trying to turn the machine one only once your turtle is infront of it. if that link doesn't help perhaps this one will http://www.computercraft.info/forums2/index.php?/topic/3904-turning-on-a-turtle-without-touching-it/ it specifically references your t.turnon() function.
hope this helps
guamie #12
Posted 03 February 2013 - 08:47 AM
I'm stuck in an airport at the moment…

Tonight I will fully upload everything to pastebin and take a couple screenshots to better illustrate. What I'm doin
ChunLing #13
Posted 03 February 2013 - 09:13 PM
Are you positive that it has a turtle in front of it when this code is called?

Put some debug prints in the function itemOn() so you can see what t is. print(type(t)) and print(type(t.turnOn)) before t.turnOn().