Lag Compensation: Difference between revisions

From Valve Developer Community
Jump to navigation Jump to search
m (moved Lag Compensation to Lag compensation: sentence case)
(expanded article somewhat, mainly to get in an explanation of how to implement.)
Line 1: Line 1:
You may be looking for:
: ''You may be looking for [[Latency Compensating Methods in Client/Server In-game Protocol Design and Optimization|Yahn Bernier's 2001 paper on game engine networking]].''


* [[Latency Compensating Methods in Client/Server In-game Protocol Design and Optimization|The theory behind lag compensation]] <!-- This isn't a good fit for the URL, but nevertheless used to reside here. External sites link to this page an expect this article, so please leave the disambiguation in place. -->
[[File:Lag compensation.jpg|thumb|Client hitboxes (red) versus rewound server hitboxes (blue). 200ms [[net_fakelag|fakelag]].]]
* [[Source Multiplayer Networking#Lag compensation|Its implementation in Source]]
 
'''Lag compensation''' is the notion of the server calculating when a [[usercmd]] was sent, and rewinding time to that point when processing it. In combination with [[prediction]], lag compensation can help to combat network latency to the point of almost eliminating it in many scenarios.
 
By default ''only'' players are rewound. For a more detailed explanation, see [[Source Multiplayer Networking#Lag compensation|Source Multiplayer Networking]].
 
{{note|<code>[[CBasePlayer]]</code> does not use lag compensation; it must be implemented by a derived class. Valve's networked player classes do this already.}}
 
== Implementation ==
 
Simply wrap whatever code you want lag compensated with two calls to the <code>lagcompensation</code> object:
 
<source lang=cpp>#ifdef GAME_DLL
 
#include "..\server\ilagcompensationmanager.h"
 
void CMyPlayer::FireBullets ( const FireBulletsInfo_t &info )
{
lagcompensation->StartLagCompensation( this, this->GetCurrentCommand() );
 
BaseClass::FireBullets(info);
 
lagcompensation->FinishLagCompensation( this );
}
#endif</source>
 
Time will be wound back on the call to <code>StartLagCompensation()</code>, and wound forward again on the call to <code>FinishLagCompensation()</code>.
 
== See also ==
 
* [[Source Multiplayer Networking#Lag compensation]]
* [[Prediction]]
* [[Interpolation]]
 
[[Category:Networking]]

Revision as of 14:50, 13 August 2009

You may be looking for Yahn Bernier's 2001 paper on game engine networking.
Client hitboxes (red) versus rewound server hitboxes (blue). 200ms fakelag.

Lag compensation is the notion of the server calculating when a usercmd was sent, and rewinding time to that point when processing it. In combination with prediction, lag compensation can help to combat network latency to the point of almost eliminating it in many scenarios.

By default only players are rewound. For a more detailed explanation, see Source Multiplayer Networking.

Note.pngNote:CBasePlayer does not use lag compensation; it must be implemented by a derived class. Valve's networked player classes do this already.

Implementation

Simply wrap whatever code you want lag compensated with two calls to the lagcompensation object:

#ifdef GAME_DLL

#include "..\server\ilagcompensationmanager.h"

void CMyPlayer::FireBullets ( const FireBulletsInfo_t &info )
{
	lagcompensation->StartLagCompensation( this, this->GetCurrentCommand() );

	BaseClass::FireBullets(info);

	lagcompensation->FinishLagCompensation( this );
}
#endif

Time will be wound back on the call to StartLagCompensation(), and wound forward again on the call to FinishLagCompensation().

See also