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

Computer able to receive two events

Started by Zepher48, 07 June 2012 - 12:52 AM
Zepher48 #1
Posted 07 June 2012 - 02:52 AM
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?
D3matt #2
Posted 07 June 2012 - 03: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.
Bossman201 #3
Posted 07 June 2012 - 04: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
Zepher48 #4
Posted 07 June 2012 - 05: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?
Zepher48 #5
Posted 07 June 2012 - 05:37 AM
Okay, i found out myself by just printing out the event, its called "redstone" =D
Zepher48 #6
Posted 07 June 2012 - 08: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

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
D3matt #7
Posted 07 June 2012 - 10: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.
Bossman201 #8
Posted 08 June 2012 - 02: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.
Spoiler

function handleEvents()  --Try not to have too many things inside this function.
evt, p1,p2 = os.pullEvent()
print("EVENT ="..evt)
if evt == "char" then  --Remember to use elseif statements
destination = p1
destination = tonumber(destination)
print("Destination entered is '"..destination.."'")
rednet.broadcast("z002"..destination)
floor = destination --Code not necessary (see below)
changeMyFloor()  --Change to "changeMyFloor(floor)" this runs the changeMyfloor() function but with parameter "floor" already inside.
end
if evt == "rednet_message" then
temp = p2
temp = string.sub(p2,1,4)
if temp == "z002" then
floor = string.sub(p2,5,6) --6 not necessary, also hurts scaleability by making the floor a single digit number. Change to "floor = tonumber(string.sub(p2, 5)). If there's no second parameter, it will read to the end. Also, commands can be nested like so.
print("Floor="..floor)
floor = tonumber(floor) --Code not necessary
changeMyFloor() -- "changeMyFloor(floor)"
end
end
if evt == "redstone" then
callE()
sleep(1)  --Recommend: Remove
end
end
-----
function callE()
print("nCalling Elevator...")
rednet.broadcast("z002"..compID)
floor = compID -- Remove
changeMyFloor() --"changeMyFloor(compID)"
end
--------
function changeMyFloor()
floor = tonumber(floor) --Not necessary since you did it before you called the function.
compID = tonumber(compID)  --May not be necessary, don't remember if os.computerID() returns a string or an integer.
if floor > compID then
rs.setOutput("right",true)
else
rs.setOutput("right",false)
end
end
-----
floor = 1  --Remember to declare your variables with "local" and put these at the top so you can easily edit them.
destination = 1
compID = os.getComputerLabel() -- Try using "os.computerID()". This code will probably return nil and insta-error.
rednet.open("left")
while true do  --Try creating a way to exit the loop.
print("You are currently on Floor "..compID..".")
write("nWhat Floor would you like to go too? ")
handleEvents()
end

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


local terminate = false
local floor = 1
local destination = 1
compID = tonumber(os.computerID())
rednet.open("left")
print("You are currently on Floor "..compID..".")
write("nWhat Floor would you like to go to? ")

function handleEvent()
 evt, p1, p2 = os.pullEvent()
 print("EVENT = " .. evt)
 if evt == "char" then
  handleChar(p1)
 elseif evt == "redstone" then
  callE()
 elseif evt == "rednet_message" then
  handleMessage(p2)
 end
end

function handleChar(character)
 character = tonumber(character)
 print("Destination entered is '" .. character .. "'")
 rednet.broadcast("z002" .. destination)
 changeMyFloor(destination)
end

function handleMessage(temp)
 temp = string.sub(p2,1,4)
  if temp == "z002" then
   floor = tonumber(string.sub(p2,5))
   print("Floor = " .. floor)
   changeMyFloor(floor)
  end
end

function changeMyFloor(floor)
 local compID = tonumber(os.computerID())
 if floor > compID then
  rs.setOutput("right",true)
 else
  rs.setOutput("right",false)
 end
end

function callE()
 print("nCalling Elevator...")
 rednet.broadcast("z002"..compID)
 floor = compID
 changeMyFloor()
end

while terminate == false do
 print("You are currently on Floor "..compID..".")
 write("nWhat Floor would you like to go to? ")
 handleEvent()
end