ENTITY:OnRemove
Description
Called when the entity is about to be removed.
See also Entity:CallOnRemove, which can even be used on engine (non-Lua) entities.
Example
Create an explosion when the entity will be removed. To create an entity, you can read ents.Create.
Arguments
The entity may or may not be recreated immediately after, depending on whether it is in the local player's PVS. (See Entity:IsDormant)
Clientside behaviour remarks
This hook may be called at odd times (when entity has actually not yet been removed from the server). This happens during the "full update" mechanism with the ENTITY:Initialize function not being called even when the entity reappears. Such calls can be differentiated via the fullUpdate
argument of this hook.
You can debug this behaviour by enabling sv_cheats
and running cl_fullupdate
on the client.
GM:NotifyShouldTransmit can be used to circumvent this problem. ENTITY:Think can also be used to detect that the entity has reappeared. You may reinitialize any necessary data in these hooks. Another way to fix this problem can be found below.