Page 1 of 2

Shooting Question

Posted: Sat Nov 29, 2003 3:12 pm
by Robomaniac
I'm having trouble with implementing shooting into my program. I'm using the code from the tech demo. I typed it in pretty much exactly, except I have two event receivers MyEventReceiver, and MyGameEventReceiver. The event check for the mouse button is in MyGameEventReceiver, but it doesn't run. I click the mouse button, and nothing happens. I debugged it, and it's not saying that the mouse is being pressed. Here is my code for the event receiver

Code: Select all

class MyGameEventReceiver : public IEventReceiver
{
public:
   virtual bool OnEvent(SEvent event)
   {
      if (camera)
			return camera->OnEvent(event);  
			
      if (event.EventType == EET_MOUSE_INPUT_EVENT && event.MouseInput.Event == EMIE_LMOUSE_LEFT_UP)
      {
         printf("shoot");
         shoot();	
      }      
	}
};
thanks for your help

--The Robomanaic

Posted: Sat Nov 29, 2003 5:12 pm
by [dx/x]=HUNT3R
You can only have one eventReceiver. When you create the irrlicht graphics device you add the event receiver to it as a parameter. There is not a parameter for a second event receiver...

Posted: Sat Nov 29, 2003 5:51 pm
by Robomaniac
ok, when i have it in one receiver though, i have a variable, game, to show whether it's the gui or the game, but it doesn't work, whenever click a button, it crashes.

--The Robomanica

Posted: Sat Nov 29, 2003 8:11 pm
by [dx/x]=HUNT3R
If you are going to do it that way then make game states to define which state the game is in, for example:

Code: Select all

// GLOBAL DEFINES /////
#define GAME_STATE_MENU    1
#define GAME_STATE_RUNNING 2

// GLOBAL VARIABLE /////
s32 gameState;

// EVENT RECEIVER /////
class MyGameEventReceiver : public IEventReceiver 
{ 
public: 
   virtual bool OnEvent(SEvent event) 
   { 
      if (camera) 
         return camera->OnEvent(event); // dont need this with v0.4.1
          
      if (event.EventType == EET_MOUSE_INPUT_EVENT &&
         event.MouseInput.Event == EMIE_LMOUSE_LEFT_UP && 
         gameState == GAME_STATE_RUNNING) // just add in a check to the gameState
      { 
         printf("shoot"); // no need for this, where's it gonna print? 
         shoot();    
      }      
   } 
}; 
Of course you'll need to set gameState to the correct state when appropriate too. For example if someone presses the New Game button

Code: Select all

gameState = GAME_STATE_RUNNING;

Posted: Sat Nov 29, 2003 8:26 pm
by Robomaniac
It still doesn't work though. I still get no response from the program.

BTW : the printf goes in the console for debugging purposes. It isn't printing either.

--The Robomaniac

Posted: Sat Nov 29, 2003 8:30 pm
by [dx/x]=HUNT3R
Try this instead, it works for me.

Code: Select all

if (event.MouseInput.Event == EMIE_LMOUSE_PRESSED_DOWN && gameState == GAME_STATE_RUN_GAME){

     Shoot();
     return true;

} // end if

Posted: Sat Nov 29, 2003 8:32 pm
by Robomaniac
still doesn't work. I found that it's not reading the mouse events for some reason

Posted: Sun Nov 30, 2003 12:15 am
by Guest
What does your code look like for creating the irrlicht device? Does your receiver object exist throughout the game loop (ie is it on the heap or is it created in a method which has/calls the game loop)?

Posted: Sun Nov 30, 2003 12:19 am
by Spartacus
i got a extra question u could add on how do u determin if the bullet hits another mesh eg. I shoot at a fairy she dies

Posted: Sun Nov 30, 2003 1:40 am
by Robomaniac
I do the exact same way of using the receiver as in the tutorials. I don't have any calls in the game loop itself.

--The Robomaniac

Posted: Sun Nov 30, 2003 7:09 am
by FleshCrawler

Code: Select all

if (camera) 
         return camera->OnEvent(event); // dont need this with v0.4.1 
          
      if (event.EventType == EET_MOUSE_INPUT_EVENT && 
         event.MouseInput.Event == EMIE_LMOUSE_LEFT_UP && 
         gameState == GAME_STATE_RUNNING) // just add in a check to the gameState 
Doesn't this have to be like:

Code: Select all

      if (camera) 
         return camera->OnEvent(event); // dont need this with v0.4.1 
          
   else // added the else here.
      if (event.EventType == EET_MOUSE_INPUT_EVENT && 
         event.MouseInput.Event == EMIE_LMOUSE_LEFT_UP && 
         gameState == GAME_STATE_RUNNING) // just add in a check to the gameState 

Posted: Sun Nov 30, 2003 3:18 pm
by [dx/x]=HUNT3R
No, you probably shouldn't use an else.

I think what the guest was asking is do you create the Irrlicht device in your main function and is the event receiver also declared globally and initialized in that main function? You should have something like:

Code: Select all

// GLOBAL /////
IrrlichtDevice* device = 0;

// MAIN OR WINMAIN /////
MyGameEventReceiver myGameEventReceiver;
..
..
device = createDevice(video::DT_DIRECTX8, core::dimension2d<s32>(1024, 768), 32, true, true, &myGameEventReceiver);
 

Posted: Sun Nov 30, 2003 4:42 pm
by Robomaniac
Yeah, that's what i have. My driver declaration is global, and my receiver is after all functions but the main one. In the main loop, i have MyGameEventReceiver receiver, and the same code to declare the device.

--The Robomaniac

Posted: Thu Dec 11, 2003 9:30 pm
by Guest
Sorry for the double post. That said ...

I got the shooting to work, i just had to take out the camera event receiver lines. Now, i can't seem to get the collision to work. Right now, i only want to determine collision between it and the map. Thanks in advance!

--The Robomanic

Shooting...

Posted: Fri Jan 02, 2004 5:59 pm
by SuryIIID
Well i'm working on a FPS myself so i implemented a basic continuously shooting system - when the left mouse button is pressed my gun keeps shoting plasma balls , when the right button is pressed the camera zooms giving a sniper view just like in Quake III :D
btw i'm using the Techdemo code for my base

so here is the code for my OnEvent function

Code: Select all

bool CDemo::OnEvent(SEvent event)
{
	if (event.EventType == EET_KEY_INPUT_EVENT &&
		event.KeyInput.Key == KEY_ESCAPE &&
		event.KeyInput.PressedDown == false)
	{
		// user wants to quit.
		device->closeDevice();
	}
	else
	if ((event.EventType == EET_KEY_INPUT_EVENT &&
		event.KeyInput.Key == KEY_SPACE &&
		event.KeyInput.PressedDown == false) ||
		(event.EventType == EET_MOUSE_INPUT_EVENT &&
		event.MouseInput.Event == EMIE_LMOUSE_PRESSED_DOWN))
	{
		
	    shootPlasma = true;
		//This calls the shoot function from the main loop

	}
	
	else
	if (event.EventType == EET_MOUSE_INPUT_EVENT &&
		event.MouseInput.Event == EMIE_LMOUSE_LEFT_UP) 
		
	{
	
	shootPlasma = false;
	
	}
	
	else
	if (event.EventType == EET_MOUSE_INPUT_EVENT &&
		event.MouseInput.Event == EMIE_RMOUSE_PRESSED_DOWN) 
		
	{
	
	device->getSceneManager()->getActiveCamera()->setFOV(1.8f);
	
	}
	
	else
	if (event.EventType == EET_MOUSE_INPUT_EVENT &&
		event.MouseInput.Event == EMIE_RMOUSE_LEFT_UP) 
		
	{
	
	device->getSceneManager()->getActiveCamera()->setFOV(0.8f);
		
	}
		
	else
	if (device->getSceneManager()->getActiveCamera())
	{
		device->getSceneManager()->getActiveCamera()->OnEvent(event);
		return true;
	}

	return false;
}

and here is what my Run function looks like

Code: Select all

while(device->run() && driver)
{
if (device->isWindowActive())
{
// load next scene if necessary
u32 now = device->getTimer()->getTime();
if (now - sceneStartTime > timeForThisScene && timeForThisScene!=-1)
switchToNextScene();
           
if (shootPlasma == true && GetTickCount() - LastTick > 100)
		
{
   shoot();
   LastTick = GetTickCount();
}  

createParticleImpacts();
         
// draw everything

driver->beginScene(true, true, backColor);
smgr->drawAll();
guienv->drawAll();

driver->endScene();

// write statistics

swprintf(tmp, 255, L"%s fps:%d polys:%d", driver->getName(),
driver->getFPS(), driver->getPrimitiveCountDrawn());

statusText->setText(tmp);
       }
  }
 device->drop();
 }
Hope this will help...

Edit :
Ohh i forgot to mention these new variables , they are declared in the CDemo.h in the private section

bool shootPlasma ;
DWORD LastTick ;