Game Development Community

GetMuzzleVector() crashes TGE 1.4.2 when no weapon is mounted

by Bryce · in General Discussion · 08/01/2009 (1:45 pm) · 5 replies

Hello,

I've finally narrowed down the odd problem I've encountered. I created a sprint function for the player that unmounts his weapon while he runs about.
The problem is that when AI characters target the player, they call getMuzzleVector() somewhere in their scripts, which crashes the game because I have no weapon.

Furthermore, entering this in the console crashes the game:

LocalClientConnection.player.unMountImage(0); echo(LocalClientConnection.player.getMuzzleVector(0));

Is there anything I can do to prevent this crash? Maybe check to see if there is an image mounted in the specified slot, and if there is none, return the eye vector instead?

Thanks!

#1
08/01/2009 (4:21 pm)
you have the answer: check to see if there is an image mounted in the specified slot, and if there is none, return the eye vector instead...

search docs for isMountedImage() or similar function...
#2
08/02/2009 (11:08 am)
Just like you described
if (%player.getMountedImage($WeaponSlot) == 0 || %player.getMountedImage($WeaponSlot) == "")
   %vec = %player.getEyeVector
else
   %vec = %player.getMuzzleVector

... or something to that effect.

#3
08/02/2009 (12:45 pm)
That was my first plan, but I was wondering how to modify the engine code so that getMuzzleVector() will automatically do that check?
#4
08/03/2009 (8:11 am)

Something along the lines (blind hack):

int slot = dAtoi(argv[2]);
   if (slot >= 0 && slot < ShapeBase::MaxMountedImages) {
      if( object->getMountedImage( slot ) )
      {
         VectorF v;
         object->getMuzzleVector(slot,&v);
         char* buff = Con::getReturnBuffer(100);
         dSprintf(buff,100,"%g %g %g",v.x,v.y,v.z);
         return buff;
      }
      else
      {
         MatrixF mat;
         object->getEyeTransform(&mat);
         VectorF v2;
         mat.getColumn(1,&v2);
         char* buff = Con::getReturnBuffer(100);
         dSprintf(buff, 100,"%g %g %g",v2.x,v2.y,v2.z);
         return buff;
      }
   }
   return "0 1 0";

That wouldn't be very clean, though. getMuzzleVector's express purpose is to get the muzzle vector of a mounted image, so modifying it like this means uglifying it.
#5
08/03/2009 (8:31 am)
True...Alright, I'll make a separate getMuzzleVector function in script (Maybe AIPlayer::getMuzzleVector), and do the mounted image check there.