←  Ask a Pro

ComputerCraft | Programmable Computers for Minecraft

»

Program error.

crazyguymgd's Photo crazyguymgd 14 Jan 2013

one thing I also noticed when I ran this on a turtle is tmDown() doesn't actually work. This is because the command to move down is just turtle.down(). And the reason it seems to do nothing when you run it is because you have a ton of sleep(1)s in there. just wait 15 seconds and you'll start getting errors that will help you find all the problems.
Quote

theoriginalbit's Photo theoriginalbit 14 Jan 2013

When I get back to my computer I will write up a 'refactored' program with a bunch of fixes and improvements...
Quote

detsuo04's Photo detsuo04 14 Jan 2013

so i fixed all those problems eccept the never initialised problem, Im not sure what it means. though i tested it and it works. anyway
at line 114 it has a nil error but the only thing there is turtle.down()
if there is a block below the turtle it gives a nil error also but at line 107 another turtle.down()
rednet.open("right")
rn = rednet.send
fx = turtle.getItemCount(16)
a = fx - 1
sleep(1)
rn (base , "Connection established")
sleep(1)
rn (base , "getting fuel level")
sleep(1)
turtle.getFuelLevel()
local f = turtle.getFuelLevel()
sleep(1)
rn (base , "Current fuel level is "..f.."" )
sleep(1)
if f < 100 then
  turtle.getItemCount(16)
  rn (base , "current coal stack is")
  turtle.select(16)
  turtle.refuel(a)
	else
	rn (base , "Fuel levels are acceptable")
	sleep(1)
  end
sleep(1)
rn (base , "getting coal count")
turtle.getItemCount(16)
sleep(1)
rn (base, "I have "..fx.." Coal")
sleep(1)
if fx < 64 then
  rn (base, "DANGER! Coal is low")
  os.reboot()
  else
	rn (base, "Coal storage is full")
	sleep(2)
	  end
sleep(1)
rn (base , "comencing program")
--=======comence mining=======--
z = 0
--===========fuel=============--
function fuel()
print("1")
f = turtle.getFuelLevel()
  if f < z + 10 then
  rn (base , "Danger! fuel levels low.")
  turtle.select(16)
  turtle.refuel(a)
f =  turtle.getFuelLevel()
	if f < z + 10 then
	rn (base , "Insuficient Coal, Returning.")
	home()
	 end
end
end
--=======return code==========--
function home()
  turtle.Up(z + 2)
  rn (base , "Mining Operation finished")
  os.shutdown()
end
--============value===========--
function value()
if turtle.detect() == false
then return false
end
  for i = 1,3 do
	 turtle.select(i)
	 if turtle.compare() then
		 return false
		 end
end
end
--============walls===========--
function walls()
  for j = 1,4 do
	if value() then
	turtle.dig()
	rn (base, "ORE!")
	  else
	  turtle.turnright()
	  end
  end
end
--=============home===========--
function home()
turtle.Up(z)
rn (base , "Done, ready for pickup")
os.shutdown()
end

--===========Dig Code=========--
function mine()
print("1")
fuel()
print("2")
  turtle.detectDown()
   if turtle.detectDown() == true then
   turtle.digDown()
   turtle.detectDown()
   print("3")
	if turtle.detectDown() == true then
	rn (base , "I hit bedrock, Returning")
	home()
	  else
	  turtle.Down()
	 print("4")
	  walls()
	  print("5")
	  z = z + 1
	  end
	  else
	   turtle.Down()
	   walls()
	   z = z + 1
	   end
   end

   --===========start=========--
   while true do
   mine()
   end
Quote

theoriginalbit's Photo theoriginalbit 14 Jan 2013

not initialised means that you aren't telling it what the variable base is... it thinks its nil... but you need a number to send a rednet message...
Quote

detsuo04's Photo detsuo04 14 Jan 2013

so rednet.send <reciever name> <msg> isn't right?

the monitor displays the message though. anyway im getting nils off of turtle.down now. i was so sure it was right :(

Quote

one thing I also noticed when I ran this on a turtle is tmDown() doesn't actually work. This is because the command to move down is just turtle.down(). And the reason it seems to do nothing when you run it is because you have a ton of sleep(1)s in there. just wait 15 seconds and you'll start getting errors that will help you find all the problems.

the program dosn't stall thoug it just terminates and back the os screen
Quote

theoriginalbit's Photo theoriginalbit 14 Jan 2013

View Postdetsuo04, on 14 January 2013 - 01:20 AM, said:

so rednet.send <reciever name> <msg> isn't right?

From the wiki http://computercraft...iki/Rednet_(API)

"rednet.send(receiverID, message) — Sends a message to the computer using the opened sides. The receiver ID is the ID (number, not string) of the computer you're sending the message to. ID as nil will do the same as a broadcast."
Quote

detsuo04's Photo detsuo04 14 Jan 2013

Oh nifty, i looked at that earlier but i miss understood it to mean computer lable. fixed now with a base = 13
Quote

detsuo04's Photo detsuo04 14 Jan 2013

daily bump?
i have poured over the code and as far as i can tel the code is good and turtle.down() is a command it should accept with no reason to return nil but i get a nil error on it in two places. :( any ideas would be awsome.
Quote

theoriginalbit's Photo theoriginalbit 14 Jan 2013

has your code changed? and are your trying to run this ON a turtle? ( some people try to run it on a computer not realising that the computer doesn't know about the turtle api )
Quote

crazyguymgd's Photo crazyguymgd 14 Jan 2013

turtle movements:

turtle.up()
turtle.down()
turtle.turnRight()
turtle.turnLeft()

notice where everything is capitalized. You often try to use turtle.Down() which wont work. Neither with turtle.turnright() and so on.
Quote

crazyguymgd's Photo crazyguymgd 14 Jan 2013

I also saw a point where you use turtle.Up(z + 2). These don't take parameters. To get this effect, you would do:

for i = 1, z+2 do
  turtle.up()
end
Quote

theoriginalbit's Photo theoriginalbit 14 Jan 2013

View Postcrazyguymgd, on 14 January 2013 - 12:00 PM, said:

I also saw a point where you use turtle.Up(z + 2). These don't take parameters. To get this effect, you would do:

for i = 1, z+2 do
  turtle.up()
end
Could have been trying to use my "pro tip" without actually fully implementing it :P
Quote

detsuo04's Photo detsuo04 14 Jan 2013

ill check my downs.

i am running it on a turtle yes. i have been fiddling for hours now and i think i have narrowed down to one last error before i have a working program.

Im using print # to find my error point and im getting a phantom end

I have been messing with it a bit so its a bit of a jumbled mess now. but for some reason the code returns 2222222 ect forever.

my nested ifs are to blame i think but i am having trouble narrowing down the problem.

EDIT: ok my downs where capped so yay thats one problem down. thanks for the help still the phantom end though
while true do
print("2")
f = turtle.getFuelLevel()
  if f < z + 10 then
   rn (base , "Danger! fuel levels low.")
   turtle.select(16)
   turtle.refuel(all)
   f =  turtle.getFuelLevel()
   if f < z + 10 then
   rn (base , "Insuficient Coal, Returning.")
   home()
   else
	if turtle.detectDown() == true then
	turtle.digDown()
	turtle.detectDown()
	print("3")
	 if turtle.detectDown() == true then
	 rn (base , "I hit bedrock, Returning")
	 home()
	  else
		turtle.Down()
		walls()
		z = z + 1
	end
   end
  end
  end
Quote

theoriginalbit's Photo theoriginalbit 14 Jan 2013

this
while true do
print("2")
is the reason it prints 222222222 continuous... its an infinite loop and at the top you have print( "2" )


EDIT: More comments on how to fix it

while true do
  f = turtle.getFuelLevel()
  if f < z + 10 then
    rn (base , "Danger! fuel levels low.")
    turtle.select(16)
    turtle.refuel(all) -- refuel( all ) wouldn't work... use refuel() to 'refuel all'
    f =  turtle.getFuelLevel()
    if f < z + 10 then
      rn (base , "Insuficient Coal, Returning.")
      home()
    else
      if turtle.detectDown() == true then -- == true is not needed here
        turtle.digDown()
        turtle.detectDown() -- this doesn't do anything... not needed
        print("3") -- another spamming print
        if turtle.detectDown() == true then -- == true is not needed here
          rn (base , "I hit bedrock, Returning")
          home()
        else
          turtle.down() -- still have a capital d
          walls()
          z = z + 1
        end
      end
    end
  end
end

Edited by TheOriginalBIT, 14 January 2013 - 12:10 PM.
Quote

detsuo04's Photo detsuo04 14 Jan 2013

i was hoping the "while true do" would effect the rest of the code. does it only work on the next line?

if i take out the while true do it just returns 2 then ends the program. so there is some reason it is shutting off after print 2
Quote

crazyguymgd's Photo crazyguymgd 14 Jan 2013

Did you read TheOriginalBITs tips on ifs? Because you didn't seem to follow them...
Quote

theoriginalbit's Photo theoriginalbit 14 Jan 2013

View Postcrazyguymgd, on 14 January 2013 - 12:10 PM, said:

Did you read TheOriginalBITs tips on ifs? Because you didn't seem to follow them...
*says in the cheering mannor* here, here... I've noticed that too... it seems like he did a selective read...
Quote

detsuo04's Photo detsuo04 14 Jan 2013

It's not so much that i didn't read im just having trouble wrapping my brain around it. i also scrammbled alot of my if stuff around when i was blindly tinkering to fix errors

if condition true then
check condition 2 if condition true then
do this
if condition 1 not true do this
if condition 2 not true do ths
Quote

theoriginalbit's Photo theoriginalbit 14 Jan 2013

View Postdetsuo04, on 14 January 2013 - 12:15 PM, said:

if condition true then
check condition 2 if condition true then
do this
if condition 1 not true do this
if condition 2 not true do ths

Not quite
if condition 1 then
  if condition 2 then
   do this
  else not condition 2 then
  end
else not condition 1 then
end

EDIT: And sorry I did miss nested ifs in the tips/explanation didnt i....
Edited by TheOriginalBIT, 14 January 2013 - 12:19 PM.
Quote

crazyguymgd's Photo crazyguymgd 14 Jan 2013

I rode a turtle all the way to bedrock with this code.
z = 0
while true do
  print("2")
  f = turtle.getFuelLevel()
  if f < z + 10 then 
    rn (base , "Danger! fuel levels low.")
    turtle.select(16)
    turtle.refuel()
    z = turtle.getFuelLevel()
    if f < z + 10 then
      rn (base , "Insuficient Coal, Returning.")
      home()
    end
  elseif turtle.detectDown() then
    turtle.digDown()
    --turtle.detectDown()
    print("3")
    if turtle.detectDown() then
      rn (base , "I hit bedrock, Returning")
      print("Your home function")
      --home()
      break  -- to break out of the while loop
    end
  else
      turtle.down()
      print("your walls function")
      --walls()
      z = z + 1
  end
end 
Quote