Garry's Mod Wiki

ENTITY:OnRemove

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.

function ENT:OnRemove() local explosion = ents.Create( "env_explosion" ) -- The explosion entity explosion:SetPos( self:GetPos() ) -- Put the position of the explosion at the position of the entity explosion:Spawn() -- Spawn the explosion explosion:SetKeyValue( "iMagnitude", "50" ) -- the magnitude of the explosion explosion:Fire( "Explode", 0, 0 ) -- explode end

Clientside behaviour remarks

This hook is called during clientside full updates.

Issue Tracker: 4675

This hook may be called at odd times (when entity has actually not yet been removed from the server). This happens during fullupdate with the ENTITY:Initialize function not being called even when the entity reappears. You can debug this behaviour by enabling 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 at the bottom of this page.

Example

Another way to circumvent the problem where the client thinks the entity is removed even though it has not is to test if the entity is valid using a tick delay.

function ENT:OnRemove() timer.Simple( 0, function() if not IsValid( self ) then -- Your remove code here end end) end

Page Links


Special Pages


Wikis

?

Render Time: 32ms

DB GetPage 3
Generate Html 7
SaveChanges (1) 12
Render Body 0
Render Sidebar 6