help understanding IEventReceiver

If you are a new Irrlicht Engine user, and have a newbie-question, this is the forum for you. You may also post general programming questions here.
Post Reply
Stauricus
Posts: 20
Joined: Mon Jun 25, 2012 10:12 pm
Location: Uberlandia, Brazil

help understanding IEventReceiver

Post by Stauricus »

hello, everybody!
i'm having a little trouble understanding how to get UserInput on Irrlicht.

considering this segment:

Code: Select all

class MyEventReceiver : public IEventReceiver
{
public:
    // This is the one method that we have to implement
    virtual bool OnEvent(const SEvent& event)
    {
        // Remember whether each key is down or up
        if (event.EventType == irr::EET_KEY_INPUT_EVENT)
            KeyIsDown[event.KeyInput.Key] = event.KeyInput.PressedDown;
 
        return false;
    }
 
    // This is used to check whether a key is being held down
    virtual bool IsKeyDown(EKEY_CODE keyCode) const
    {
        return KeyIsDown[keyCode];
    }
    
    MyEventReceiver()
    {
        for (u32 i=0; i<KEY_KEY_CODES_COUNT; ++i)
            KeyIsDown[i] = false;
    }
 
private:
    // We use this array to store the current state of each key
    bool KeyIsDown[KEY_KEY_CODES_COUNT];
};
taken from the 4th tutorial (http://irrlicht.sourceforge.net/docu/example004.html), i'd like some help understanding what's happening.

1- so IEventReceiver is just a class, right? so, i should create an object from it, which will receive instructions from Irrlicht when something happens? and then i just check the object every time i want to know if some user input happened?
2-why, in this example, a derivated class was created? is it just to know if a key is being held down? can't Irrlicht check that by itself?
3-thanks in advance?

thanks in advance :mrgreen:
polylux
Posts: 267
Joined: Thu Aug 27, 2009 12:39 pm
Location: EU

Re: help understanding IEventReceiver

Post by polylux »

It is an abstract class which means you cannot instantiate it. You'd rather write your own class wich derives from IEventReceiver, like this:

Code: Select all

class MyClass : public irr::IEventReceiver
Through the layout of IEventReceiver, you'd have to implement the functionality for the method

Code: Select all

bool OnEvent(const irr::SEvent&)
(it's pure virtual) where you'd do your stuff based on the event (and its type) passed-in.
Finally you register your class to receive events. Do this with:

Code: Select all

//assuming 'dev' is your instance of the IrrlichtDevice (pointer) and 'myc' is your instance of MyClass (pointer)
dev->setEventReceiver(myc);
Hope that helps.
p.
beer->setMotivationCallback(this);
Stauricus
Posts: 20
Joined: Mon Jun 25, 2012 10:12 pm
Location: Uberlandia, Brazil

Re: help understanding IEventReceiver

Post by Stauricus »

sure it helps! i would try to instantiate the class itself if you haven't said that.
now i understand why all tutorials create these derivated classes.

thanks for clarifying it :)
polylux
Posts: 267
Joined: Thu Aug 27, 2009 12:39 pm
Location: EU

Re: help understanding IEventReceiver

Post by polylux »

Cheers!
beer->setMotivationCallback(this);
Post Reply