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?
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
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
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
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