Keep references when saving

Hi people, I have an entity that controls the patrol route of an NPC, and I want to make it compatible with gm_save. Whenever I load a save with that entity, all variables and tables seem to be there, except for the one that stores a reference to the NPC. The NPC is there, but the variable in my entity is replaced with nil. Is there any way to keep references to other entities when saving game??

Bump? I’m not sure if my questions are too complex or too dumb.

Well, I solved it by myself. If anyone is interested, the solution is this hook:

 ENT:PostEntityPaste( Player Player, Table Ent, Table CreatedEntities ) 

This is executed right after an entity is pasted with the default Duplicator or the Gmod Save system. “CreatedEntities” is a table with references to all copied entities, and their key is the original EntityIndex (before copying). So, if you want to recover a reference to another entity, first you need to save its ID, then override the hook:

function ENT:SetEntity(ent)
        self.MyEntity = ent
	self.MyEntityID = ent:EntIndex()

function ENT:PostEntityPaste(pl, Ent, CreatedEntities)
	self.MyEntity = CreatedEntities[self.MyEntityID]

Perhaps someone find this useful.