I am a bit confused as to what your goal is... Is it about showing a loading picture when graphics.update hasn't been called for a while or is it to get rid of the script hanging error?
Well, either way turns towards a solution of using threads.
Here is a script snippet where the Graphics module is altered to keep track of last time Graphics.update was called.
A separate thread is started. It keeps track of how long time has passed since the last time Graphics.update was called. if more than 4 seconds has passed, it will call Graphics.update. (It will only check once every second)
if @zer_no_hang_stack.nil?
##
# Change the Graphics module so it contains the time of the last update
# Add a getter for the time property
#
module Graphics
# Alias the update method (you have to do it this way since Graphics is a module)
class << self
alias no_hang_update update
end
##
# Change the update method
#
def self.update
@@time = Time.now
self.no_hang_update
end
##
# Retrieve the Time at the last update
#
def self.time
# Protection if this method is called before the first update
@@time = Time.now if @@time.nil?
return @@time
end
end
##
# A separate thread that will run and keep track of the time since the last
# update
#
Thread.new {
loop do
# Lets the thread sleep for a while to minimize CPU usage
sleep 1
# If more than 4 seconds has passed since the last update
if Time.now - Graphics.time > 4
# Update the graphics
Graphics.update
end
end
}
@zer_no_hang_stack = true
end
You must note that if you use this snippet your code becomes indeterministic. There is the potential of Graphics.update being called from at any time within your main code, which means any area dealing with graphics may potentially generate errors. It may just be graphical bugs or it may be bugs that will actually crash your game. I can't tell you. All I can tell you is that updating the graphics while drawing something on a bitmap is potentially dangerous. In addition. Recreating a situation where you previously got an error may not yield that error again, or may only yield it sometimes.
So yeah, this is not a snippet you should mindlessly place in your project.
It would be better if you explained why you want to prevent the script hanging bug. In which situation does it happen. Has it something to do with establishing a connection to a server/client? Some large generating mechanism/script?
*hugs*
- Zeriab