Not logged inGosu Forums
Forum back to libgosu.org Help Search Register Login
Up Topic Gosu / Gosu Exchange / Errors error
- By lol_o2 Date 2010-05-31 13:51
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.
- By jlnr (dev) Date 2010-05-31 15:47
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…
- By BlueScope Date 2010-05-31 16:27
At least that sounds very much like me, running an x64, so this might really be the reason. For the record, I haven't found a way how to solve this...
- By lol_o2 Date 2010-05-31 16:36
Yes, I have 64 bit.
- By jlnr (dev) Date 2010-05-31 17:12
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? :/
- By BlueScope Date 2010-06-05 17:10 Edited 2010-06-05 17:23
While I don't know how to help ya on solving this, would you mind sharing your idea, as that might have an actual clue on how to temp-solve it... no?

That is really a bugger...

EDIT: I'm running Ruby 1.8.7 by the way...
- By jlnr (dev) Date 2010-06-07 09:49
Well, what does happen if you put a begin…rescue block inside draw and print the exception yourself? Does that work?
- By BlueScope Date 2010-06-07 12:34
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.
- By jlnr (dev) Date 2010-06-07 18:49
Does anything happen at all if you do something like exit(1)? I am curious if the rescue gets triggered at all before hanging.
- - By BlueScope Date 2010-06-18 17:51
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!

Just thought you might be interested...
Parent - - By kyonides Date 2010-06-18 21:16
But it should say...

@windows.each{ |window| window.update } unless @windows.nil? or @windows.empty?
Parent - By BlueScope Date 2010-06-19 01:33
I realize that; but as I wanted to illustrate the matter of described fact, I left the code unchanged. Thanks anyway though ;)
Parent - By BlueScope Date 2010-06-19 16:29
As an update to what I said earlier, it also happens upon encoutering a wrong variable type, for example trying to do math operations with strings.
- - By lol_o2 Date 2011-04-24 19:34 Edited 2011-04-25 07:31
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.
Parent - - By Quit Date 2011-04-30 00:58
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.
Parent - - By lol_o2 Date 2011-04-30 08:03
Really? For me it's working. I think it's not so good only if someone changes some variables directly before drawing.
Parent - - By Quit Date 2011-04-30 08:34
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.
Parent - - By jlnr (dev) Date 2011-04-30 17:14
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?)
Parent - By Quit Date 2011-04-30 17:30
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.
Parent - By lol_o2 Date 2011-04-30 18:07
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'.
Parent - - By psadda Date 2011-04-30 21:40 Edited 2011-04-30 21:52
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
Parent - By erisdiscord Date 2011-05-01 01:32
For what it's worth, you can also stick a rescue clause directly at the end of a method, module or class definition, saving a level of nesting.

def update
  update_callback
rescue SystemExit
  close
rescue Exception => e
  puts e.message
  puts e.backtrace
  close
end

def draw
  draw_callback
rescue SystemExit
  close
rescue Exception => e
  puts e.message
  puts e.backtrace
  close
end
Parent - By jlnr (dev) Date 2011-05-01 06:02
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!

https://github.com/jlnr/gosu/blob/master/lib/gosu/swig_patches.rb

I'll extend this approach to all the other callbacks in the next version.
Parent - By jlnr (dev) Date 2011-06-04 16:00
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 :)
Up Topic Gosu / Gosu Exchange / Errors error

Powered by mwForum 2.29.7 © 1999-2015 Markus Wichitill