When making a mistake in code, in command prompt should appear message like "Undefined method draw for NilClass" etc. and it's known where is the problem. But I don't know why, errors for me are not displaying. When there is a bug in draw, application just tries to make this method again and game is just getting stock. It's annoying, because many times I don't know what is wrong. Is there any way to do something with it? I have ruby 1.9.
Are you on 64-bit Windows? Someone else had this problem on the issue tracker, but because I don't have a 64-bit Windows, I cannot even test if this happens on all 64-bit Windows installations or not…
I would really love to know if there are other C extensions which pass control back to Ruby code, and if the problem is the same for them. If not, I have a starting point to get rid of this. Does any one come to your minds? :/
No, already tried that... it just seems to hold the script indefinately, which is why I was talking about a "script hanging" error when I was first talking to you about this...
Another noteable thing is that when i drag the Window instance along the screen, it lags horribly.
exit(1) just closes the application, without any further notice or hanging screens.
No, I think the most if not only (didn't keep track close enough to say it now :/ ) issue is undefined variables. Regarding that, I found a very interesting thing you might want to check out... so, let's have some code first:
def self.draw @windows.each{ |window| window.draw } unless @windows.empty? end
def self.update @windows.each{ |window| window.update } unless @windows.empty? end
These are two methods from my GUI script I'm working on. By never initializing @windows, it will be an undefined instance variable (you sure know that, but yeah... XD ). Now, running the script like this, it gives me a black screen, however, when I comment out the line within self.draw (and therefore not calling that one, but the one in update next), I get "line 50 in update: undefined method `empty?' for nil:NilClass (NoMethodError)"... strange!
Pretty old subject, but I found some solution since this problem lasts unfixed and someone may be interested. Code below uses $game variable as a game state.
class GameWindow < Window def initialize super(640,480,false) #initialize window @drawn=true #special variable to check bug end
def update $game.draw if @bug #call draw method in update when error occurs $game.update #update the game state end
def draw @bug=!@drawn #set bug value if drawing fails @drawn=nil #reset drawing value $game.draw if !@bug #draw the game state if there's no error @drawn=true #variable for successful drawing end end
And how does it work? When error occurs while drawing, Gosu starts drawing again and makes a loop. This code uses a variable to check if the drawing has ended. If not, drawing code is skipped and called in update. So after error, application crashes and you can see what's wrong. Nice :) Hope it's useful.
It's a horrible idea to call drawing related stuff in update - you will crash the application, sure, but that won't necessarily be the error that happened earlier.
As far as I know (and back then it was like that), draw is called by Gosu when everything was set for drawing. This isn't the case in update - and if you are trying to draw stuff within update, everything could happen.
Yes, that is true. Of course, it's only a workaround around an uglier bug, so I guess, whatever works just works at least...
lol_o2: Can you "rescue" the exception object (rescue Exception => e, around $game.draw), then save it into a local variable and raise it later? (Maybe in update, where it seems to work for you?)
In my opinion it's not working to call $game.draw within update to get the exception - because whatever the exception was, it could be gone or it could be something completely differently.
I don't know exactly what did you meant, but I used this 'rescue Exception => e ' and the variable was able to put even after exception. Test code (in draw):
draw_line(0,25,0xffffffff,50,0) rescue Exception => e puts e
The message it's showing: "wrong # of arguments(5 for 6)"
And Quit: I don't know what 'everything' and 'something' you are talking about. My method have already caught successfully above ten errors, and they wasn't 'something different'.
I posted this in another thread. I think it might be somwhat of a better solution. Someone else said that this approach did not work for them, but it works for me on 64 bit Windows and I have been running into the same issues with exceptions in the draw method.
class GosuWindowWrapper < Gosu::Window def initialize super(640, 480, false) end def update_callback #... end def draw_callback #... end def update begin update_callback rescue SystemExit close rescue Exception => e puts e.message puts e.backtrace close end return nil end def draw begin draw_callback rescue SystemExit close rescue Exception => e puts e.message puts e.backtrace close end return nil end end
Thanks for all the input here. Gosu already does something similar to what you are doing in 0.7.30/0.7.31 (the C wrapper calls update_ and draw_). Calling 'close' sounds like it would be a useful addition!
I am now rescuing and saving the exception, then re-throwing it at the end of show; also doing this for all callbacks, not just the major two.
However, I have no idea about SystemExit and from a quick irb'ing don't understand it. Since this is about the first pure-Ruby bug ever and I am on github, anyone who can reproduce this bug (not me) can test & merge a solution :)