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

[Lua][Error] Can't get my turtle program to work

Started by jasiu2002, 17 November 2012 - 11:12 PM
jasiu2002 #1
Posted 18 November 2012 - 12:12 AM
Hello! I am kinda new, but still watched some tutorials etc. I tried to make a lua program using rednet, wich basically works like I use rednet to say "up" and then my turltle goes up.
Code is:

rednet.open("right")
repeat
  local event,p1,p2,p3 = os.pullEvent()
  if event=="rednet_message" then
   rednet.send(p1, "Turtle ready")
   if p2 == "right" then
	turtle.turnRight()
   elseif p2 == "left" then
	turtle.turnLeft()
   elseif p2 == "back" then
	turtle.back()
   elseif p2 == "forward" then
	turtle.forward()
   elseif p2 == "up" then
	turtle.up()
   elseif p2 == "down" then
	turtle.down()
   elseif p2 == "checkforobstacles" then
	turtle.detect()
Instead if running this program returns me an error
"Attempt to index ? A nil value"

And I have no idea how to fix it. Thanks in advance.
Viproz #2
Posted 18 November 2012 - 01:13 AM
You forget to put until

repeat
[…]
until condition

And you forget to put an "end" after the last elseif.


If you want to your program repeat all time do that :

rednet.open("right")
while true do
  local event,p1,p2,p3 = os.pullEvent()
  if event=="rednet_message" then
   rednet.send(p1, "Turtle ready")
   if p2 == "right" then
	    turtle.turnRight()
   elseif p2 == "left" then
	    turtle.turnLeft()
   elseif p2 == "back" then
	    turtle.back()
   elseif p2 == "forward" then
	    turtle.forward()
   elseif p2 == "up" then
	    turtle.up()
   elseif p2 == "down" then
	    turtle.down()
   elseif p2 == "checkforobstacles" then
	    turtle.detect()
   end
end
jasiu2002 #3
Posted 19 December 2012 - 08:32 AM
Sorry for being late, wasn't playing Minecraft because it kept bugging. Thanks to you I get no error, but there is still a problem called my turtle doesn't want to move :?
Lyqyd #4
Posted 19 December 2012 - 09:00 AM
Ensure that your turtle is sufficiently fueled.
remiX #5
Posted 19 December 2012 - 09:37 AM
The code Viproz posted is missing another end…


rednet.open("right")
while true do
  local event,p1,p2,p3 = os.pullEvent()
  if event=="rednet_message" then
   rednet.send(p1, "Turtle ready")
   if p2 == "right" then
			turtle.turnRight()
   elseif p2 == "left" then
			turtle.turnLeft()
   elseif p2 == "back" then
			turtle.back()
   elseif p2 == "forward" then
			turtle.forward()
   elseif p2 == "up" then
			turtle.up()
   elseif p2 == "down" then
			turtle.down()
   elseif p2 == "checkforobstacles" then
			turtle.detect()
   end
  end
end

Also, would this not work instead of doing an elseif for each direction:

rednet.open("right")
while true do
    local event,p1,p2,p3 = os.pullEvent()
    if event=="rednet_message" then
        rednet.send(p1, "Turtle ready")
        if p2 == "checkforobstacles" then
            turtle.detect()
        elseif p2 == "left" then
            turtle.turnLeft()
        elseif p2 == "right" then
            turtle.turnRight()
        else
            turtle[p2]() -- Would this not work for forward, back etc..
        end
    end
end
ChunLing #6
Posted 19 December 2012 - 12:39 PM
Yeah. I would use it for all of them, in fact.
jasiu2002 #7
Posted 20 December 2012 - 07:08 AM
Ensure that your turtle is sufficiently fueled.

Wow thanks, I am sooo dumb. I need to turn off the fuel because I always forget about it.
@Remix I fixed it because error mesage said me about the problem, thanks anyways. And the idea for using p2 is good. Also could I ask how to receive boolean? I mean I'd like to make it like if it sees obstacle it sends the computer who requested obstacle check an message like "An obstacle has been detected"

EDIT: I refuelled it, doesn't work for some reason.
ChunLing #8
Posted 20 December 2012 - 05:35 PM
If you've revised your code at all, then post the current version. As noted, your old version won't run.
jasiu2002 #9
Posted 21 December 2012 - 06:26 AM
Here you go:

rednet.open("right")
while true do
  local event,p1,p2,p3 = os.pullEvent()
  if event=="rednet_message" then
   rednet.send(p1, "Turtle ready")
   if p2 == "right" then
					    turtle.turnRight()
   elseif p2 == "left" then
					    turtle.turnLeft()
   elseif p2 == "back" then
					    turtle.back()
   elseif p2 == "forward" then
					    turtle.forward()
   elseif p2 == "up" then
					    turtle.up()
   elseif p2 == "down" then
					    turtle.down()
   elseif p2 == "checkforobstacles" then
					    turtle.detect()
   end
  end
end
ChunLing #10
Posted 21 December 2012 - 06:43 AM
You're not getting an error, right? It just doesn't do what you want. What exactly is it not doing?
jasiu2002 #11
Posted 21 December 2012 - 07:30 AM
Basically, nothing. It just does nothing. Turtle is in the mode for receiving rednet messages, but if I use rednet.broadcast or rednet.send Turtle does not react. So if I do: rednet.send(0, "up") it doesn't do anything.
Orwell #12
Posted 21 December 2012 - 07:33 AM
Is it fueled? Maybe put some prints in there to see if it gets the message and what it is etc.
ChunLing #13
Posted 21 December 2012 - 08:00 AM
Jasiu said it was fueled.

Try this:
rednet.open("right")
repeat
    local event,p1,p2,p3 = os.pullEvent("rednet_message")
print(event," from :",p1,": '",p2,"' distance:",p3)
    if turtle[p2] then rednet.send(p1, "turtle."..p2.." returned "..tostring(turtle[p2](1)))
    else rednet.send(p1, "Invalid Command")    end
until p2 == "exit"
Usage is to send the last part of the turtle API command ("forward", "back", "up", "down", "detect", "compare", etc) as a rednet message.

Expected output is to print out the values from the rednet event, and if the message identifies a valid turtle API function, execute that function (with an argument of 1, for functions that need it), sending back a reply identifying the function and giving the return, otherwise sending the reply "Invalid Command". The loop will terminate upon reception of the message "exit" (after replying that it is an "Invalid Command", cause I'm lazy).

The console output on the turtle should let you know if the turtle is actually getting the messages your sending, and what they are.
jasiu2002 #14
Posted 22 December 2012 - 06:08 AM
After sending 2 commands to turtle my computer didn't get response from turtle, but on turtle there were 2 messages:
rednet_message from :1: 'up' distance:3 (testing what it prints on valid command)
rednet_message from :1: 'sandwich' distance:3 (this time tested what it prints on invalid command)
jasiu2002 #15
Posted 22 December 2012 - 06:13 AM
Okay so maybe something changed, because I had to do turtle.refuel() while I remember turtle refuelling itself. Sorry for all of this, but I am still a newbie. I really feel bad for all this mess, I could think of using the command before.
Anyways I would have one question. Would using

while turtle.detect() then
   rednet.send(p1, "Block detected")
 else rednet.send(p1, "No blocks detected")
Work like if I request turtle to detect a block it says block was detected if it was or wasn't if it wasn't? Or would it keep spamming my computer console with "No blocks detected"?
remiX #16
Posted 22 December 2012 - 11:27 AM
While loop does not use else, so no - It would not work. You could do something like this


detectedBlock = false
while not detectedBlock do
    print("No block!")
    if turtle.detect() then detectedBlock = true end  -- Will then stop the loop if it detects a block
end
ChunLing #17
Posted 22 December 2012 - 11:36 AM
It would spam the hell out of you.

Dang, ninja'd anyway!

Okay, back to the previous issue, the print messages indicate that you sent "up" and then "sandwich". But the turtle didn't move and didn't send you any message back…or you didn't get the message because it arrived almost immediately after you sent it. What is the program that you're using to send the rednet messages look like? If it doesn't immediately check for a rednet response, it's not going to see them.
Edited on 22 December 2012 - 10:41 AM
jasiu2002 #18
Posted 22 December 2012 - 10:56 PM
Ughh probally I am gonna play ComputerCraft when I am not sick. I forgot that rednet.receive is needed, but last days my mind is not made for thinking. I cannot think because every though in my head is replaced by how sick am I. Anyways I am planning on adding some things like using os.pullEvent for key control (it's annoying when I have to type every command by myself) and make it like turtle after receiving and completing task waits around half a second and sends something like "Task completed!", while sender program would execute rednet.receive() right after sending message. Also I got one question. Is there a difference if I juse keyboard "5" or numpad "5"? Because I am thinking of using numpad to steer robot (8 and 2 - forward and back, 4 and 6 - turn left and right, + and - would be up and down and then finally 5 would be attack).
ChunLing #19
Posted 22 December 2012 - 11:07 PM
If you use char events, there is no difference. But if you use key events, then the keyboard "5" is key 6 and the numberpad "5" is key 76. There's a list of key codes as they are used by minecraft on the minecraft wiki. It has been noted that the numberpad Enter generates the same code as the regular Enter, 28. There may be other irregularities, so use an event printing loop to check all the keys you use (I use key 0 to drop bombs, it's Shift+numpad5).