Posted 14 February 2017 - 08:51 AM
I'm currently working on making a queue system for a train system and I've run into a strange problem. When receiving a rednet signal the first if statement won't work if I put quotes around the required protocol, despite the fact that the else if statements work with quotes around the required protocol. The code in its current state works but I'm trying to figure out why this is happening.
Server Code
Client Code
Server Code
local queue = {}
rednet.open("back")
term.clear()
term.setCursorPos(1,1)
--checks if a destination is already in the queue
function inqueue(element)
for _, value in pairs(queue) do
if value == element then
return true
end
end
return false
end
--ends the program
function quit()
while true do
local event, key = os.pullEvent("key")
if key == 20 then
os.reboot()
end
end
end
--receives a destination and adds it to the queue
function receivedestination()
while true do
local senderId, message, protocol = rednet.receive()
if protocol == arrival then
table.remove(queue, 1)
else if protocol == "destination" then
table.insert(queue, message)
else if protocol == "origin" and inqueue(message) == false then
table.insert(queue, message)
end
end
end
term.clear()
term.setCursorPos(1,1)
local text = textutils.serialize(queue)
print(text)
end
end
parallel.waitForAll(receivedestination, quit)
Client Code
local station = 1 --current station number
rednet.open("right")
term.clear()
term.setCursorPos(1,1)
while true do
local event, key = os.pullEvent("key")
if key == 2 and station ~= 1 then
rednet.send(23,station, "origin")
rednet.send(23,1, "destination")
else if key == 3 and station ~= 2 then
rednet.send(23,station, "origin")
rednet.send(23,2, "destination")
else if key == 4 and station ~= 3 then
rednet.send(23,station, "origin")
rednet.send(23,3, "destination")
else if key == 22 then
rednet.send(23,u, "arrival")
else if key == 20 then
error()
end
end
end
end
end
print("Key Sent at")
print(os.time())
end