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

[Lua] [Error] os.pullEvent?

Started by deity12, 16 June 2012 - 11:34 PM
deity12 #1
Posted 17 June 2012 - 01:34 AM
Hey, os.pullEvent() is causing me multpiple problems by not working for me. What am I doing wrong? For example:

while true do
os.pullEvent()
if event == "char" and param1 == "q" then
print ("You pressed Q!")
break
else
print ("You did not press Q")
break
end
end
Every time I press q I get. "You did not press Q!" Why? Can somebody please explain?
tfoote #2
Posted 17 June 2012 - 01:54 AM
you are not storing a variable for the os.pullEvent(). In order for this to work correctly you need to use this (code below)… I also recommend this webpage…

Webpage:
http://computercraft.info/wiki/index.php?title=Os.pullEvent

Code:

while true do
local x = os.pullEvent("char") -- you don't need the in event == char thing now
if x == "q" then
print("You pressed Q!")
break
else
print("You did not press Q")
break
end
end

Let me explain a little more… You must specify which variables the parameters set to…
In my code i specified that parameter 1 was x. this way i would check x to see if it was q.
Also i limited the event type by using os.pullEvent("char")
This way i do not get other types of events.

Let me know if this all works for you and check out that API!
MysticT #3
Posted 17 June 2012 - 01:55 AM
You have to store the return values of os.pullEvent to use them:

local event, param1 = os.pullEvent()
if event == "char" and param1 == "q" then

Edit:

you are not storing a variable for the os.pullEvent(). In order for this to work correctly you need to use this (code below)… I also recommend this webpage…

Webpage:
http://computercraft...le=Os.pullEvent

Code:

while true do
local x = os.pullEvent("char") -- you don't need the in event == char thing now
if x == "q" then
print("You pressed Q!")
break
else
print("You did not press Q")
break
end
end

Let me explain a little more… You must specify which variables the parameters set to…
In my code i specified that parameter 1 was x. this way i would check x to see if it was q.
Also i limited the event type by using os.pullEvent("char")
This way i do not get other types of events.

Let me know if this all works for you and check out that API!
os.pullEvent returns the event type even when you give it a parameter, so you need to do:

local event, x = os.pullEvent("char")
event will always be "char", but you need it anyway.
deity12 #4
Posted 17 June 2012 - 02:48 AM
Sorry, tfoote what you gave me still does the same thing. I'm not to sure what you're trying to say sorry MysticT? If you could give me a fixed version of my code I could probably learn from it. I also have another question, no sense in creating another thread.
So, I have this program which sends messages to a turtle, which acts accordingly, the turtle part of the code is going fine but the client end (from the computer) is having problems. Rednet :347: positive umber expected.
Here is the code:

rednet.open("right")
term.clear()
term.setCursorPos(1,1)
print ("Which turtle would you like to send commands to?")
r = io.read()
while true do
term.clear()
term.setCursorPos(1,1)
print ("Turtlecom wireless (client) v1.4 Running.")
print ("Type 'help' for help or 'exit' to exit.")
x = io.read()
if x == "help" then
term.clear()
term.setCursorPos(1,1)
print ("This program sends commands to the previously designated turtle. Typing 'exit' will cause both the client and turtle to close the program. Commands are as follows:")
print ("w = forward")
print ("s = back")
print ("a = turn left")
print ("d = turn right")
print ("q = up")
print ("z = down")
print ("r = place up")
print ("f = place")
print ("v = place down")
print ("Will return to interface in 10 seconds")
sleep(10)
elseif x == "exit" then
rednet.send(r, "exit")
break
elseif x == "w" then
rednet.send(r, "w")
elseif x == "s" then
rednet.send(r, "s")
elseif x == "a" then
rednet.send(r, "a")
elseif x == "d" then
rednet.send(r, "d")
elseif x == "q" then
rednet.send(r, "q")
elseif x == "r" then
rednet.send(r, "r")
elseif x == "f" then
rednet.send(r, "f")
elseif x == "v" then
rednet.send(r, "v")
else
print ("That is not a valid commans")
sleep(1)
end
end
I'm pretty sure I know why, but I'm not sure how to fix it? io.read() returns a string right? Therefore 'r' is a string and rednet won't accept a string as the receiver id. But I'm not sure how to get use input without the output being a string, rather than number. Is there a specific function? I don't need a rewrite of my code just the name of the function (unless of course that isn't the problem). Thanks in advance!
MysticT #5
Posted 17 June 2012 - 03:38 AM
Fixed code for pullEvent:

while true do
  local event, param1 = os.pullEvent("char")
  if param1 == "q" then
    print ("You pressed Q!")
    break
  else
    print ("You did not press Q")
    break
  end
end

Sorry, tfoote what you gave me still does the same thing. I'm not to sure what you're trying to say sorry MysticT? If you could give me a fixed version of my code I could probably learn from it. I also have another question, no sense in creating another thread.
So, I have this program which sends messages to a turtle, which acts accordingly, the turtle part of the code is going fine but the client end (from the computer) is having problems. Rednet :347: positive umber expected.
Here is the code:

- snip -
I'm pretty sure I know why, but I'm not sure how to fix it? io.read() returns a string right? Therefore 'r' is a string and rednet won't accept a string as the receiver id. But I'm not sure how to get use input without the output being a string, rather than number. Is there a specific function? I don't need a rewrite of my code just the name of the function (unless of course that isn't the problem). Thanks in advance!
The problem is that read() returns a string, and rednet.send needs a number. You have to convert it with tonumber:

local id = tonumber(read())
tfoote #6
Posted 17 June 2012 - 04:40 AM
Fixed code for pullEvent:

while true do
  local event, param1 = os.pullEvent("char")
  if param1 == "q" then
	print ("You pressed Q!")
	break
  else
	print ("You did not press Q")
	break
  end
end
BTW: That code was given by MisticT

Do you have to have the event part in…
local event…
??
deity12 #7
Posted 17 June 2012 - 05:36 AM
The problem is that read() returns a string, and rednet.send needs a number. You have to convert it with tonumber:

local id = tonumber(read())
Thanks! Knew that rednet needed a number and that it was being given a string, but did't know the function name thanks!
EDIT: Just tryed out your pellevent thingy to. Works great thanks!
MysticT #8
Posted 17 June 2012 - 05:42 PM
Fixed code for pullEvent:

while true do
  local event, param1 = os.pullEvent("char")
  if param1 == "q" then
	print ("You pressed Q!")
	break
  else
	print ("You did not press Q")
	break
  end
end
BTW: That code was given by MisticT

Do you have to have the event part in…
local event…
??
os.pullEvent returns the event type even when you give it a parameter, so you need to do:

local event, x = os.pullEvent("char")
event will always be "char", but you need it anyway.