So far, each monitor typically only redraws if it is directly interacted with or when the program first launches. I expect this to produce NO lag as a redraw only occurs when a monitor is touched. (I am also using 1x1 montiors, so a redraw is only 150 chars+color data. That's like what, one packet to send out?)
However, some of the screens I can display automatically refresh. In all instances where more than one monitor is being drawn at a time, I sleep for a tick in-between monitors, and refreshes can only occur every two seconds at the fastest. This is my main event loop:
repeat
event,device,clickX,clickY = os.pullEvent()
if event == "monitor_touch" then
--irrelevant
elseif event == "timer" and device == refreshTimer then
refreshTimer = os.startTimer(updateRate) --timer initially started outside the loop
updateScreens("refresh", count)
elseif event == "peripheral" then
--irrelevant
elseif event == "peripheral_detach" then
--irrelevant
end
count = count + 1
until event == "key"
This is the part that checks if a screen needs to be redrawn:
function updateScreens(trigger,ID,mX,mY)
if trigger == "draw" then
screen[mon[ID].screen].draw(ID)
elseif trigger == "refresh" then
--Redraw any screens that request it
local count = ID
for k, v in pairs(mon) do
if screen[mon[k].screen].refresh ~= nil then --each screen can define it's own refresh to be how many cycles between draws
if count % screen[mon[k].screen].refresh == 0 then
screen[mon[k].screen].draw(k)
sleep(0.05) -- Drawing can cause lag;
end -- if
end -- not nil
sleep(0.05) --sleep even we we don't draw anything; I was having a strange issue where the interface wouldn't see monitor touches
on occasion if there wasn't at least one monitor being refreshed. Bandaid? Maybe.
end -- for
elseif --other possible draw events removed for brevity and irrelevance
end -- clear
end -- function()
Will this be enough to prevent multiplayer lag, or are there other steps I should take? Am I being too careful, and I could speed things up? I'm afraid to just go online and test it in the offchance it gets me in trouble. I get no lag in singleplayer, but we all know that doesn't mean anything.
However, while the GAME does not lag adding more monitors does seem to slow down the interface a bit and cause it to sometimes miss clicks; what could I do to make the refresh process more light-weight so it doesn't bog the program down?