🔄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

Called when a new entity is created.


OnEntityDestroy(entity: CEntity): nil

Called when an entity is destroyed.


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

Called when a modifier is created.


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

Called when a modifier is destroyed.


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

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.

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.

Called when an entity is killed.


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

This callback is called only in unsafe mode.

Called when a game event is fired.


OnUnitAnimation(data: table): nil

Called when a unit animation is played.


OnUnitAnimationEnd(data: table): nil

Called when a unit animation ends.


OnProjectile(data: table): nil

Called when new projectile is created.


OnProjectileLoc(data: table): nil

Called when new projectile loc is created.


OnLinearProjectileCreate(data: table): nil

Called when new linear projectile is created.


OnLinearProjectileDestroy(data: table): nil

Called when linear projectile is destroyed.


OnParticleCreate(data: table): nil

Called when new particle is created.


OnParticleDestroy(data: table): nil

Called when particle is updated.


OnParticleUpdateFallback(data: table): nil

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


OnParticleUpdateEntity(data: table): nil

Called when particle is updated on entity.


OnParticleDestroy(data: table): nil

Called when particle is destroyed.


OnStartSound(data: table): nil

Called when sound is started.


OnChatEvent(data: table): nil

Called on chat event.


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

Called on event above the hero's head.


OnUnitAddGesture(data: table): nil

Called when a unit is added to a gesture.


OnPrepareUnitOrders(data: table): boolean

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


OnGCMessage(data: table): boolean

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

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

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

Called on NPC dormancy state changed.


OnGameRulesStateChange(data: {}): nil

Called on gamestate change.


OnNpcDying(npc: CNPC): nil

Called on NPC dying.

Last updated