Is there a way to set a timeout for waiting for an event in Lua? (Opencomputer's lua)

107 Views Asked by At

I was creating a script which would send something and wait for it to be received back, but it just hang.

Here's my script:

local component = require("component")
local event = require("event")
local m = component.modem -- get primary modem component
m.open(80)
print(m.isOpen(80)) -- true

while true do


local timeout = 3
local timeoutEnded = false
function(
wait(timeout)
timeoutEnded = true
end)
local re = io.read()
m.broadcast(80, re)
while timeoutEnded == false do
local message = event.pull("modem_message")
end
if message == re then
print("sent success")
else
print("dammit")
end
end

when running, it would just hang up at waiting for "local message = event.pull("modem_message")" but since i was trying to get it to tell me if it transmitted successfully within 1 seconds and this was not my purpose i am clueless on how to make it timeout and continue executing (also i'm new to lua)

0

There are 0 best solutions below