Jump to content
  • Advertisement
Sign in to follow this  
schragnasher

event handling at a noob level

This topic is 3580 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!