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

os.pullEvent not working?

Started by madscientist712, 02 March 2015 - 06:20 PM
madscientist712 #1
Posted 02 March 2015 - 07:20 PM
I have been trying to make a code that allows me to remotely control a turtle from my computer but whenever i press the button or try anything on my program it does not work. im thinking the os.pullEvent is wrong as im not good with that. Any answers?
NOTE:Not fully finished left and right on the turtle but forwards and backwards is done.

[Computer code]
rednet.open("right")

term.write("please select slave: ")
local id = read()
sleep(2)
term.clear()

while true do
local event = os.pullEvent("key")

if key == keys.w then
print("forward")
rednet.broadcast("fw")
elseif key == keys.s then
print("backward")
rednet.broadcast("bw")
elseif key == keys.a then
print("left")
rednet.broadcast("lt")
elseif leys == keys.d then
print("right")
rednet.broadcast("rt")
end
end


[END]

[Turtle Code]

rednet.open("right")
local message = rednet.receive()

while true do

if message == "fw" then

turtle.dig()
turtle.forward()

end end
[END]
SquidDev #2
Posted 02 March 2015 - 07:22 PM
Where you have

local event = os.pullEvent("key")

you should have

local event, key = os.pullEvent("key")

The first variable stores the name of the event, the second variable stores the current key.

You can use
(without spaces) to wrap code blocks.
Edited on 02 March 2015 - 06:22 PM
madscientist712 #3
Posted 02 March 2015 - 07:32 PM
i did this and it works on the computer but the turtle doesn't move and reboots. Any reason why?
MKlegoman357 #4
Posted 02 March 2015 - 07:37 PM
rednet.receive returns the sender ID first, then the message.
madscientist712 #5
Posted 02 March 2015 - 07:49 PM
Thanks. Just wondering also. how would i make it so i put in a number and when it sends the message it uses that?
because I have it so i enter the id of the turtle but when i put
rednet.send(id, message). ( message bieng what I pressed e.g forward) it says
rednet:87: Excepted number
madscientist712 #6
Posted 02 March 2015 - 08:03 PM
I have a code which can move a turtle but I want it to go to a certain turtle. i tried doing this by typing it in before I go to control and have it represented as "id". However I get the error
rednet:87:expected number and dont know how to fix it.


rednet.open("right")

term.write("please select slave: ")
local id = read()
sleep(2)
term.clear()

while true do
local event = os.pullEvent("key")

if key == keys.w then
print("forward")
rednet.send(id, "fw")
elseif key == keys.s then
print("backward")
rednet.send(id, "bw")
elseif key == keys.a then
print("left")
rednet.send(id, "lt")
elseif leys == keys.d then
print("right")
rednet.send(id, "rt")
end
end
Quintuple Agent #7
Posted 02 March 2015 - 08:06 PM
read() returns a string, use tonumber() to change it to a number
Ex:
id = tonumber(read())
madscientist712 #8
Posted 02 March 2015 - 08:07 PM
ok thanks
Dog #9
Posted 02 March 2015 - 08:08 PM
Read returns a string. Since you need a number for your id, you would use tonumber…

local id = tonumber(read())

EDIT: with the thread merge, I've been ninja'd :P/>
Edited on 02 March 2015 - 10:31 PM
Lyqyd #10
Posted 02 March 2015 - 09:08 PM
Threads merged. Please stick to one thread for all questions about a given piece of code.
madscientist712 #11
Posted 03 March 2015 - 04:22 PM
I have a new code for making my turtle move which uses functions but when I use my computer to move the turtle it ends the program. I think it is to do with the functions as when i have used them in the past it just ended it but in this case i would like to use them. Why doesnt this work?

Also when i did it without function (basically the same) it would only work once and with a while loop it would go forever. how do i make it move once at a time but also not stop receiving?



rednet.open("right")
id, message = rednet.receive()

function Dig()
if turtle.detect() then
turtle.dig()
end

function Left()
turtle.turnLeft()
end

function Right()
turtle.turnRight()
end

function Forward()
dig()
turtle.Forward()
end

function Backward()
dig()
turtle.Backward()
end

if message == "fw" then
Forward()
elseif message == "bw" then
Backward()
elseif message == "lt" then
Left()
elseif message == "rt" then
Right()
end end
[END]
madscientist712 #12
Posted 03 March 2015 - 05:06 PM
I have a new code for making my turtle move which uses functions but when I use my computer to move the turtle it ends the program. I think it is to do with the functions as when i have used them in the past it just ended it but in this case i would like to use them. Why doesnt this work?

Also when i did it without function (basically the same) it would only work once and with a while loop it would go forever. how do i make it move once at a time but also not stop receiving?



rednet.open("right")
id, message = rednet.receive()

function Dig()
if turtle.detect() then
turtle.dig()
end

function Left()
turtle.turnLeft()
end

function Right()
turtle.turnRight()
end

function Forward()
dig()
turtle.Forward()
end

function Backward()
dig()
turtle.Backward()
end

if message == "fw" then
Forward()
elseif message == "bw" then
Backward()
elseif message == "lt" then
Left()
elseif message == "rt" then
Right()
end end
[END]
KingofGamesYami #13
Posted 03 March 2015 - 05:39 PM
I'd guess the events sent are being eaten by the turtle movement functions - they call os.pullEvent internally and filter everything that isn't what they want. To fix this, simply use a call/response thing where the computer waits for the turtle to respond saying "I did it" or "I failed doing it".

PS: code tags

valithor #14
Posted 03 March 2015 - 05:39 PM
Right now you have a problem where you define your dig function. You do not close the function after the end for the if statement, so the dig function contains everything below it. You will want to put a end right after the end in the dig function. You will also want to delete one of the ends at the end.
Edited on 03 March 2015 - 04:40 PM
Goof #15
Posted 03 March 2015 - 05:48 PM
You're also calling the Dig function wrong…
Lua is case sensitive, so dig() won't work, if the function is declared as Dig()


so you need to, either change the function name:

function Dig()
or all the dig() commands.
madscientist712 #16
Posted 03 March 2015 - 06:42 PM
How would I do this call and response. Can you someone show me an example?
Edited on 03 March 2015 - 05:53 PM
KingofGamesYami #17
Posted 03 March 2015 - 07:27 PM
It's very simple.

Server:

send message

Client:

wait for message
execute movement
send message

Server:

receive message
repeat
madscientist712 #18
Posted 03 March 2015 - 07:42 PM
I did this but the turtl still ends the program. here is the edited version

P.S I got rid of the functions


rednet.open("right")

function Wait()
id, message = rednet.receive()
end

Wait()
if message == "fw" then
turtle.dig()
turtle.forward()
rednet.send(18, "Complete")
Wait()
elseif message == "bw" then
turtle.dig()
turtle.backward()
rednet.send(18, "Complete")
Wait()
elseif message == "lt" then
turtle.dig()
turtle.turnLeft()
rednet.send(18, "Complete")
Wait()
elseif message == "rt" then
turtle.dig()
turtle.turnRight()
rednet.send(18, "Complete")
Wait()
else
rednet.send(18, "Failed")
Wait()
end
Edited on 03 March 2015 - 06:49 PM
Bomb Bloke #19
Posted 04 March 2015 - 12:08 AM
Every command in your script runs once unless you specifically set up a mechanism for repeating it, such as a while or a repeat loop.

If you want your script to wait for a new command every time your loop repeats, then simply make sure your rednet.receive() call is inside the loop.
Dragon53535 #20
Posted 04 March 2015 - 01:01 PM
Quite simple, however your code is flawed, and everything is stuck inside your dig function.
A few things.
One, when moving back, you cannot dig there, it digs forward.
Two, it's turtle.back, not turtle.Backward.
Three, it's turtle.forward not turtle.Forward, capitalization matters.
Four, at your function dig, add and extra end at the end of that, and remove ONE end at the bottom of your script.
Five, if you want things to loop, put it inside the loop. rednet.receive stops the loop and waits until it receives a message, then continues. Ex:

while true do
  local id,message = rednet.receive()
  if message = "Codemeplease" then
    print("I like cake, not code, MWAHAHHAHHAHAHAHA")
  end
end
Six, attempt to indent your code, if you notice what I've done ^ is that when I hit a loop, function, or if statement, I add two spaces to each line under it, until I meet it's end, then I bring it back two spaces for the next lines.
Seven, you do not need your functions, they're useless actually. Just renaming old functions, and actually making your code slower.
Eight, attempt to use locals, they're a bit tricky, but are faster than globals. Which means that your variables in your script, can be accessed anywhere on the computer until it's shutdown. Locals, cannot. A little tutorial. Check the variable scope area.
Nine, when posting code onto the forums, try to do what I did, a little easier on the eyes :P/> [.CODE] YOUR CODE HERE [./CODE] Just put this, without the .'s and you will see. Or on the bar above your text, look for the blue <>, it will pop up a window for you to type your code into, and will do this for you.

That's about it, your immediate problem, was answered on number 4 and 5
madscientist712 #21
Posted 04 March 2015 - 07:19 PM
I have linked my code to wirelessly move a turtle and tracking it together and wanted to send its coords to a computer to put on a monitor but i get an error saying 14: ')' expected. Also have i done this tracking right as im not sure… also how would i have the coords update on the monitor after it moves
here is the code

rednet.open("right")

function Coords()
term.write("enter value of x:")
local x = tonumber(read())
term.write("enter value of y: ")
local y = tonumber(read())
term.write("enter value of z: ")
local z = tonumber(read())
term.write(" please enter the facing direction")
local f = tonumber(read())
while true do
rednet.send( 19, x y z )
end
if f = 5 then
f = 1
if f = 0 then
f = 4

while true do
if turtle.forward() then
if f = 1
x = x - 1
elseif f = 2 then
z = z - 1
elseif f = 3 then
x = x + 1
elseif f = f + 4 then
z = z + 1
elseif turtle.back() then
if f = 1 then
x = x + 1
elseif f = 2 then
z = z + 1
elseif f = 3 then
x = x - 1
elseif f = 4 then
z = z -1
elseif turtle.turnLeft() then
f = f - 1
elseif turtle.turnRight() then
f = f + 1
elseif turtle.up() then
y = y + 1
elseif turtle.down() then
y = y - 1
end
  function Wait()
id, message = rednet.receive()
end
function Sense()
if turtle.detect() then
turtle.dig()
end
end
while true do
Wait()
if message == "fw" then
Sense()
turtle.forward()
rednet.send(18, "Complete")
elseif message == "bw" then
Sense()
turtle.back()
rednet.send(18, "Complete")
elseif message == "lt" then
turtle.turnLeft()
rednet.send(18, "Complete")
elseif message == "rt" then
turtle.turnRight()
rednet.send(18, "Complete")
else
rednet.send(18, "Failed")  
	end
  end
Edited on 04 March 2015 - 06:20 PM
KingofGamesYami #22
Posted 04 March 2015 - 08:34 PM
The problem is here:

rednet.send( 19, x y z )

x, y, and z are three separate variables. Because you did not put commas between them, it will error. If you put commas between them, it'll only send x.

Plus, the while loop here will go on forever, probably causing a too long without yielding error.

You have, in fact, done tracking as completely wrong as I can imagine.

This thing is going to infinately repeat, until the turtle can't go forward, then move backward and forward repeatedly.

while true do
if turtle.forward() then
if f = 1
x = x - 1
elseif f = 2 then
z = z - 1
elseif f = 3 then
x = x + 1
elseif f = f + 4 then
z = z + 1
elseif turtle.back() then
if f = 1 then
x = x + 1
elseif f = 2 then
z = z + 1
elseif f = 3 then
x = x - 1
elseif f = 4 then
z = z -1
elseif turtle.turnLeft() then
f = f - 1
elseif turtle.turnRight() then
f = f + 1
elseif turtle.up() then
y = y + 1
elseif turtle.down() then
y = y - 1
end
madscientist712 #23
Posted 05 March 2015 - 03:59 PM
Ok I went back to the code and edited it so all the movements were linked to the tracking using functions. But now the whole program just doesn't run when I type it in. can someone show me what is causing it…


rednet.open("right")

function Coords()
term.write("enter value of x:")
local x = tonumber(read())
term.write("enter value of y: ")
local y = tonumber(read())
term.write("enter value of z: ")
local z = tonumber(read())
term.write(" please enter the facing direction")
local f = tonumber(read())
end
function Send()
while true do
rednet.send( 19,x)
rednet.send( 19, y)
rednet.send(19, z)
rednet.send(19, f)
end  
end
 
  function Wait()
id, message = rednet.receive()
end
function Sense()
if turtle.detect() then
turtle.dig()
end
end
function Forward()
Sense()
if f == 1 then
x = x - 1
elseif f == 2 then
z = z - 1
elseif f == 3 then
x = x + 1
elseif f == 4 then
z = z + 4
Send()
end
function Backward()
turtle.back()
if f == 1 then
x = x + 1
elseif f == 2 then
z = z + 1
elseif f == 3 then
x = x - 1
elseif f == 4 then
z = z - 1
Send()
end
function Left()
turtle.turnLeft()
f = f - 1
Send()
end
function Right()
f = f + 1
Send()
end
while true do
if f == 0 then
f = 4
if f == 5 then
f = 1
Wait()
if message == "fw" then
Forward()
rednet.send(18, "Complete")
elseif message == "bw" then
Backward()
rednet.send(18, "Complete")
elseif message == "lt" then
Left()
rednet.send(18, "Complete")
elseif message == "rt" then
Right()
rednet.send(18, "Complete")
else
rednet.send(18, "Failed")  
    end
  end
end end end end
madscientist712 #24
Posted 05 March 2015 - 06:05 PM
I changed my code to track where the turtle is but now when I run the code it doesn't start. Help please?

rednet.open("right")

function Coords()
term.write("enter value of x:")
local x = tonumber(read())
term.write("enter value of y: ")
local y = tonumber(read())
term.write("enter value of z: ")
local z = tonumber(read())
term.write(" please enter the facing direction")
local f = tonumber(read())
end
function Send()
while true do
rednet.send( 19,x)
rednet.send( 19, y)
rednet.send(19, z)
rednet.send(19, f)
end  
end
 
  function Wait()
id, message = rednet.receive()
end
function Sense()
if turtle.detect() then
turtle.dig()
end
end
function Forward()
Sense()
if f == 1 then
x = x - 1
elseif f == 2 then
z = z - 1
elseif f == 3 then
x = x + 1
elseif f == 4 then
z = z + 4
Send()
end
function Backward()
turtle.back()
if f == 1 then
x = x + 1
elseif f == 2 then
z = z + 1
elseif f == 3 then
x = x - 1
elseif f == 4 then
z = z - 1
Send()
end
function Left()
turtle.turnLeft()
f = f - 1
Send()
end
function Right()
f = f + 1
Send()
end
while true do
if f == 0 then
f = 4
if f == 5 then
f = 1
Wait()
if message == "fw" then
Forward()
rednet.send(18, "Complete")
elseif message == "bw" then
Backward()
rednet.send(18, "Complete")
elseif message == "lt" then
Left()
rednet.send(18, "Complete")
elseif message == "rt" then
Right()
rednet.send(18, "Complete")
else
rednet.send(18, "Failed")  
    end
  end
end end end end
The_Cat #25
Posted 05 March 2015 - 06:53 PM
To many "end" at the end (Try only 4 ends at the end)? (And is there no error message? It would appear in red writing)
Heres better indentation(Sorta):

while true do
if f == 0 then
  f = 4
  if f == 5 then
   f = 1
   Wait()
   if message == "fw" then
	Forward()
	rednet.send(18, "Complete")
   elseif message == "bw" then
	Backward()
	rednet.send(18, "Complete")
   elseif message == "lt" then
	Left()
	rednet.send(18, "Complete")
   elseif message == "rt" then
	Right()
	rednet.send(18, "Complete")
   else
	rednet.send(18, "Failed")
   end
  end
end
end
Edited on 05 March 2015 - 05:56 PM
Lyqyd #26
Posted 05 March 2015 - 07:02 PM
Threads merged. Please stick to one topic for all questions about a given program or piece of code.
Dragon53535 #27
Posted 05 March 2015 - 11:21 PM
To many "end" at the end (Try only 4 ends at the end)? (And is there no error message? It would appear in red writing)
Heres better indentation(Sorta):

while true do
if f == 0 then
  f = 4
  if f == 5 then
   f = 1
   Wait()
   if message == "fw" then
	Forward()
	rednet.send(18, "Complete")
   elseif message == "bw" then
	Backward()
	rednet.send(18, "Complete")
   elseif message == "lt" then
	Left()
	rednet.send(18, "Complete")
   elseif message == "rt" then
	Right()
	rednet.send(18, "Complete")
   else
	rednet.send(18, "Failed")
   end
  end
end
end
Well that's not gonna work, you need an elseif…

while true do
  if f == 0 then
	f = 4
  elseif f == 5 then
	f = 1
  end
  Wait()
  if message == "fw" then
	Forward()
	rednet.send(18, "Complete")
  elseif message == "bw" then
	Backward()
	rednet.send(18, "Complete")
   elseif message == "lt" then
	Left()
	rednet.send(18, "Complete")
   elseif message == "rt" then
	Right()
	rednet.send(18, "Complete")
   else
	rednet.send(18, "Failed")
   end
  end
end
Edited on 05 March 2015 - 10:21 PM
The_Cat #28
Posted 06 March 2015 - 08:07 AM
Well that's not gonna work, you need an elseif…

while true do
  if f == 0 then
	f = 4
  elseif f == 5 then
	f = 1
  end
  Wait()
  if message == "fw" then
	Forward()
	rednet.send(18, "Complete")
  elseif message == "bw" then
	Backward()
	rednet.send(18, "Complete")
   elseif message == "lt" then
	Left()
	rednet.send(18, "Complete")
   elseif message == "rt" then
	Right()
	rednet.send(18, "Complete")
   else
	rednet.send(18, "Failed")
   end
  end
end

Ahh i see. (: