Posted 20 July 2013 - 07:48 PM
Hi, guys, old Lua scripter here, but new to ComputerCraft. I seem to be having problems with writes from one parallel function not being read by the other. I'm using parallel.waitForAll() with two functions that run forever ("while true do" loops). I have a variable that was declared outside of the functions, in the script's scope. One function waits for rednet input, then adjusts the variable's value. The other function loops and reads the variable's value, using it to display text on a monitor. The issue is, the value never changes. Although the write is clearly successful in one thread, it fails in the other. Here is a snippet of my code to show what I mean:
Even after it receives a rednet signal that one room has closed, it continues to show "open" and "true". Here are pictures to demonstrate:
Upon startup
Right after closing one room (display hasn't updated the screen yet)
After display() updates the screen again
Is this a problem with the parallel API itself, or am I just doing something wrong?
local rooms = {true, true, true, true} --true = unoccupied, false = occupied; all rooms start off occupied
rednet.open("top")
monitor = peripheral.wrap("right")
monitor.setTextScale(2.5)
term.redirect(monitor)
function listen() --waits for rednet input, then writes to "rooms" table
while true do
id, mes = rednet.receive(nil)
local room = string.sub(mes,1,-2)
local open = tonumber(string.sub(mes,-1,-1))
write("Room " .. room .. " is now ") --Debug check
if open > 0 then
print("open!")
rooms[room] = true
else
print("closed!") --This does, in fact, print
rooms[room] = false
end
print("The value at index " .. room .. " is now " .. tostring(rooms[room])) --Sanity check: it does print false
sleep(5) --Shouldn't get messages too frequently
end
end
function display() --Reads table, displays on monitor
while true do
term.setBackgroundColor(colors.black)
term.clear()
term.setCursorPos(1,1)
for i=1,#rooms do
print("The value at index " .. i .. " is " .. tostring(rooms[i])) --Sanity check: always prints true!
term.setBackgroundColor(colors.black)
term.setTextColor(colors.white)
write("Room " .. i .. ": ") --Actual display
if rooms[i] then
term.setBackgroundColor(colors.green) --Always gets printed, even after claiming to have closed the room
term.write("Empty")
else
term.setBackgroundColor(colors.red)
term.write("Taken")
end
term.setBackgroundColor(colors.black)
print(" (" .. tostring(rooms[i]) .. ")") --Final sanity check: also always prints true!
end
sleep(10) --Will be decreased, but currently high for debugging purposes
end
end
parallel.waitForAll(listen, display)
Even after it receives a rednet signal that one room has closed, it continues to show "open" and "true". Here are pictures to demonstrate:
Upon startup
Right after closing one room (display hasn't updated the screen yet)
After display() updates the screen again
Is this a problem with the parallel API itself, or am I just doing something wrong?