Hi, in my computer i would like it to be able to receive text input from a user, but during that time, i would like the computer also to do something if it receives a rednet message, is this possible?
Computer able to receive two events
Started by Zepher48, Jun 07 2012 01:52 AM
7 replies to this topic
#1
Posted 07 June 2012 - 01:52 AM
#2
Posted 07 June 2012 - 02:48 AM
Yes. You can use a loop with os.pullEvent(). Catch "char" events and add the result to a string and display it on screen so the user knows what they're typing, and catch Rednet_Message" events and do what you need to do with those.
#3
Posted 07 June 2012 - 03:03 AM
local terminate = false function handleEvent() evt, p1, p2 = os.pullEvent() if evt == "char" then writeChar(p1) elseif evt == "key" then handleKey(p1) elseif evt == "rednet_message" then messageReceive(p1, p2) elseif evt == "timer" then handleTimers(p1) end end function writeChar(character) CODE end function handleKey(key) CODE end function messageReceive(id, message) CODE end function handleTimers(timer) CODE end while terminate == false do handleEvent() end
#4
Posted 07 June 2012 - 04:32 AM
Thank you so much this is exactly what I was looking for, and also what would an event be called if it was a redstone input?
#5
Posted 07 June 2012 - 04:37 AM
Okay, i found out myself by just printing out the event, its called "redstone" =D
#6
Posted 07 June 2012 - 07:36 AM
Ive ran into a few problems with the os.pullEvent() function
I am making an elevator script
When i run my code and enter a number, the function that runs if evt="redstone" is called which i dont understand why after debugging a while
and also it seems that my loop at the bottom where i run handleEvents() is ran twice even if i just type one number, it runs for entering a char and a key, which is another thing i want to fix, im still trying to learn exactly how this pullEvent function works, any help is greatly appreciated
This is my code
I am making an elevator script
When i run my code and enter a number, the function that runs if evt="redstone" is called which i dont understand why after debugging a while
and also it seems that my loop at the bottom where i run handleEvents() is ran twice even if i just type one number, it runs for entering a char and a key, which is another thing i want to fix, im still trying to learn exactly how this pullEvent function works, any help is greatly appreciated
This is my code
function handleEvents()
evt, p1,p2 = os.pullEvent()
print("EVENT ="..evt)
if evt == "char" then
destination = p1
destination = tonumber(destination)
print("Destination entered is '"..destination.."'")
rednet.broadcast("z002"..destination)
floor = destination
changeMyFloor()
end
if evt == "rednet_message" then
temp = p2
temp = string.sub(p2,1,4)
if temp == "z002" then
floor = string.sub(p2,5,6)
print("Floor="..floor)
floor = tonumber(floor)
changeMyFloor()
end
end
if evt == "redstone" then
callE()
sleep(1)
end
end
-----
function callE()
print("nCalling Elevator...")
rednet.broadcast("z002"..compID)
floor = compID
changeMyFloor()
end
--------
function changeMyFloor()
floor = tonumber(floor)
compID = tonumber(compID)
if floor > compID then
rs.setOutput("right",true)
else
rs.setOutput("right",false)
end
end
-----
floor = 1
destination = 1
compID = os.getComputerLabel()
rednet.open("left")
while true do
print("You are currently on Floor "..compID..".")
write("nWhat Floor would you like to go too? ")
handleEvents()
end
#7
Posted 07 June 2012 - 09:11 AM
Try using elseif instead of if, and put char before key. Your code is running through all of the if clauses to see if an event matches, you only need to go until you find a match.
EDIT: Misunderstood what you were asking (though I'd still use elseifs) why does it matter if your pullEvent() code is triggered twice if it doesn't actually do anything? It's going to catch all events, that's what it does. Just don't handle the ones you don't need.
EDIT: Misunderstood what you were asking (though I'd still use elseifs) why does it matter if your pullEvent() code is triggered twice if it doesn't actually do anything? It's going to catch all events, that's what it does. Just don't handle the ones you don't need.
#8
Posted 08 June 2012 - 01:59 AM
Your code is hard to read without formatting. Also you have a lot of repetitive things in here. I've commented on things that I saw.
Now here's the code if I'd have written it. I am in NO way stating that this would work. I simply took your code and took out a lot of repetitive/unnecessary things.
Spoiler
Now here's the code if I'd have written it. I am in NO way stating that this would work. I simply took your code and took out a lot of repetitive/unnecessary things.
Spoiler
1 user(s) are reading this topic
0 members, 1 guests, 0 anonymous users











