event handling at a noob level

Started by
3 comments, last by Kylotan 15 years, 2 months ago
Im looking for a good tutorial that spells things out fairly plainly with regards to event handling. I need to start talking to my objects, but i cant get it all to connect in my head. I can hit the spacebar and create an event in my event list, but how do i let my grid know the spacebar was hit? Currently my gamestate is setup as such

class CGameState
{
    public:
        virtual ~CGameState()=0;
        virtual void Init()=0;
        virtual void Draw()=0;
        virtual void Update()=0;
        virtual void HandleEvents()=0;
        virtual void Cleanup()=0;
        virtual void Pause()=0;
        virtual void Resume()=0;

        void LoadEntity(CEntity* entity);


    protected:
        std::vector<CEntity*> m_entitylist;
        CGameState();
};

it has a list of entities, but no way to talk to a specific one. So i need the event handler to send the event to the object right? Do i register every single entity to receive every single event? Im not sure if i'm handling entities correctly in the first place, keeping a list in the gamestate. Any tips about how to structure this kind of stuff would be appreciated. i guess iv got an event handler, a gamestate and a bunch of entities, but im not sure how to glue them together.
Advertisement
The information you've provided is somewhat vague, but from what I understand a simple solution would be to have each entity contain a list of events that it listens to. When the state detects that an event has been fired, to dispatch the event to the appropriate entities it just looks through the entity list, checking each to see if it listens for this particular message.

However, the problem of event handling is a very common one that is encountered in many projects, and as such some generic solutions exist that abstract away the guts of this process. One example is the Boost.Signals library, which provides a signal-slot framework.

Switching to a third-party event handling library may require you to refactor your code somewhat, but I think it's worth consideration unless there's some particular reason that you need to roll your own.
Ok iv got some signal/slot stuff working using the sigslot library.

http://sigslot.sourceforge.net/#docs

its exactly what i wanted , nice simple and low rent. My question is...

Im currently using the setup from the gamedevgeek website for my game loop, it has a function HandleEvents() that runs each frame, when not using and event handler with signals this makes sense, but now that my events are all handled asynchronously outside of any game loop, can i lose this call? Im thinking it will become useless. So my engine would simply, update and draw, and all the events will happen immediately when they occur.
actually im beginning to this using my event synchronously is a bad idea. Is it wiser to have my objects submit events to a que then have the event handler emit them in order?
That depends on what you want to do with these events, which you've been vague about. Most people don't need signals, slots, or message queues at all.

This topic is closed to new replies.

Advertisement