-1

Since i'm writing a game in RoR, i need to have a game loop that is responsible for checking different things every time a page refresh happens. My question is, what is the best way to implement ?

I currently include the game_loop in my application controller. Is this the best practice ?

Jonas
  • 121,568
  • 97
  • 310
  • 388
Spyros
  • 46,820
  • 25
  • 86
  • 129
  • Not sure if there is a common best practice for this, but yes, if you need to do things every time on every page, having it in the application controller seems like a good idea. – Robin Feb 26 '11 at 22:46

1 Answers1

1

Executing the game look as a before_filter in your ApplicationController sounds reasonable, although you may not wish to put your logic in this class:

class ApplicationController < ActionController::Base
  before_filter :do_game_loop

  private

    def do_game_loop
      Game.do_game_loop # the implementation of Game is in another file, maybe in lib
    end
end

Do note that this will execute the game loop before every action in your application that involves a controller that extends from ApplicationController, including user login, logout, etc. It may be better to add the before_filter only in the controllers that definitely need to process the game loop.

Michelle Tilley
  • 157,729
  • 40
  • 374
  • 311