I created a button class which draw a button on the screen. When I click on it, I want to see something happening. In WinForm, I would simply use the event OnClick of the button. What about XNA?

Should I add an OnClick event to my button class? Is that a good practice? Otherwise, how do you handle events in your game loop?

  • \$\begingroup\$ You're going to have to write your own onClick event. Something that takes the current mouse position and matches that against where your button is \$\endgroup\$
    – Jeff
    Commented Jun 10, 2011 at 1:34

4 Answers 4


The only reason against using event in a game is that creating a delegate to attach to the event handler creates a heap object that can cause a garbage collection which can cause a frame-rate hiccup on on Xbox 360 (and possibly WP7, haven't tested it).

In general, this should not be relevant to a game UI that you set-up once and simply let run.

Also, calling an event handler is a tiny, tiny, tiny bit slower some other available methods. And this is also absolutely irrelevant for a UI. (It only comes into play for micro-optimised number-crunching).

So, as long as you're not running around assigning event handlers all willy-nilly, then the choice of using an event in a game is no different to the choice of using one in a regular application.

Copying the design of WinForms for your game UI is perfectly fine.

(It's worth pointing out one caveat of events is that they are "hidden" strong references that can unintentionally keep objects alive if you don't remove the handler. This is relevant to both games and regular applications)


It's perfectly acceptable to use events to handle things like clicks and other interaction with your button interface; it's certainly a superior method to the one that involves subclassing the Button type to implement customized behavior.

Another uncommon alternative is to attach scripts to buttons and other UI elements, but this is more involved (unless you already have a robust scripting system in place) and not neccessarily better anyhow.

You may also want to look into the concept of "immediate mode GUIs," which provide for another way of handling input.

  • \$\begingroup\$ I'm just wondering how is your answer diffrent from mine? is there any method that differ between us? \$\endgroup\$
    – Ali1S232
    Commented Jun 10, 2011 at 8:28

There are three common approches I saw in game engines, I'm not sure about XNA supporting all three but here they are :

  1. you can inherite a class from your button class and override OnClickFunction in your CustomButton class. The benefit is you don't have to check if the button is pressed it informed you whenever it's clicked but it may cause to overuse of inheritence.

  2. you can define some function OnClick and pass it to OnClickEvent butten class (just like normal c# event handleing). this has the same benefit as the previous one and you don't need to worry for inheritence overuse.

  3. you have to check in your main loop if this button is clicked or not. in this method it's not realy event. so the disadvantage is you have to check yourself whenever you have to do somthing based on button inputs but it has a advantage that you can control where the buttons really are checked and take effect.


I stopped using .net events and started to store all my events and changes in buffer:

public sealed class GameStateData
    public bool IsEmpty = true;
    public List<EventData> EventDatas = new List<EventData>();
    public List<ChangeData> ChangeDatas = new List<ChangeData>();
    ...//your additional data

    public void Reset()
        IsEmpty = true;
        ManifoldDatas.Count = 0;
        ChangeDatas.Count = 0;

public struct ChangeData
    public Node Sender;
    public short PropertyIndex; // predefined property index (f.e. (short)MyIndexEnum.Life)
    public object OldValue;
    public object NewValue;
    public object AdditionalData;


  1. For multithreading support the only thing you need is to swap buffers and process all events and changes in render thread.
  2. Every gamestep change will be saved, so any game logic can be based on combination of events. You can even implement time reversing.
  3. You don't need to create event fields increasing size of single object. Game logic in most cases require only single rare subscriber.
  4. There is almost no additional memory allocation (excluding boxing cases) (WP7,XBox and other micro framework critical).
  5. You can have different event sets handling a predefined GameStateData to object.


  1. More memory spending, although you need only 3 buffers (game, swap and render).
  2. Writing additional code at start to implement needed functionality.

BTW sometimes I use both: event model and buffer model.


You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .