Page 1 of 1

On Event Callback not calling back!

Posted: Mon Apr 11, 2005 7:20 pm
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;
    
}

Posted: Mon Apr 11, 2005 7:49 pm
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.

Posted: Mon Apr 11, 2005 8:33 pm
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?

Posted: Mon Apr 11, 2005 10:31 pm
by jox
Did you pass the event receiver to your device?

Posted: Tue Apr 12, 2005 3:14 pm
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?

Posted: Tue Apr 12, 2005 4:42 pm
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);

Posted: Tue Apr 12, 2005 4:57 pm
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