AddDebugHook: Difference between revisions

From Multi Theft Auto: Wiki
Jump to navigation Jump to search
No edit summary
No edit summary
Line 31: Line 31:
function onPreEvent( sourceResource, eventName, eventSource, eventClient, luaFilename, luaLineNumber, ... )
function onPreEvent( sourceResource, eventName, eventSource, eventClient, luaFilename, luaLineNumber, ... )
     local args = { ... }
     local args = { ... }
     local resname
     local srctype = eventSource and getElementType(eventSource)
     if sourceResource then
     local resname = sourceResource and getResourceName(sourceResource)
        resname = getResourceName(sourceResource)
     local plrname = eventClient and getPlayerName(eventClient)
     else
        resname = "unknown"
    end
     outputDebugString( "preEvent"
     outputDebugString( "preEvent"
         .. " " .. tostring(resname)
         .. " " .. tostring(resname)
         .. " " .. tostring(eventName)
         .. " " .. tostring(eventName)
         .. " source:" .. tostring(eventSource)
         .. " source:" .. tostring(srctype)
         .. " player:" .. tostring(eventClient)
         .. " player:" .. tostring(plrname)
         .. " file:" .. tostring(luaFilename)
         .. " file:" .. tostring(luaFilename)
         .. ":" .. tostring(luaLineNumber)
         .. ":" .. tostring(luaLineNumber)

Revision as of 05:59, 24 November 2013

This function allows tracing of MTA functions and events. It should only be used when debugging scripts as it may degrade script performance.

Syntax

bool addDebugHook( string hookType, function callbackFunction )

Required Arguments

  • hookType: The type of hook to add. This can be:
    • preEvent
    • postEvent
    • preFunction
    • postFunction
  • callbackFunction : The function to call

Returns

Returns true if the hook was successfully added, or false otherwise.

Callback parameters

pre/postEvent( resource sourceResource, string eventName, element eventSource, element eventClient, string luaFilename, int luaLineNumber, eventArguments )
pre/postFunction( resource sourceResource, string functionName, bool isAllowedByACL, string luaFilename, int luaLineNumber, functionArguments )

Example

This example will dump info about all triggered events:

function onPreEvent( sourceResource, eventName, eventSource, eventClient, luaFilename, luaLineNumber, ... )
    local args = { ... }
    local srctype = eventSource and getElementType(eventSource)
    local resname = sourceResource and getResourceName(sourceResource)
    local plrname = eventClient and getPlayerName(eventClient)
    outputDebugString( "preEvent"
        .. " " .. tostring(resname)
        .. " " .. tostring(eventName)
        .. " source:" .. tostring(srctype)
        .. " player:" .. tostring(plrname)
        .. " file:" .. tostring(luaFilename)
        .. ":" .. tostring(luaLineNumber)
        .. " #args:" .. tostring(#args)
        .. " args[1]:" .. tostring(args[1])
        )
end
addDebugHook( "preEvent", onPreEvent )

Requirements

This template will be deleted.

See Also