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

[Lua][Error] Turtle/rednet

Started by craftminer502, 13 April 2012 - 06:29 PM
craftminer502 #1
Posted 13 April 2012 - 08:29 PM
Hi i started lua yesterday so i don't know much about it.
I made a turtle with rednet connection that accepts commands like forward, up, down, etc from a computer but the only command that work's is forward.
My code:
turtle code:

shell.run("clear")
rednet.open("right")
print("Remote Turtle")
var = 1
while var == 1 do
    action, senderID, text = os.pullEvent()
    
    if action == "rednet_message" then
        if text == "forward" then
            turtle.forward()
        if text == "back" then
            turtle.back()
        if text == "up" then
            turtle.up()
        if text == "down" then
            turtle.down()
        if text == "left" then
            turtle.turnLeft()
        if text == "right" then
            turtle.turnRight()
        if text == "exit" then
            var == 2
        end
    end
end

computer code:

rednet.open("right")
print("Remote Turtle Control Computer")
print()
var = 1
while var == 1 do
    write("Enter a Remote Turtle command:  ")
    input = read()
    if input == "xit" then
        var = 2
    else
        rednet.broadcast(input)
    end
end
I really hope someone can help me with this :P/>/>
cant_delete_account #2
Posted 13 April 2012 - 08:37 PM
READ POST TWO POSTS DOWN (Fixed fixed code xD)
craftminer502 #3
Posted 13 April 2012 - 08:39 PM
Thanks i will test this now.
cant_delete_account #4
Posted 13 April 2012 - 08:41 PM

shell.run("clear")
rednet.open("right")
print("Remote Turtle")
var = 1
repeat
sleep(0)
	action, senderID, text = os.pullEvent()
  
	if action == "rednet_message" then
		if text == "forward" then
			turtle.forward()
end
		if text == "back" then
			turtle.back()
end
		if text == "up" then
			turtle.up()
end
		if text == "down" then
			turtle.down()
end
		if text == "left" then
			turtle.turnLeft()
end
		if text == "right" then
			turtle.turnRight()
end
		if text == "exit" then
			var = 2
		end
	end
end
until var == 2
Here's the FIXED code. I fixed the exit.
craftminer502 #5
Posted 13 April 2012 - 08:45 PM
It doesn't work. Only the forward command works.
OmegaVest #6
Posted 13 April 2012 - 09:12 PM
Hey, I can't quite say what might be wrong (nothing looks out of place save you use a lot of ifs and no elseifs), but you can debug some of this yourself by adding a return feed into your code.

So, at the end of your while loop, or before the until, add:

rednet.send(senderID, text)

and on the computer, add in the else clause, after the broadcast:


feedback = rednet.receive(5)
if feedback then
   print(feedback)
else
   print("Error, " .. input .. " command failed.")
end 

Might help.
Luanub #7
Posted 13 April 2012 - 09:27 PM
Its always best to us if elseif when you are only checking 1 var against multiple conditions for 1 event. Using just ifs over and over can mess with your logic, I've seen several cases of this lately. Try this…


shell.run("clear")
rednet.open("right")
print("Remote Turtle")
var = 1
repeat
sleep(0)
		action, senderID, text = os.pullEvent()

		if action == "rednet_message" then
				if text == "forward" then
						turtle.forward()
				elseif text == "back" then
						turtle.back()
				elseif text == "up" then
						turtle.up()
				elseif text == "down" then
						turtle.down()
				elseif text == "left" then
						turtle.turnLeft()
				elseif text == "right" then
						turtle.turnRight()
				elseif text == "exit" then
						var = 2
				end
		end
until var == 2
craftminer502 #8
Posted 13 April 2012 - 09:57 PM
LOL I just found out i used the wrong turtle script.
Thanks for the help anyway :P/>/>