Game Development Community

Minor bug in FlyingVehicle network code

by Scott Richards · in Torque Game Engine · 09/10/2005 (2:31 am) · 1 replies

The current code:

U32 FlyingVehicle::packUpdate(NetConnection *con, U32 mask, BitStream *stream)
{
   U32 retMask = Parent::packUpdate(con, mask, stream);

   // The rest of the data is part of the control object packet update.
   // If we're controlled by this client, we don't need to send it.
  [b]if(getControllingClient() == con && !(mask & InitialUpdateMask))[/b]
      return retMask;

   stream->writeFlag(createHeightOn);

   stream->writeInt(mThrustDirection,NumThrustBits);

   return retMask;
}

void FlyingVehicle::unpackUpdate(NetConnection *con, BitStream *stream)
{
   Parent::unpackUpdate(con,stream);

   [b]if(getControllingClient() == con)[/b]
      return;

   createHeightOn = stream->readFlag();            

   mThrustDirection = ThrustDirection(stream->readInt(NumThrustBits));
}

May fail because it is possible for the control object to change between the time the server writes the packet, and when the client reads it. If that happens, the stream will become corrupt, and the client will be disconnected (with that wonderfully helpful error "You do not have the required art.. yadda yadda"). To fix this, you could:

U32 FlyingVehicle::packUpdate(NetConnection *con, U32 mask, BitStream *stream)
{
   U32 retMask = Parent::packUpdate(con, mask, stream);

   // The rest of the data is part of the control object packet update.
   // If we're controlled by this client, we don't need to send it.
  [b]if(stream->writeFlag(getControllingClient() == con && !(mask & InitialUpdateMask)))[/b]
      return retMask;

   stream->writeFlag(createHeightOn);

   stream->writeInt(mThrustDirection,NumThrustBits);

   return retMask;
}

void FlyingVehicle::unpackUpdate(NetConnection *con, BitStream *stream)
{
   Parent::unpackUpdate(con,stream);

   [b]if(stream->readFlag())[/b]
      return;

   createHeightOn = stream->readFlag();            

   mThrustDirection = ThrustDirection(stream->readInt(NumThrustBits));
}

Yeah, it's an extra bit in the stream, but it's preferable IMO to getting disconnected seemingly at random.

Or you could just remove the "if controllingClient return" lines entirely and write createHeightOn and mThrustDirection regardless. Writing the extra bit will save one vehicle (the client controlled one) from writing 4 extra bits. OTOH every other ghost vehicle will be sending one more bit. So if you have 4 other vehicles being ghosted, you've lost the benifit of not writing the 4 bits to the client controlled vehicle.

Either way. Doesn't matter to me, so long as I don't get disconnected.

About the author

I am an artist, a programmer, and a rogue developer, subbornly chasing a dream, trying to make the game I want to play.


#1
09/11/2005 (6:46 pm)
Thanks for the fix! Will be reviewed soon. #371.