M PlayerAnimState: Difference between revisions

From Valve Developer Community
Jump to navigation Jump to search
(→‎Implementation: incomplete)
(implemented in SDK Skeleton)
Line 12: Line 12:
== Implementation ==
== Implementation ==


{{todo|Complete this. There are lots of things to do, so it may be better to provide a sample player class instead of explaining step-by-step.}}
Implementation is complex, and requires editing several classes. See [[SDK Skeleton]]'s multiplayer components for a finished product.
 
You must define <code>m_PlayerAnimState</code> in both your client and server classes:
 
<source lang=cpp>
class CMyPlayer : public CBasePlayer // and on client!
{
// ...
CMultiPlayerAnimState* m_PlayerAnimState;
// ...
};
</source>
 
And then initialise it in shared code:
 
<source lang=cpp>
inline void CMyPlayer::Spawn_Shared()
{
MultiPlayerMovementData_t mv;
mv.m_flSprintSpeed = -1;
mv.m_flRunSpeed = 200;
mv.m_flWalkSpeed = 80;
mv.m_flBodyYawRate = 180;
m_PlayerAnimState = new CMultiPlayerAnimState( this,mv );
 
UseClientSideAnimation();
}
</source>
 
Now you need to hook things up. On the server:
 
<source lang=cpp>
void CMyPlayer::PostThink()
{
BaseClass::PostThink();
 
// Keep the model upright; pose params will handle pitch aiming.
QAngle angles = GetLocalAngles();
angles[PITCH] = 0;
SetLocalAngles( angles );
 
m_PlayerAnimState->Update( EyeAngles()[YAW], EyeAngles()[PITCH] );
}
 
void CMyPlayer::DoAnimationEvent( PlayerAnimEvent_t event, int nData )
{
m_PlayerAnimState->DoAnimationEvent( event, nData );
TE_PlayerAnimEvent( this, event, nData ); // Transmit to other clients. Comment this out in SP.
}
</source>
 
And on the client:
 
<source lang=cpp>
#include "prediction.h"
#include "c_basetempentity.h"
 
void C_MyPlayer::UpdateClientSideAnimation()
{
m_PlayerAnimState->Update( EyeAngles()[YAW], EyeAngles()[PITCH] );
}
 
void C_MyPlayer::DoAnimationEvent( PlayerAnimEvent_t event, int nData )
{
if ( IsLocalPlayer() && prediction->InPrediction() && !prediction->IsFirstTimePredicted() ) )
return;
MDLCACHE_CRITICAL_SECTION();
m_PlayerAnimState->DoAnimationEvent( event, nData );
}
</source>
 
You will also want to expand <code>TranslateActivity()</code> to poll weapons for [[acttable_t|activity translation tables]]:
 
<source lang=cpp>
Activity CMultiPlayerAnimState::TranslateActivity( Activity actDesired )
{
// Existing swimming code...
 
if ( GetBasePlayer()->GetActiveWeapon() )
actDesired = GetBasePlayer()->GetActiveWeapon()->ActivityOverride( actDesired, false );
 
return actDesired;
}
</source>


=== Dispatching to clients ===
=== Dispatching to clients ===

Revision as of 06:11, 8 November 2010

The m_PlayerAnimState object maintains a player's animation state. It is shared code.

There are two versions: CBasePlayerAnimState and CMultiPlayerAnimState.

Usage

Update( float eyeYaw, float eyePitch )
Manages ambient, looping animations (running/swimming, aiming, breathing) based on player velocity and look direction every frame.
Tip.pngTip:CMultiPlayerAnimState will have your model playing one of the ACT_MP activities (search in Visual Studio's Class View for a list). You will probably want to add activity translation tables to your weapons to turn these into activities pertaining to the weapon currently being held. Missing or incomplete translation tables are the number one cause of "jesus pose" players.
Note.pngNote:Your player model must be set up with the appropriate blend sequences for this to work. Unfortunately there are several different schemes floating around: Ep1, OB and Counter-Strike players are all driven differently. Check SetupPoseParameters() to see what your AnimState object is looking for.
DoAnimationEvent( PlayerAnimEvent_t event, int nData = 0 )
Begins a discrete, event-based animation (crouching, shooting, reloading) whenever called. PlayerAnimEvent_t is an enum value that defines what type of animation event this is; it is translated into an activity elsewhere depending on the status of the player (weapon held, swimming, etc).

Implementation

Implementation is complex, and requires editing several classes. See SDK Skeleton's multiplayer components for a finished product.

Dispatching to clients

In multiplayer you must dispatch animation events to all onlooking clients, a job which is not performed by CBasePlayer. See m_PlayerAnimState/TempEnt for the temporary entity used by Valve.