Talk:NPC Lag Compensation

From Valve Developer Community
Jump to: navigation, search

I appreciate that this article is messily laid out ... help on that front would be appreciated! Rather than large code blocks, I had initially used the form "select this whole function and find & replace in selection Player, replacing with Entity" ... this got very confusing. --Winston 16:40, 11 Apr 2008 (PDT)



We used this code for Zombie Master, and it appears to contain a memory leak (at the time of writing). I solved it by moving the NPC's lag compensation data (ie. its linked list of LagRecords etc) into BaseNPC. Not very pretty, but it stops leaking memory. I'd theorize that the switching around of tracks performed in UpdateAIIndexes() may be losing track of memory somehow, perhaps only in certain edge cases where many NPCs die. Whatever the cause, having each NPC hold its own lagcompensation data simplifies things a lot and prevents the leak from occurring.

I don't feel I have sufficient information on the cause of the leak to modify this article, and the solution I described isn't ideal, but I figured I'd at least leave a note for people who run into a similar problem. --theGreenBunny 01:12, 13 Dec 2008 (PST)

Aha, I was experiencing a significant leak too, but I'd never suspected this code. Eitehr what you describe, or perhaps some of the Purge calls need to be changed to PurgeAndDeleteContents. I hope to be able to get back to this sometime in the next few weeks. --Winston 12:55, 20 Dec 2008 (PST)
Ok, it's been most of a year. I've now tried to merge the required changes into this article, based on code that has been further modified. There may be errors or inconsistancies, so if you try this, please post here. --Winston 22:32, 14 November 2009 (UTC)

code incorrectly updated

After removing the old code that caused memory leaks and try to implement the new changes I have encountered a problem. The wiki is said to add this code to end of FrameUpdatePostEntityThink() function:

CAI_BaseNPC **ppAIs = g_AI_Manager.AccessAIs();

int nAIs = g_AI_Manager.NumAIs();

for ( int i = 0; i < nAIs; i++ ) {

CAI_BaseNPC *pNPC = ppAIs[i];

CUtlFixedLinkedList< LagRecord > *track = &m_EntityTrack[i];

(function continues)


but "CUtlFixedLinkedList< LagRecord > *track = &m_EntityTrack[i];" is wrong, because with new changes the m_EntityTrack[] array not exist and project not compile.

I have tried change the wrong line for this: CUtlFixedLinkedList< LagRecord > *track = pNPC->GetLagTrack();

But the engine crash on the next line... --KuRouZu 6:01, 17 May 2010 (UTC)

This is most likely because track is null. It doesn't look like the changes include actually instantiating m_LagTrack in CAI_BaseNPC's constructor. Also, you should probably check to see if pNPC is null (removing the track->RemoveAll() line) before trying to grab its lag track. I've updated the wiki to reflect these two changes. --ssba 17:43, 18 May 2010 (UTC)
With new changes, in an apparently random (about 5 - 20 minutes), the server is restarted without leaving any 'Mdmp' files. I had to return to the previous version of the code for the moment. --KuRouZu 2:22, 24 May 2010 (UTC)
I believe I've finally fixed all of the issues with this tutorial. It involved renaming a lot of instances of LagRecord and LayerRecord and some other minor adjustments, but upon doing so, the system finally appears to be working as it should. --£cho 22:49, 14 July 2010 (UTC)

LAG_COMPENSATION_TELEPORTED_DISTANCE_SQR

This macro is not defined and it stopping the compile with an error. I found this in a source 2007 player_lagcompensation.cpp file on github. It might work for this. ( https://github.com/VSES/SourceEngine2007/blob/master/se2007/game/server/player_lagcompensation.cpp#28 )

  1. #define LAG_COMPENSATION_TELEPORTED_DISTANCE_SQR ( 64.0f * 64.0f )

--Payne (talk) 12:04, 28 February 2018 (UTC)