destroy entity event||doc135515

September 20, 2022

Format

 ENTITY_LEAVE
  • The class to which the function belongs:Entity

Description

This event is fired when an entity is destroyed.


Context

The context content of the event

name type Description
obj1 Entity Destroyed entity instance


Code Samples

On the server side of the entity templatemount script, add the register when the entity of this class is destroyedCallbackbroadcast tipsDestroyed entity object name

Trigger.addHandler(this:cfg(), "ENTITY_LEAVE", function(context)        --Register entity destruction callback function
    local entity = context.obj1
    World.CurWorld.SystemNotice(1, entity.name, 40)                     --Broadcast object name
end)

on the server sideglobal script, add the register when the game startsCallback, the entity is spawned, and destroyed after one second

Trigger.addHandler(World.cfg, "GAME_START", function()
    local world = World.CurWorld                     --Get current world
    local defaultMap = world.defaultMap              --Get the default map of the current world
    local createParams = {
    cfgName = "myplugin/entity",                     --The config name of the entity
    pos = { x = 0, y = 1, z = 0 } ,                  --Entity spawn location
    map = defaultMap                                 --Entity generated map
    }
    local entity = EntityServer.Create(createParams) --Generate entity
    World.Timer(60,function()
        entity:destroy()                             --Destroy the entity and trigger the destroy entity callback function
    end)
end)