Problem with the events

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.
The BasheR
Posts: 73
Joined: Thu Apr 05, 2007 7:01 pm
Location: France
Contact:

Problem with the events

Post 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.
When you want you can!
Quand on veut on peut!
The BasheR
Posts: 73
Joined: Thu Apr 05, 2007 7:01 pm
Location: France
Contact:

Post 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.
When you want you can!
Quand on veut on peut!
roxaz
Posts: 575
Joined: Tue Jan 23, 2007 8:35 pm
Location: LT

Post 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
The BasheR
Posts: 73
Joined: Thu Apr 05, 2007 7:01 pm
Location: France
Contact:

Post by The BasheR »

Ok thank you but can you tell me more about the second way?
When you want you can!
Quand on veut on peut!
Tanuva
Posts: 54
Joined: Tue Oct 10, 2006 6:49 pm
Location: 200 metres behind the moon
Contact:

Post 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.
Tanuva
roxaz
Posts: 575
Joined: Tue Jan 23, 2007 8:35 pm
Location: LT

Post 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.
Perceval
Posts: 158
Joined: Tue May 30, 2006 2:42 pm

Post by Perceval »

Yes, and don't forget to return true in the if statement.
The BasheR
Posts: 73
Joined: Thu Apr 05, 2007 7:01 pm
Location: France
Contact:

Post 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
When you want you can!
Quand on veut on peut!
The BasheR
Posts: 73
Joined: Thu Apr 05, 2007 7:01 pm
Location: France
Contact:

Post 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.
When you want you can!
Quand on veut on peut!
Perceval
Posts: 158
Joined: Tue May 30, 2006 2:42 pm

Post 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.
Luben
Posts: 568
Joined: Sun Oct 09, 2005 10:12 am
Location: #irrlicht @freenode

Post 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.
If you don't have anything nice to say, don't say anything at all.
The BasheR
Posts: 73
Joined: Thu Apr 05, 2007 7:01 pm
Location: France
Contact:

Post by The BasheR »

Thank you but how can i use the eventReceiver implemented into the ICameraSceneNode class?
When you want you can!
Quand on veut on peut!
roxaz
Posts: 575
Joined: Tue Jan 23, 2007 8:35 pm
Location: LT

Post 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 ;)
The BasheR
Posts: 73
Joined: Thu Apr 05, 2007 7:01 pm
Location: France
Contact:

Post 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
When you want you can!
Quand on veut on peut!
vitek
Bug Slayer
Posts: 3919
Joined: Mon Jan 16, 2006 10:52 am
Location: Corvallis, OR

Post 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
Post Reply