Entity/AddCallback

From Garry's Mod
Jump to: navigation, search
 Entity:AddCallback( )

Contents

Description

Add a callback function to a specific event. This is used instead of hooks to avoid calling empty functions unnecessarily.

This also allows you to use certain hooks in engine entities (non-scripted entities).

WARNING

This method does not check if the function has already been added to this object before, so if you add the same callback twice, it will be run twice! Make sure to add your callback only once.

Arguments

string hook

The hook name to hook onto. See Entity Callbacks

Arguments

function func

The function to call

Returns

number

The callback ID that was just added, which can later be used in Entity:RemoveCallback.

Returns nothing if the passed callback function was invalid or when asking for a non-existent hook.

Examples

Example

Adds a callback to an entity which is called every time the entity angles change.

myentity:AddCallback( "OnAngleChange", function( entity, newangle )
	-- Do stuff
end )


Examples

Example

Creates watermelon prop which creates sparks on collision point whenever touches something.

local melon = ents.Create( "prop_physics" ) -- Spawn prop
if ( !IsValid( melon ) ) then return end -- Safety first
melon:SetModel( "models/props_junk/watermelon01.mdl" ) -- Set watermelon model
melon:SetPos( Entity(1):GetEyeTrace().HitPos ) -- Set pos where is player looking
melon:Spawn() -- Instantiate prop

local function PhysCallback( ent, data ) -- Function that will be called whenever collision happends
	local effect = EffectData() -- Create effect data
	effect:SetOrigin( data.HitPos ) -- Set origin where collision point is
	util.Effect( "cball_bounce", effect ) -- Spawn small sparky effect
end
melon:AddCallback( "PhysicsCollide", PhysCallback ) -- Add Callback


Personal tools
Navigation