AddDebugHook: Difference between revisions

From Multi Theft Auto: Wiki
Jump to navigation Jump to search
(Undo revision 43236 by AlexTMjugador (talk))
No edit summary
Line 29: Line 29:
==Callback parameters==
==Callback parameters==
<syntaxhighlight lang="lua">
<syntaxhighlight lang="lua">
pre/postEvent( resource sourceResource, string eventName, element eventSource, element eventClient, string luaFilename, int luaLineNumber, eventArguments )
pre/postEvent( resource callingResource, string eventName, element eventSource, element eventClient, string luaFilename, int luaLineNumber, eventArguments )
pre/postFunction( resource sourceResource, string functionName, bool isAllowedByACL, string luaFilename, int luaLineNumber, functionArguments )
pre/postFunction( resource callingResource, string functionName, bool isAllowedByACL, string luaFilename, int luaLineNumber, functionArguments )
</syntaxhighlight>
</syntaxhighlight>


Line 36: Line 36:
This example will dump info about all triggered events:
This example will dump info about all triggered events:
<syntaxhighlight lang="lua">
<syntaxhighlight lang="lua">
function onPreEvent( sourceResource, eventName, eventSource, eventClient, luaFilename, luaLineNumber, ... )
function onPreEvent( callingResource, eventName, eventSource, eventClient, luaFilename, luaLineNumber, ... )
     local args = { ... }
     local args = { ... }
     local srctype = eventSource and getElementType(eventSource)
     local srctype = eventSource and getElementType(eventSource)
     local resname = sourceResource and getResourceName(sourceResource)
     local resname = callingResource and getResourceName(callingResource)
     local plrname = eventClient and getPlayerName(eventClient)
     local plrname = eventClient and getPlayerName(eventClient)
     outputDebugString( "preEvent"
     outputDebugString( "preEvent"
Line 56: Line 56:
This example will dump info about all called MTA functions:
This example will dump info about all called MTA functions:
<syntaxhighlight lang="lua">
<syntaxhighlight lang="lua">
function onPreFunction( sourceResource, functionName, isAllowedByACL, luaFilename, luaLineNumber, ... )
function onPreFunction( callingResource, functionName, isAllowedByACL, luaFilename, luaLineNumber, ... )
     local args = { ... }
     local args = { ... }
     local resname = sourceResource and getResourceName(sourceResource)
     local resname = callingResource and getResourceName(callingResource)
     outputDebugString( "preFunction"
     outputDebugString( "preFunction"
         .. " " .. tostring(resname)
         .. " " .. tostring(resname)

Revision as of 23:57, 3 March 2016

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

Debug hooks are not recursive, so functions and events triggered inside the hook callback will not be traced.

Syntax

bool addDebugHook( string hookType, function callbackFunction[, table nameList ] )

Required Arguments

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

Optional Arguments

ADDED/UPDATED IN VERSION 1.3.5 r6142:
  • nameList: Table of strings for restricting which functions and events the hook will be triggered on

Returns

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

Callback parameters

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

Example

This example will dump info about all triggered events:

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

This example will dump info about all called MTA functions:

function onPreFunction( callingResource, functionName, isAllowedByACL, luaFilename, luaLineNumber, ... )
    local args = { ... }
    local resname = callingResource and getResourceName(callingResource)
    outputDebugString( "preFunction"
        .. " " .. tostring(resname)
        .. " " .. tostring(functionName)
        .. " allowed:" .. tostring(isAllowedByACL)
        .. " file:" .. tostring(luaFilename)
        .. "(" .. tostring(luaLineNumber) .. ")"
        .. " numArgs:" .. tostring(#args)
        .. " arg1:" .. tostring(args[1])
        )
end
addDebugHook( "preFunction", onPreFunction)

This example adds a hook which will only be triggered for the named functions

addDebugHook( "preFunction", onPreFunction, {"setElementPosition","loadstring"} )

Changelog

Version Description
1.3.5-9.06054 Added clientside
1.3.5-9.06142 Added option to restrict to specified functions and events

Requirements

Minimum server version 1.3.4-9.05939
Minimum client version 1.3.5-9.06054

Note: Using this feature requires the resource to have the above minimum version declared in the meta.xml <min_mta_version> section. e.g. <min_mta_version server="1.3.4-9.05939" client="1.3.5-9.06054" />

See Also