Weapon Switching using MountPoints

Original Source  -Royce Tucker, Lead Programmer of 40 Past Midnight
Weapon switching using MountPoints



The mount point system in V3D is extremely easy to use. All you have to do is grab a model, go to the mount point tab in the Entity editor, and throw mounts all over the place.

Once that is done, you need to attach them to your character. This is done with this line of code:

AttachObjectToMountPoint( new NameID( mountPointName ), objectToMount );

This function does exactly what it says, it attaches the object to the mount point, but one thing we have to note is that it also adds the attachment to the Actor's Component list . The component list has a maximum capacity of 32 and using AttachObjectToMountPoint DOES NOT delete the any previous components. So, in short, calling AttachObjectToMountPoint to switch a weapon from one mount to another will crash the engine with extreme prejudice after 32 executions.

To circumvent that, we'll need to manually add and remove the weapons from the component list ourselves. I'm using a list of weapons called "GunList" and holding the index of the current weapon to keep track of things.

In your custom player class add:

// object args is there simply to allow the function to be binded to a key and called through the command spec, its never used for anything
public void SwitchWeaponForward( object args )
{
      string holster = GunList[currentWeaponIndex].holsterPointName;
      string hand = GunList[currentWeaponIndex].equippedPointName;

      // Relocate the weapon to the holster
     MountedAttachments.AddOrReplace( new Attachment( holster, new NameID(holster), GunList[currentWeaponIndex] ) );

     // Remove the old component and add the new one
     Components.Remove( MountedAttachments[hand] );
     Components.Add( MountedAttachments[holster] );

     // Increase the weapon index
     currentWeaponIndex = (currentWeaponIndex + 1) % GunList.Length;

     holster = GunList[currentWeaponIndex].holsterPointName;
     hand = GunList[currentWeaponIndex].equippedPointName;

     // Relocate the new weapon to the player's hand
     MountedAttachments.AddOrReplace( new Attachment(hand, new NameID(hand), GunList[currentWeaponIndex] ) );

     // Remove the old component and add the new one
     Components.Remove( MountedAttachments[holster] );
     Components.Add( MountedAttachments[hand] );
}

Thanks, this is amazing~

I would have a try later~ Thanks. :)


love