Shooting Question

You are an experienced programmer and have a problem with the engine, shaders, or advanced effects? Here you'll get answers.
No questions about C++ programming or topics which are answered in the tutorials!
Robomaniac
Posts: 602
Joined: Sat Aug 23, 2003 2:03 am
Location: Pottstown, PA
Contact:

Shooting Question

Post 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
[dx/x]=HUNT3R
Posts: 271
Joined: Sat Aug 23, 2003 5:52 pm
Location: Hurricane Central, Florida

Post 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...
Robomaniac
Posts: 602
Joined: Sat Aug 23, 2003 2:03 am
Location: Pottstown, PA
Contact:

Post 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
[dx/x]=HUNT3R
Posts: 271
Joined: Sat Aug 23, 2003 5:52 pm
Location: Hurricane Central, Florida

Post 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;
Robomaniac
Posts: 602
Joined: Sat Aug 23, 2003 2:03 am
Location: Pottstown, PA
Contact:

Post 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
[dx/x]=HUNT3R
Posts: 271
Joined: Sat Aug 23, 2003 5:52 pm
Location: Hurricane Central, Florida

Post 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
Robomaniac
Posts: 602
Joined: Sat Aug 23, 2003 2:03 am
Location: Pottstown, PA
Contact:

Post by Robomaniac »

still doesn't work. I found that it's not reading the mouse events for some reason
Guest

Post 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)?
Spartacus
Posts: 70
Joined: Fri Nov 21, 2003 11:56 pm

Post 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
Robomaniac
Posts: 602
Joined: Sat Aug 23, 2003 2:03 am
Location: Pottstown, PA
Contact:

Post 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
FleshCrawler
Posts: 108
Joined: Fri Aug 22, 2003 1:04 pm
Location: Kerkrade, Netherlands
Contact:

Post 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 
I've been absent for really long, but i'm ready to reign my terror on you once again, mwuahahahahaha
[dx/x]=HUNT3R
Posts: 271
Joined: Sat Aug 23, 2003 5:52 pm
Location: Hurricane Central, Florida

Post 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);
 
Robomaniac
Posts: 602
Joined: Sat Aug 23, 2003 2:03 am
Location: Pottstown, PA
Contact:

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

Post 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
SuryIIID
Posts: 14
Joined: Mon Dec 29, 2003 8:54 pm
Location: Bulgaria..?

Shooting...

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