Main events of the game: overloading an event or a method?

If you are going to develop a game in, say, Ruby and you have been provided with a game framework, you would prefer to act on key up / down events by overloading the method in the main window like this:

class MyGameWindow < Framework::GameWindow
    def button_down(id)
        case id
            when UpArrow
                do_something
            when DownArrow
                do_something
        end
    end
end

      

Or have an event class with which you can create a method and assign a handle to it, for example:

class MyGameWindow < Framework::GameWindow
    def initialize
        key_down.add_handler(method(:do_something))
    end
    def do_something
        puts "blah blah"
    end
end

      

Please give your views which you think will be the best in game development and thanks in advance, ell.

+2


a source to share


1 answer


I prefer poll. Events (on Windows, anyway) have a nasty habit of getting lost. I've struggled long and hard with key and release button mice to get lost, so now I prefer to just check the state of every tick and do a set of operations to mark the ones that change.

To get closer to your question, I would like to expect that you don't want to introduce input processing to your window class. Rather, create another class that does the input processing (and feeds events to it if you prefer event driven) and then just let it do its job. In this setup, your window accepts events, but the only thing it does with them is pass them to a set of objects that can deal with them.



The problem with handling events on a window is that you are just one more step (or more) removed from the thing that cares about the results of handling the input. I would like the input processor and the input requestor to be closer together in the object graph, but again - the main window doesn't really care about (the) event itself, it should just pass these things to the objects that do it.

+3


a source







All Articles