On Event Callback not calling back!

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
steveybop
Posts: 30
Joined: Wed Mar 02, 2005 2:52 pm

On Event Callback not calling back!

Post by steveybop »

I have tried to implement an event reciever in my program, i used a lot of the code form the Mercior Irrlicht/Newton tutorial. But when i click the mouse or hit escape nothing happens, put in breakpoints and execution never reaches callback. Any ideas?

here is the class definition

Code: Select all

class CGame  : public IEventReceiver { 
Here is the callback prototype

Code: Select all

// Irrlicht Callbacks
	virtual bool OnEvent(SEvent event);
Here is the callback definition

Code: Select all

bool CGame::OnEvent(SEvent event)
{
     printf("Newt~licht Event Recieved...");
     if (event.EventType == irr::EET_MOUSE_INPUT_EVENT && event.MouseInput.Event == EMIE_LMOUSE_PRESSED_DOWN)
    {
		// make a cube where the camera is and set its velocity to follow the target
		NewtonModel *tmp = MakeModel(cam->getPosition()/*, veloc*/);
		if (!tmp) return false;
		vector3df camvec = (cam->getTarget() - cam->getPosition()).normalize() * 500;
		float newpos[3] = { camvec.X, camvec.Y, camvec.Z };
		NewtonBodySetVelocity(tmp->body1, (float*)newpos);
	}
	else if(event.EventType == EET_GUI_EVENT)
	{
         //here put GUI STUFF     
    }
    else if( event.EventType == EET_KEY_INPUT_EVENT)
    {
        bool key[KEY_KEY_CODES_COUNT];
        key[event.KeyInput.Key] = event.KeyInput.PressedDown;
        if(key[KEY_ESCAPE])
        {
               NewtonReleaseCollision(nWorld, floor->collision); 
               NewtonDestroy(nWorld);  
	           device->drop();
        }
    }

	return false;
    
}
DISCLAIMER: Having read this post & all information therein,you have entered into a legally binding contract.You are obliged to agree wholeheartedly with all opinions expressed within this post.Failure to comply will be deemed a breach of contract.
Spintz
Posts: 1688
Joined: Thu Nov 04, 2004 3:25 pm

Post by Spintz »

return true if you handle the event, return false if you don't.

For example, if you're using an FPS camera, and also trying to get input from other keys. You need to return false from your event receiver's OnEvent function if it gets the KEY_UP, KEY_DOWN, KEY_LEFT, KEY_RIGHT. If you return true, it means you're handling the event and noone else should.
Image
steveybop
Posts: 30
Joined: Wed Mar 02, 2005 2:52 pm

Post by steveybop »

ok so it should only return false at end if none of the relevent events happened, right?

So when should i return true?

Like this:

Code: Select all

bool CGame::OnEvent(SEvent event)
{
     
     if (event.EventType == irr::EET_MOUSE_INPUT_EVENT && event.MouseInput.Event == EMIE_LMOUSE_PRESSED_DOWN)
    {
      // make a cube where the camera is and set its velocity to follow the target
      NewtonModel *tmp = MakeModel(cam->getPosition()/*, veloc*/);
      if (!tmp) return false;
      vector3df camvec = (cam->getTarget() - cam->getPosition()).normalize() * 500;
      float newpos[3] = { camvec.X, camvec.Y, camvec.Z };
      NewtonBodySetVelocity(tmp->body1, (float*)newpos);
      
      return true;   ///// IS THIS WHERE IT SOULD GO?

   } 
At the end of each reletive section, check with if statement, if valid do lines of code, return true. That how it goes?
DISCLAIMER: Having read this post & all information therein,you have entered into a legally binding contract.You are obliged to agree wholeheartedly with all opinions expressed within this post.Failure to comply will be deemed a breach of contract.
jox
Bug Slayer
Posts: 726
Joined: Thu Apr 22, 2004 6:55 pm
Location: Germany

Post by jox »

Did you pass the event receiver to your device?
It is like it is. And because it is like it is, things are like they are.
steveybop
Posts: 30
Joined: Wed Mar 02, 2005 2:52 pm

Post by steveybop »

I dont think i did:

Code: Select all

device = createDevice(EDT_OPENGL, core::dimension2d<s32>(640, 480), 16, false,false,false);
After checking the documentation, i think it should be this, no?

Code: Select all

device = createDevice(EDT_OPENGL, core::dimension2d<s32>(640, 480), 16, false,false,false,OnEvent);
Do i put in the name of the event reciever or just specify that there is one and the program will pick up on it??

once again though, can anyone tell me WHERE i return true within my event reciever?
DISCLAIMER: Having read this post & all information therein,you have entered into a legally binding contract.You are obliged to agree wholeheartedly with all opinions expressed within this post.Failure to comply will be deemed a breach of contract.
jox
Bug Slayer
Posts: 726
Joined: Thu Apr 22, 2004 6:55 pm
Location: Germany

Post by jox »

You do either:

Code: Select all

EventReceiverClass receiver;
device = createDevice(EDT_OPENGL, core::dimension2d<s32>(640, 480), 16, false,false,false,&receiver);
Or if you call createDevice from within a class that derives from IEventReceiver (e.g. your CGame class):

Code: Select all

device = createDevice(EDT_OPENGL, core::dimension2d<s32>(640, 480), 16, false,false,false,this);
It is like it is. And because it is like it is, things are like they are.
steveybop
Posts: 30
Joined: Wed Mar 02, 2005 2:52 pm

Post by steveybop »

Thanks a million jox, you have now achieved guru status in my books. Simple problems are always the hardest to solve without experts around!
Completely forgot the event reciever was even dealt with in createDevice. Threw in a 'this' at the end and everythin was hunky-dorey!!!! I now have poop flying out of my camera!!!!!! :D
DISCLAIMER: Having read this post & all information therein,you have entered into a legally binding contract.You are obliged to agree wholeheartedly with all opinions expressed within this post.Failure to comply will be deemed a breach of contract.
Post Reply