Problem with key is pressed event

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
alfabeta90
Posts: 52
Joined: Wed Dec 06, 2006 5:18 pm

Problem with key is pressed event

Post by alfabeta90 »

How to make a key is pressed event?
I make this one but when i run the program, program crach out.

Code: Select all

IrrlichtDevice *device = 0;
s32 cnt = 0;


class MyEventReceiver : public IEventReceiver
{
public:
	virtual bool OnEvent(SEvent event)
	{
			s32 id = event.GUIEvent.Caller->getID();
			IGUIEnvironment* env = device->getGUIEnvironment();

		if (event.EventType == EET_KEY_INPUT_EVENT && event.KeyInput.Key == KEY_SPACE && event.KeyInput.PressedDown == false)
		{
			MessageBox(0, "asd", "ad", MB_OK);


		}

		return true;		
	}
};
JonLT
Posts: 152
Joined: Thu Mar 15, 2007 5:47 pm
Location: Denmark

Post by JonLT »

did you assign the event receiver to the irrlicht device?

Code: Select all

device->setEvenreceiver(&myEventReceiver);
You could also assign the event receiver when you create the irrlicht device, if i remember correctly ist the last parameter.
alfabeta90
Posts: 52
Joined: Wed Dec 06, 2006 5:18 pm

Post by alfabeta90 »

Yes
vitek
Bug Slayer
Posts: 3919
Joined: Mon Jan 16, 2006 10:52 am
Location: Corvallis, OR

Post by vitek »

I'd be willing to bet that this is the classic global variable problem.

Code: Select all

IrrlichtDevice* device = 0; // 1

// snip

class MyEventReceiver : public IEventReceiver
{
public:
  virtual bool OnEvent(SEvent event)
  {
    // snip

    IGUIEnvironment* env = device->getGUIEnvironment(); // 2

    // snip
  }
};

int main()
{
  IrrlichtDevice* device = createDevice(...); // 3

  // snip
}
The device pointer at // 3 is initialized after main starts, but this has no effect on the global one at // 1. That device pointer is set to NULL before main starts to execute. When the program gets to // 2 the global device pointer is still NULL, and a function gets invoked on a NULL pointer.

Travis
alfabeta90
Posts: 52
Joined: Wed Dec 06, 2006 5:18 pm

Post by alfabeta90 »

So, what must i do?
JonLT
Posts: 152
Joined: Thu Mar 15, 2007 5:47 pm
Location: Denmark

Post by JonLT »

you have two options: (that i can think of)
1) create the device in global scope
2) have the device as a memeber of the event receiver class
Post Reply