Page 1 of 1

Switching Weapons!

Posted: Wed Apr 19, 2006 3:05 pm
by LOL
I want to incorporate more weapons in a FPS game and have made it switch weapons from a uzi to a shotgun. But have one problem, when the switch occurs the games slows down.

I believe it is because the previous weapon mesh has not been cleared from the memory or something!?! Is there any way to delete the addAnimatedMeshSceneNode? or is there another of switching weapons?

I would appreciate any help on this issue. :wink:

thx

This is the coding so far:

Code: Select all

irr::scene::IAnimatedMesh* m_pWeaponMesh;
irr::scene::IAnimatedMeshSceneNode* m_pWeaponNode;  
     
     if(weapon==1)
     {
               
         m_pWeaponMesh = pManager->getSceneManager()->getMesh("media/uzi.3ds");
         //m_pWeaponNode = pManager->getSceneManager()->createDeleteAnimator(); 
     }
     else if(weapon==2)
     {
         m_pWeaponMesh = pManager->getSceneManager()->getMesh("media/mygun.3ds"); 
     }
     
     m_pWeaponNode = pManager->getSceneManager()->addAnimatedMeshSceneNode( m_pWeaponMesh, pManager->getSceneManager()->getActiveCamera(), -1); 
     m_pWeaponNode->setMaterialFlag(video::EMF_LIGHTING, false); 
     m_pWeaponNode->setMaterialTexture(0, pManager->getDriver()->getTexture("media/uzi.jpg")); 
     m_pWeaponNode->setScale(core::vector3df(2,2,2)); 
     m_pWeaponNode->setPosition(core::vector3df(12, -8, 25)); 
     m_pWeaponNode->setRotation(core::vector3df(-2,0,0)); 

Posted: Wed Apr 19, 2006 3:18 pm
by JP
m_pWeaponNode->drop() might get rid of it, if not m_pWeaponNode->delete()

it might be best to load all the weapons into the game first, and then just move them into/out of view when required? Would get rid of the slowing down i should think

Posted: Wed Apr 19, 2006 3:46 pm
by LOL
thx JP

the m_pWeaponNode->drop(); just makes it crash and m_pWeaponNode->delete(); doesnt work cos i think its a reserved word.

At the moment i can call up different meshes when i press a button, its just the previous mesh is still there. So the previous mesh is overlapped with the mesh of the new weapon. :?

if u see the code ive tried to use

Code: Select all

m_pWeaponNode = pManager->getSceneManager()->createDeleteAnimator(); 
but i get an error:
422 no matching function for call to 'irr::scene::ISceneManager::createDeleteAnimator()'

when i try

Code: Select all

m_pWeaponNode = pManager->getSceneManager()->createDeleteAnimator(10);
i get an error saying:
422 cannot convert `irr::scene::ISceneNodeAnimator*' to `irr::scene::IAnimatedMeshSceneNode*' in assignment

Otherwise loading all the weapons and then moving them into/out of view sounds like a good solution.thx :wink:

Posted: Wed Apr 19, 2006 4:16 pm
by sudi
do this:

if(weopon == 1)
{
if(node != 0)
{
ISceneNodeAnimator* anim = smgr->createDeleteAnimator(1);
node->addAnimator(anim);
anim->drop();
}
node = smgr->addAnimatedMeshSceneNode(/*mesh*/);
}
.
.
.
.

Posted: Wed Apr 19, 2006 6:43 pm
by Guest
Maybe you should try this?

if(weapon==1)
{
m_pWeapon1Node->setVisible(1);
m_pWeapon2Node->setVisible(0);

}
else
if(weapon==2)
{
m_pWeapon1Node->setVisible(0);
m_pWeapon2Node->setVisible(1);
}

Posted: Wed Apr 19, 2006 6:57 pm
by sudi
Yeah but it's slowler since u are having a lot more scennodes active......ok probably not the switching but the overall performance

Posted: Thu Apr 20, 2006 11:37 am
by area51
You could also look in the IrrWizard CVS repository as I've added a similar thing to what you're doing there.

The approach I used was to create weapon states, this does away with the need for ugly IF/ELSE statements, makes it more extendable, pluggable, readable and lots of other things ending in -ble.

Here is the MouseEvent function:

Code: Select all

//! Mouse event handler passed down by CGameManager
void CGamePlayState::MouseEvent(CGameManager* pManager)
{
	switch(pManager->getMouse()) 
    { 
		case EMIE_LMOUSE_PRESSED_DOWN:	// left mouse pressed, FIRE!!!
			if (!pManager->getWeaponManager()->isWeaponLocked() && pManager->
                getPlayer()->getAmmo() > 0)
		        pManager->getWeaponManager()->getActiveWeapon()->ChangeState(WeaponStateShoot::Instance()); 
			break;
		case EMIE_RMOUSE_PRESSED_DOWN:	// right mouse pressed, RELOAD!!!
			 if (pManager->getPlayer()->getAmmo() <= 0)
				pManager->getPlayer()->setAmmo(100); // 100% full     
			 break;
      case EMIE_MOUSE_WHEEL:			// change weapon 
			if (!pManager->getWeaponManager()->isWeaponLocked())
				pManager->getWeaponManager()->getActiveWeapon()->ChangeState(WeaponStateChange::Instance()); 
             break; 
	}
}

The good thing is that you can have different shooting code for each weapon, pistol, rocket launcher, flamethrower etc. Each in it's own state class. Keeps things tidy and organized.

The classes of interest are CGameWeaponManager & CGameWeaponOwnedStates. (They need to be moved out of core and ai and put somewhere else, so dont be alarmed)

All you will need to do is add these files to your project, add them to CGameManager by creating a pointer and accessor to it (see how other managers are added).

Then in the CGameStateLevel Init() somewhere:

Code: Select all

pManager->getWeaponManager()->addFPSWeapons(pManager);
Look at the CVS code as it has a full implementation of the WeaponManager.

I've been doing a Tech Demo which has it working, to see if it's the sort of thing you could use. (Be warned the demo is far from complete, but is playable)

link here : http://www.gameputty.com/LockNLoad/

Good luck, I'm affraid I wont be able to offer much support at this time, home computer has expired :(

p.s Dont worry about the performance overhead of a few extra sceneNodes either. Get the design right first, then implement it, then worry about the performance right at the end.
________
Grow Medical Marijuana