Is multithread the only solution?
If yes, can you explain me the basic commands for lua?
Thanks
-- put local breakMainLoop = false at the top, above your functions, set it to true and return if you want to break the loop.
while true do
if breakMainLoop then break end
local event, p1, p2 = os.pullEvent()
if event == "char" then
yourCharEventFunction(p1, p2)
elseif event == "rednet" then -- may be rong
yourRedNetEventFunction(p1, p2)
end
end
I think the events are queued, and will fire one after the other as they arrive.
IIRC, rednet has collision detection, and will only allow one machine to send at a time.
If you have multiple bundles connected, then you can receive two at the same time, but the one that fires first will be arbitrary.
while true do
event, arg1 arg2 = os.pullEvent()
if event == "rednet_message" then
doSomething(arg1, arg2) --function
end
end
I think the events are queued, and will fire one after the other as they arrive.
IIRC, rednet has collision detection, and will only allow one machine to send at a time.
If you have multiple bundles connected, then you can receive two at the same time, but the one that fires first will be arbitrary.
only one machine at a time is allowed to send message, ok.
hypotetical scenario:
2+ clients are connected (through the same bundled cable) to a server. Clients send a message simultaneously to the server which is scripted like this:while true do event, arg1 arg2 = os.pullEvent() if event == "rednet_message" then doSomething(arg1, arg2) --function end end
are all the messages completely delivered to the server? The order is arbitrary, right (that's not a problem to me)?
rednet.open("back")
os.startTimer(0.05)
local listen = false
while true do
local event, p1, p2 = os.pullEvent()
if event == "timer" then
os.startTimer(0.05)
else
print("Event: " .. event, " p1: ", p1, " p2: ", p2)
end
if event == "rednet_message" then
print(string.format("Recieved rednet message from %n, message: %s", p1, p2))
if p2 == "start" and listen then
print("Sleeping for 10 seconds, then sending message...")
print(os.clock()) -- Both computers should show the same #
sleep(10) -- Both machines should recieve this at the same tick;
rednet.broadcast("I am computer #" .. os.computerID())
end
end
if event == "char" then
if p1 == "a" then
rednet.announce()
elseif p1 == "b" then
print("broadcasting!")
rednet.broadcast("start")
elseif ep1 == "l" then
listen = true
print("Listen ON")
end
end
end
if p2 == "start" and listen then
print("Sleeping for 10 seconds, then sending message...")
print(os.clock()) -- Both computers should show the same #
sleep(10) -- Both machines should recieve this at the same tick;
rednet.broadcast("I am computer #" .. os.computerID())
end
Hey Advert, I didn't yet look deeply into the program logic, but I looked into the problem you were having where your program seems to keep hanging.I'm not quite sure what's causing this, it could be faulty collision handling :)/>/>
EDIT: Pardon me, but that was not completely right. The else will always kick in, regardless of any other event.^^
Omg, you're completely right. I confused myself heavily it seems. That means my original post was indeed correct and I overcorrected myself.ok, but after the 'else' there are other 'if', in fact it reach 'print(string.format("Recieved rednet message from %n, message: %s", p1, p2))' but stops just after it i also tried removing everything about the timerEDIT: Pardon me, but that was not completely right. The else will always kick in, regardless of any other event.^^
rednet.open("back")
rednet.receive(50)
CROSSTALKER PC:while true do
rs.setBundledOutput("back", 3)
sleep(0.05)
rs.setBundledOutput("back", 0)
end
SENDER PC:rednet.open("back")
rednet.broadcast("hello")
On the listener I then always received something different on each run.Aw that's not fair, stop teasing us! :D/>/>Lordy lordy 1.3 hi-speed modems…you should see how fast this shit flies.
-- Run the shell
local ok, err = pcall( function()
parallel.waitForAny(
function()
rednet.run()
end,
function()
os.run( {}, "rom/programs/shell" )
end
)
end )
if not ok then
print( err )
end
As you can see, both rednet.run, as well as the shell program are run in "parallel".
function cmdInput()
term.clear()
term.setCursorPos( 1, 2 )
while true do
write(">: ")
local input = read()
if input == "hi" then print("Hi yourself!") end
if ( string.lower( input ) == "quit" ) or ( string.lower( input ) == "exit" ) then break end
end
end
function rednetListener( side )
rednet.open( side )
while true do
local event, id, message = os.pullEvent()
if event == "rednet_message" then
local currX, currY = term.getCursorPos() -- Note current cursor position.
term.setCursorPos( 1, 1 )
term.clearLine()
write( "Message from ID#"..id..": "..message )
term.setCursorPos( currX, currY ) -- Reset cursor position to where it was before.
rs.setOutput( "top", true )
sleep(1)
rs.setOutput( "top", false )
end
--if event == "char" and string.lower( id ) == "q" then
--rednet.close( side )
--break
--end
end
end
local ok, err = pcall( function()
parallel.waitForAny(
function()
rednetListener( "back" )
end,
function()
cmdInput()
end
)
end )
if not ok then
print( err )
end
If you execute this program, then you will be presented with a text input, which is running from within the function cmdInput.rs.setOutput( "top", true )
I had a lamp on top of the computer and wanted to see ingame if the incoming rednet events are indeed being processed the moment the event fires, and not after I produced another event by interacting with the computer.