🔄Callbacks

Callbacks for lua

Scripts should return a table with the following functions. If the table contains one of the functions below, it will be registered as a callback and will be called at the appropriate time.

OnScriptsLoaded(): nil

Called after all scripts are loaded.


OnDraw(): nil

Called when the game is drawing. Works only in the game. Recommended to use for drawing only.


OnFrame(): nil

The same as OnDraw, but called in the menu too.


OnUpdate(): nil

Called every game update. Works only in the game. Recommended to use for logic.


OnPreHumanizer(): nil

TODO


OnUpdateEx(): nil

Called every game update. Same as OnUpdate but as well called in the menu. Recommended to use for logic.


OnEntityCreate(entity: CEntity): nil

Name
Type
Description

entity

The entity that was created.

Called when a new entity is created.


OnEntityDestroy(entity: CEntity): nil

Name
Type
Description

entity

The entity that was destroyed.

Called when an entity is destroyed.


OnModifierCreate(entity: CNPC, modifier: CModifier): nil

Name
Type
Description

entity

The entity that has the modifier.

modifier

The modifier that was created.

Called when a modifier is created.


OnModifierDestroy(entity: CNPC, modifier: CModifier): nil

Name
Type
Description

entity

The entity that has the modifier.

modifier

The modifier that was destroyed.

Called when a modifier is destroyed.


OnModifierUpdate(entity: CNPC, modifier: CModifier): nil

Name
Type
Description

entity

The entity that has the modifier.

modifier

The modifier that was updated.

Called when a modifier is updated/refreshed.


OnEntityHurt(data: {source:CEntity|nil, target:CEntity|nil, ability:CAbility|nil, damage:number}): nil

This callback is called only in unsafe mode.

Name
Type
Description

data

{source:CEntity|nil, target:CEntity|nil, ability:CAbility|nil, damage:number}

The data about the event.

Called when an entity is hurt.


OnEntityKilled(data: {source:CEntity|nil, target:CEntity|nil, ability:CAbility|nil}): nil

This callback is called only in unsafe mode.

Name
Type
Description

data

{source:CEntity|nil, target:CEntity|nil, ability:CAbility|nil}

The data about the event.

Called when an entity is killed.


OnFireEventClient(data: {name:string, event:Event}): nil

This callback is called only in unsafe mode.

Name
Type
Description

data

{name:string, event:Event}

The data about the event.

Called when a game event is fired.


OnUnitAnimation(data: table): nil

Name
Type
Description

data

table

The data about the event.

 .unit

The unit that played the animation.

 .sequenceVariant

number

The sequence variant.

 .playbackRate

number

The playback rate.

 .castpoint

number

The castpoint.

 .type

integer

The type.

 .activity

integer

The activity.

 .sequence

integer

The sequence.

 .sequenceName

string

The sequence name.

 .lag_compensation_time

number

The lag compensation time.

Called when a unit animation is played.


OnUnitAnimationEnd(data: table): nil

Name
Type
Description

data

table

The data about the event.

 .unit

The unit that played the animation.

 .snap

boolean

The snap.

Called when a unit animation ends.


OnProjectile(data: table): nil

Name
Type
Description

data

table

The data about the event.

 .source

The source entity.

 .target

The target entity.

 .ability

The ability linked to the projectile.

 .moveSpeed

integer

The move speed.

 .sourceAttachment

integer

The source attachment.

 .particleSystemHandle

integer

The particle system handle.

 .dodgeable

boolean

The dodgeable.

 .isAttack

boolean

The is attack.

 .expireTime

number

The expire time.

 .maxImpactTime

number

The max impact time.

 .launch_tick

integer

The tick the pojectile was launched.

 .colorGemColor

integer

The color gem color.

 .fullName

string

The full name of projectile.

 .name

string

The short name of projectile.

 .handle

integer

The handle of projectile.

 .target_loc

The location of the target.

 .original_move_speed

integer

The original move speed.

Called when new projectile is created.


OnProjectileLoc(data: table): nil

Name
Type
Description

data

table

The data about the event.

 .target [?]

The source entity. (default: nil)

 .sourceLoc

The source location.

 .targetLoc

The target location.

 .moveSpeed

integer

The move speed.

 .original_move_speed

integer

The original move speed.

 .particleSystemHandle

integer

The particle system handle.

 .dodgeable

boolean

The dodgeable.

 .isAttack

boolean

The is attack.

 .expireTime

number

The expire time.

 .colorGemColor

integer

The color gem color.

 .launchTick

integer

The launch tick.

 .handle

integer

The handle of projectile.

 .fullName

string

The full name of projectile.

 .name

string

The short name of projectile.

Called when new projectile loc is created.


OnLinearProjectileCreate(data: table): nil

Name
Type
Description

data

table

The data about the event.

 .source

The source entity.

 .origin

The origin.

 .velocity

The velocity.

 .particleIndex

integer

The particle index.

 .handle

integer

The handle of projectile.

 .acceleration

The acceleration.

 .maxSpeed

number

The max speed.

 .fowRadius

number

The fow radius.

 .distance

number

The distance.

 .colorGemColor

integer

The color gem color.

 .fullName

string

The full name of projectile.

 .name

string

The short name of projectile.

Called when new linear projectile is created.


OnLinearProjectileDestroy(data: table): nil

Name
Type
Description

data

table

The data about the event.

 .handle

integer

The handle of projectile.

Called when linear projectile is destroyed.


OnParticleCreate(data: table): nil

Name
Type
Description

data

table

The data about the event.

 .index

integer

The index of particle.

 .entity [?]

The entity. (default: nil)

 .entity_id

integer

The entity id.

 .entityForModifiers [?]

The entity for modifiers. (default: nil)

 .entity_for_modifiers_id

integer

The entity for modifiers id.

 .attachType

The attach type.

 .fullName

string

The full name of particle.

 .name

string

The short name of particle.

 .hash

integer

The hash of particle.

 .particleNameIndex

integer

The particle name index.

Called when new particle is created.


OnParticleDestroy(data: table): nil

Name
Type
Description

data

table

The data about the event.

 .index

integer

The index of particle.

 .controlPoint

integer

The control point.

 .position

The position.

Called when particle is updated.


OnParticleUpdateFallback(data: table): nil

Name
Type
Description

data

table

The data about the event.

 .index

integer

The index of particle.

 .controlPoint

integer

The control point.

 .position

The position.

Called when particle is updated. Alternative version for some particles.


OnParticleUpdateEntity(data: table): nil

Name
Type
Description

data

table

The data about the event.

 .index

integer

The index of particle.

 .controlPoint

integer

The control point.

 .entity

The entity.

 .entIdx

integer

The entity id.

 .attachType

The attach type.

 .attachmentName

string

The attachment name.

 .position

The position.

 .includeWearables

boolean

Include wearables.

Called when particle is updated on entity.


OnParticleDestroy(data: table): nil

Name
Type
Description

data

table

The data about the event.

 .index

integer

The index of destroyed particle.

 .destroyImmediately

boolean

Destroy immediately.

Called when particle is destroyed.


OnStartSound(data: table): nil

Name
Type
Description

data

table

The data about the event.

 .source [?]

The source of sound. (default: nil)

 .hash

integer

The hash of sound.

 .guid

integer

The guid of sound.

 .seed

integer

The seed of sound.

 .name

string

The name of sound.

 .position

The position of sound.

Called when sound is started.


OnChatEvent(data: table): nil

Name
Type
Description

data

table

The data about the event.

 .type

integer

The type of chat event.

 .value

integer

The value of chat event.

 .value2

integer

The value2 of chat event.

 .value3

integer

The value3 of chat event.

 .playerid_1

integer

The playerid_1 of chat event.

 .playerid_2

integer

The playerid_2 of chat event.

 .playerid_3

integer

The playerid_3 of chat event.

 .playerid_4

integer

The playerid_4 of chat event.

 .playerid_5

integer

The playerid_5 of chat event.

 .playerid_6

integer

The playerid_6 of chat event.

Called on chat event.


OnOverHeadEvent(data: {player_source:CPlayer|nil, player_target:CPlayer|nil, target_npc:CNPC, value:integer}): nil

Name
Type
Description

data

{player_source:CPlayer|nil, player_target:CPlayer|nil, target_npc:CNPC, value:integer}

The table with the event info.

Called on event above the hero's head.


OnUnitAddGesture(data: table): nil

Name
Type
Description

data

table

The data about the event.

 .npc [?]

The unit that is added to a gesture. (default: nil)

 .sequenceVariant

integer

The sequence variant.

 .playbackRate

number

The playback rate.

 .fadeIn

number

The fade in.

 .fadeOut

number

The fade out.

 .slot

integer

The slot.

 .activity

integer

The activity.

 .sequenceName

string

The sequence name.

Called when a unit is added to a gesture.


OnPrepareUnitOrders(data: table): boolean

Name
Type
Description

data

table

The data about the event.

 .player

The player that issued the order.

 .order

The order type.

 .target [?]

The target of the order. (default: nil)

 .position

The position of the order.

 .ability [?]

The ability of the order. (default: nil)

 .orderIssuer

The order issuer.

 .npc

The unit of the order.

 .queue

boolean

If the order is queued.

 .showEffects

boolean

The show effects of the order.

Called on every player order. Return false to prevent the order from being executed.


OnGCMessage(data: table): boolean

Name
Type
Description

data

table

 .msg_type

number

The message type.

 .size

number

The size of the message.

 .binary_buffer_send [?]

userdata

The binary buffer of the send message. (default: nil)

 .binary_buffer_recv [?]

userdata

The binary buffer of the recieved message. (default: nil)

Called when a game coordinator protobuff message is received. Return false to prevent the message from being sent (doesnt work with recieved messages). For more look at GC table description.

Example

-- ongc_message.lua
-- import protobuf and json libraries
local protobuf = require('protobuf');
local JSON = require('assets.JSON');

-- do the stats request
local request = protobuf.encodeFromJSON('CMsgDOTAMatchmakingStatsRequest', JSON:encode({}));
GC.SendMessage( request.binary, 7197, request.size );

return {
    OnGCMessage = function(msg)
        if (msg.msg_type ~= 7198) then
            return true;
        end

        -- decode the response and print it
        local response = protobuf.decodeToJSON('CMsgDOTAMatchmakingStatsResponse', msg.binary_buffer_recv, msg.size);
        Log.Write(response);

        return true;
    end
}

OnSendNetMessage(data: table): boolean

Name
Type
Description

data

table

The data about the event.

 .message_id

number

The message id.

 .message_name

string

The message name.

 .buffer

lightuserdata

The encoded buffer of the message.

 .size

number

The size of the message.

Called when a net message is sent. Return false to prevent the message from being sent. See example

Example

-- onsend_netmsg.lua
-- anti-mute script for dota 2
-- redirects all chat messages to console command 'say' or 'say_team'

-- import protobuf and json libraries
local protobuf = require('protobuf');
local JSON = require('assets.JSON');
return {
    OnSendNetMessage = function(msg)
        if msg.message_id ~= 394 then
            return true;
        end

        -- decode protobuf message to json
        local json_message = JSON:decode(protobuf.decodeToJSON("CDOTAClientMsg_ChatMessage", msg.buffer , msg.size));
        if not json_message then
            return true;
        end

        -- message CDOTAClientMsg_ChatMessage {
        --     optional uint32 channel_type = 1;
        --     optional string message_text = 2;
        -- }

        local text_message = json_message.message_text;
        -- skip commands starting with '-'
        if text_message:find("-") == 1 then
            return true;
        end
    
        -- 11 - all chat 
        if (json_message.channel_type == 11) then
            Engine.ExecuteCommand('say "'..text_message..'"');
            return false;
        -- 12 - team chat
        elseif (json_message.channel_type == 12) then
            Engine.ExecuteCommand('say_team "'..text_message..'"');
            return false;
        end
    end
}

OnPostReceivedNetMessage(data: table): boolean

Name
Type
Description

data

table

The data about the event.

 .message_id

number

The message id.

 .msg_object

lightuserdata

The encoded buffer of the message.

Called when a net message is received. Return false to prevent the message from being recieved

Example

-- onrecv_netmsg.lua
local protobuf = require('protobuf')
local JSON = require('assets.JSON')
return {
    OnPostReceivedNetMessage = function(msg)
        if msg.message_id == 612 then -- DOTA_UM_ChatMessage https://github.com/SteamDatabase/GameTracking-Dota2/blob/932a8b002f651262ffda6562b758d8ca97c98297/Protobufs/dota_usermessages.proto#L152
            local json = protobuf.decodeToJSONfromObject(msg.msg_object);
            Log.Write(json)
            local lua_table = JSON:decode(json)
            -- ...
        end
    end
}

OnSetDormant(npc: CNPC, type: Enum.DormancyType): nil

Name
Type
Description

npc

The target npc.

type

The type of change.

Called on NPC dormancy state changed.


OnGameRulesStateChange(data: {}): nil

Name
Type
Description

data

{}

The table with new game state info.

Called on gamestate change.


OnNpcDying(npc: CNPC): nil

Name
Type
Description

npc

The target npc.

Called on NPC dying.

Last updated