Not logged inGosu Forums
Forum back to libgosu.org Help Search Register Login
Up Topic Gosu / Gosu Exchange / Gosu::Window Singleton mixin Issue
- By asoules Date 2009-07-10 01:50
Based on the Space Shooter demo app by Belén Albeza González, I've been using a Singleton instance of Gosu::Window. However, I ran into and issue when trying to access the attributes of the window, specifically mouse_x and mouse_y. I can't seem to find a way to access those instance variables while using the Singleton mixin. Has anyone else run into this problem?
- By Basic Date 2009-07-10 07:11 Edited 2009-07-10 09:50
I've not run into any problems with singleton classes. and just to test the mouse_x and mouse_y coords


require 'rubygems'
require 'gosu'
require 'singleton'

class Game < Gosu::Window
 
  include Singleton

  def initialize
    super(800, 600, false)
    @test_mouse = TestMouse.new
  end
 
  def update
    @test_mouse.update
  end
 
  def draw
    @test_mouse.draw
  end
 
end

class TestMouse
 
  def initialize
    @colour = Gosu::red
  end
 
  def update
  end
 
  def draw
    mouse_x = Game.instance.mouse_x
    mouse_y = Game.instance.mouse_y
   
    Game.instance.draw_quad(mouse_x, mouse_y, @colour,
                            mouse_x + 2, mouse_y, @colour,
                            mouse_x, mouse_y + 2, @colour,
                            mouse_x + 2, mouse_y + 2, @colour, 1)
  end
 
end

Game.instance.show


this works for me
- By asoules Date 2009-07-10 12:52
Thanks for the help. I tried game.instance but my game locked up so I assumed that was not the correct syntax. Thanks again.
- By jlnr (dev) Date 2009-07-10 15:05
I think Ruby's singleton doesn't solve circular dependencies. I used it in one game and had the lockup too. My Window called SomeGameObject.new in its constructor, which in turn called Image.new(MyWindow.instance, ...), and so it ended up being an endless loop. Kind of sad that the singleton lib didn't even check for this very common problem :(

My workaround was to just create the first "state" after the Window was constructed (and accessible via MyWindow.instance). The future, supercool workaround will of course happen in Gosu itself, once Image.new doesn't need the Window arg anymore. The next release might already be very close to that.
- By Basic Date 2009-07-10 15:26
nothing gets past you jlnr
- By philomory Date 2009-07-10 22:40

> Kind of sad that the singleton lib didn't even check for this very common problem :(


What's more sad is, this is, according to Matz, the correct behavior: http://www.ruby-forum.com/topic/179676

Here's my workaround: OperationLamba/app/MainWindow.rb:31
more here: MainWindow.rb:43 and here: MainWindow.rb:105
- By asoules Date 2009-07-11 03:51
Thanks for the help. I haven't had a chance to get back into the code today, but I'll get back on it tomorrow and try to access the mouse position.

Somehow I missed that operation lambda was open source, or I would have been looking at it for tips :)
- By asoules Date 2009-07-11 04:16
Alright, just jumped into the code for a minute, turns out I was calling Game.instance inside a constructor which was getting called inside the constructor for the play state, which was getting initialized by my Gosu::Window. So, my solution was as simple as initializing my variables to zero, then setting them based on the mouse position in the update loop. Hopefully I won't make that mistake again.

Thanks for the quick help!
Up Topic Gosu / Gosu Exchange / Gosu::Window Singleton mixin Issue

Powered by mwForum 2.29.7 © 1999-2015 Markus Wichitill