Page 1 of 2

Problem with the events

Posted: Mon Apr 30, 2007 5:57 pm
by The BasheR
Hello all, for a little game, i would like to create a camera like in RTS games. I've made the first part (with the mouse), and now i would like when i press a key that the camera moves.
For my camera i have a class, and i do not know how i can do to use this class in the member OnEvent of the class MyEventReceiver.

Can you help me?

Thanks a lot.

Posted: Tue May 01, 2007 8:22 am
by The BasheR
Nobody knows how i can use this object (the camera), in the member OnEvent? Because i think we can't add a new argument in this member (there is only one who is SEvent), but i may be wrong.

Posted: Tue May 01, 2007 9:18 am
by roxaz
to capture events you create your own event receiver. now there are two ways of moving camera. fist way: you can pass camera pointer to your event receiver and there do all stuff you need. second way: you can pass something like keysDown[] array to cam and if specific key is down - do stuff

Posted: Tue May 01, 2007 9:23 am
by The BasheR
Ok thank you but can you tell me more about the second way?

Posted: Tue May 01, 2007 10:57 am
by Tanuva
You create an array for the keys you want the camera to be controlled with. Your EventReceiver stores the pressed key(s) in the array and passes it to the camera then. Now the camera can move itself according to the pressed keys.

Posted: Tue May 01, 2007 11:34 am
by roxaz
My event receiver using keysDown[]:

Code: Select all

Controller::Controller()
{
	u32 k = 0; 
	for (k = 0; k < KEY_KEY_CODES_COUNT; ++k) 
		keysDown[k] = false;

	printf("Controller created\n");
}

bool Controller::OnEvent(SEvent event)
{
	if(event.EventType == EET_KEY_INPUT_EVENT) 
	{
		keysDown[event.KeyInput.Key] = event.KeyInput.PressedDown;
	}
	else
		return false;
}
As you see constructor assigns default false value to all keysDown[] array members and then OnEvent() sets specific member to true or false when it is pressed down or not pressed down.

Posted: Tue May 01, 2007 11:58 am
by Perceval
Yes, and don't forget to return true in the if statement.

Posted: Tue May 01, 2007 5:26 pm
by The BasheR
Ok, but then how can i apply this code for my camera? (i'm sorry but i'm really a newbie with Irrlicht). In fact would you can show me a code who does this: the full class MyEventReceiver (that you've called Controller isn't it?), and then a code who shows how to use this for the camera?

Thanks a lot

Posted: Wed May 02, 2007 11:59 am
by The BasheR
Actualy i would like to know if i can do this in a function in an other file:

Code: Select all

if(keysDown[KEY_KEY_I])
{
   /* My camera's action */
}
This code wouldn't be in the Controller class.

Posted: Wed May 02, 2007 12:12 pm
by Perceval
When you want you can!
Quand on veut on peut!
You can :lol: !
Of course, make sure your function is called in the main loop.

Posted: Wed May 02, 2007 12:24 pm
by Luben
From ICameraSceneNode.h

Code: Select all

		//! It is possible to send mouse and key events to the camera.
		/** Most cameras
		may ignore this input, but camera scene nodes which are created for 
		example with ISceneManager::addMayaCameraSceneNode or
		ISceneManager::addMeshViewerCameraSceneNode, may want to get this input
		for changing their position, look at target or whatever.  */
		virtual bool OnEvent(SEvent event) = 0;
Basically, the active camera node gets input on it's own, in the same way as a user created event receiver, so you don't need to catch the input in the event receiver and pass it to the cam, it's done automatically.

Posted: Wed May 02, 2007 12:44 pm
by The BasheR
Thank you but how can i use the eventReceiver implemented into the ICameraSceneNode class?

Posted: Wed May 02, 2007 1:15 pm
by roxaz
you have to create bool OnEvent(SEvent event); method in your camera then. it should be very similar to that i wrote you before.

Code: Select all

bool YourCamClass::OnEvent(SEvent event) 
{ 
   if(event.EventType == EET_KEY_INPUT_EVENT) //Checks if keyboard key is pressed down
   { 
      if(event.KeyInput.Key == KEY_KEY_I)
      {
         // do your stuff
         return true;
      }
   } 
   else 
      return false; 
}
Its not hard to answer to you but i suggest you firstly take a deep look at tutorials and never ask information that you can find yourself. I know its hard in the beginning but now you are asking us tu write for you what you need. Its not good. you will never learn ;)

Posted: Wed May 02, 2007 1:50 pm
by The BasheR
roxaz wrote:Its not hard to answer to you but i suggest you firstly take a deep look at tutorials and never ask information that you can find yourself. I know its hard in the beginning but now you are asking us tu write for you what you need. Its not good. you will never learn ;)
Yes i know and i'm sorry, i search but i don't find, i don't think yet Irrlicht ^^

I've understood your code, and i've tried it, but that does not work :(
This is my code (just the importants parts):

Code: Select all

/* The declaration of my class */
class RTSCamera
{
private:
   /* Caméra */
   irr::scene::ICameraSceneNode *mp_camera;
   /* Variables pour l'environnement */
   irr::video::IVideoDriver* mp_driver;
   irr::scene::ISceneManager* mp_smgr;
   irr::gui::IGUIEnvironment* mp_env;
   /* Variable contenant l'état des touches du clavier */
   bool m_keysDown[irr::KEY_KEY_CODES_COUNT];

public:
   /* Fonction pour la gestion des évennements */
   bool OnEvent(irr::SEvent event);
   /* Constructeur */
   RTSCamera(irr::video::IVideoDriver* driver, irr::scene::ISceneManager* smgr,
      irr::gui::IGUIEnvironment* env);
   /* Constructeur de copie */
   RTSCamera(const RTSCamera &Camera);
   /* Destructeur */
   ~RTSCamera(void);
};




/* The code of the events */
/* Fonction pour la gestion des évennements */
bool RTSCamera::OnEvent(SEvent event)
{
   std::cout << "ok" << std::endl;
   /* On regarde si il y a un évennement venant d'une touche */
   if(event.EventType == EET_KEY_INPUT_EVENT)
   {
      m_keysDown[event.KeyInput.Key] = event.KeyInput.PressedDown;
      return true;
   }
   else
   {
      return false;
   }
}
And the problem is that the word "ok" is never displayed on the console, so the function is never executed, but why and how to execute it?

Thanks a lot

Posted: Wed May 02, 2007 2:33 pm
by vitek
You need to hook your camera helper into the event system somehow. You can do this a few different ways.
  1. you could make RTSCamera inherit from IEventReceiver and pass a pointer to your rts camera to the device method setEventReceiver.
  2. you could make RTSCamera inherit from ICameraSceneNode and you could make your camera delegate method calls to the camera that you keep a pointer to. You would have to tell the scene manager that your camera is the active camera by calling setActiveCamera.
  3. you could use your own external event receiver [see tutorials] that forwards key press events to your camera.
  4. you could write an event receiver that just sets the key states [pressed or not] and then call methods on the RTSCamera to tell it what to do based on the pressed keys.
Travis