This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
Armitige's profile picture

Screen blinking on update

Started by Armitige, 22 November 2014 - 02:12 PM
Armitige #1
Posted 22 November 2014 - 03:12 PM
Hey there, me again :P/>

I'm having an issue with monitors. I've just finished my reactor/turbine control program to a level where I'm almost completely satisfied with it, with one exception. Whenever the monitor updates, sections of it occasionally blink black. I was wondering if this is because my code is badly optimised, or if it's just something that's unavoidable when refreshing monitors continuously in Computercraft.

Should I be avoiding completely refreshing the screen, and only write to it when something has changed (this would be extremely difficult I think due to the nature of the program)

Here's the pastebin for the source code if there's any kind soul out there that take a look at it and let me know if there's a better way (which undoubtedly there is) to do the display portion of the program.

http://pastebin.com/m0bxA5yi

If there's anyone that would like to see the program in action. I could make a void world with the reactor setup and computer/monitor setup available for download.
austinv11 #2
Posted 22 November 2014 - 07:22 PM
This is something that is unavoidable AFAIK. I've found that this happens when updating a monitor too fast. I'd recommend trying to either lower the refresh rate or make it only refresh when necessary. If you wanted to do this, I'd make a variable that can be accessed throughout the program which caches various values. And then when you update your stats, compare it to the cache. If it's different, then update the screen.
Lyqyd #3
Posted 22 November 2014 - 08:59 PM
It's not unavoidable by any means. You're clearing the entire screen, and then going through and gathering information to display. Clearing the screen should be the very last thing you do before displaying the updated information. You should be gathering up all the necessary information to display before you clear anything. That way, it will be cleared for the shortest amount of time possible before new information is populated. You may want to clear your windows individually, just before drawing to them, rather than clearing the entire screen at once in the setupScreen function.
Bomb Bloke #4
Posted 23 November 2014 - 01:18 AM
Or don't clear the windows at all - you could clear lines individually, or just outright draw over old bits of data. I find string.rep() handy for partial line clears.

It's also worth noting that while "windows" are handy for dividing up screen-space, any display updates you attempt through them will be slower than direct writes to a monitor. Again, this doesn't mean flicker is unavoidable, but it does mean you have to be a bit tighter in your coding.
Armitige #5
Posted 23 November 2014 - 05:54 AM
It's not unavoidable by any means. You're clearing the entire screen, and then going through and gathering information to display. Clearing the screen should be the very last thing you do before displaying the updated information. You should be gathering up all the necessary information to display before you clear anything. That way, it will be cleared for the shortest amount of time possible before new information is populated. You may want to clear your windows individually, just before drawing to them, rather than clearing the entire screen at once in the setupScreen function.


while true do
	    turbineControl()
	    setReactorRods()
	    mon[1].setBackgroundColor(colors.black)
	    mon[1].clear() -- This is the issue?
	    setupScreen()
	    displayStats()
	    os.sleep(0.5)
end -- while

I'll have a look at my functions that right stuff to the screens and try an optimize the number of clears I do, but if I remove the marked clear in the main loop, I end up with weird background colour issues that I've only been able to fix by doing a complete monitor clear. Thanks for clearing this up though, I can now at least do some more code optimisation to try and fix it knowing that it's definitely avoidable.

Or don't clear the windows at all - you could clear lines individually, or just outright draw over old bits of data. I find string.rep() handy for partial line clears.

It's also worth noting that while "windows" are handy for dividing up screen-space, any display updates you attempt through them will be slower than direct writes to a monitor. Again, this doesn't mean flicker is unavoidable, but it does mean you have to be a bit tighter in your coding.

Yeah, I had a feeling there was possibly an issue with using the windows, I've noticed that the bottom right hand window goes blank a lot more frequently than any other part of the screen. I'm not sure if I could remove the windows though, because of the way I'm handling the drawing of the turbine screens and data (using a for loop to draw each one consecutively).

Ideally I'd like to draw the fixed part of the screen once at the start of the program, then just write/re-write the data, but the issue I think I'd have is keeping the data right justified. I'll do some more testing around your ideas and see how I do.

The main thing is I know it's my code that's causing the issue, and not just beacuse Minecraft :P/>

Thanks Lyqyd, and thanks again Bomb Bloke, your help is invaluable :)/>
Armitige #6
Posted 23 November 2014 - 10:19 AM
Excellent, so between the advice Lyqyd has given me about cutting out on clearing the entire screen repeatedly, and the tip Bomb Bloke gave me to check out string.rep() I've managed to totally remove the blinking issue.

The program still updates all the data values whenever it runs through the while loop, but I've placed the initial screen layout outside that loop and only have to run it once.

Thanks again fellas!