Skip to content

tw.debugger

Side: Shared + Client

Performance benchmarking and visual debugging tools.

Shared Functions

tw.debugger.benchmark(label?, cb)

Measures the execution time of a function.

ParameterTypeDefaultDescription
labelstring?nilOptional label printed with the result
cbfunctionFunction to benchmark

Returns: number (execution time in microseconds)


tw.debugger.benchmark_repeat(label?, iterations?, cb, delay_between?)

Runs a function multiple times and returns the average execution time.

ParameterTypeDefaultDescription
labelstring?nilOptional label printed with the result
iterationsinteger?1000Number of iterations
cbfunctionFunction to benchmark
delay_betweeninteger?nilMilliseconds to wait between iterations

Returns: number (average execution time in microseconds)

Client Functions

tw.debugger.resetText()

Resets the Y offset for drawText. Call at the beginning of each frame to stack text lines from the top.


tw.debugger.drawText(text, pos_x?, pos_y?)

Draws debug text on screen. Each call advances the Y position automatically if pos_y is not specified.

ParameterTypeDefaultDescription
textstringText to display
pos_xnumber?0.5Screen X position (0.0 to 1.0)
pos_ynumber?autoScreen Y position (auto-increments if omitted)

tw.debugger.drawSphere(coords, radius?, color?)

Draws a 3D debug sphere in the world.

ParameterTypeDefaultDescription
coordsvector3World position
radiusnumber?0.5Sphere radius
colortable?{r=255, g=0, b=0, a=128}RGBA color

Examples

Benchmark a function

lua
local time = tw.debugger.benchmark("hash lookup", function()
    local hash = GetHashKey("a_m_m_rancher_01")
end)
print("Took " .. time .. " us")

Average benchmark over many iterations

lua
local avg = tw.debugger.benchmark_repeat("inventory check", 5000, function()
    tw.fw:canUseItem(source, "bandage", 1)
end)
print("Average: " .. avg .. " us")

On-screen debug text (client)

lua
-- In a tick loop
CreateThread(function()
    while true do
        Wait(0)
        tw.debugger.resetText()
        tw.debugger.drawText("Player Pos: " .. tostring(GetEntityCoords(PlayerPedId())))
        tw.debugger.drawText("Health: " .. GetEntityHealth(PlayerPedId()))
        tw.debugger.drawText("Speed: " .. GetEntitySpeed(PlayerPedId()))
    end
end)

3D debug sphere

lua
-- Visualize a collection point
tw.debugger.drawSphere(vec3(-301.62, 783.04, 117.75), 1.0, { r = 0, g = 255, b = 0, a = 100 })

Notes

  • Benchmark results are in microseconds (1 ms = 1000 us).
  • drawText auto-increments the Y position with each call. Use resetText() at the start of each frame to avoid text drifting down.
  • drawSphere must be called every frame to remain visible (like all draw functions in RedM).
  • These tools are intended for development only. Remove debug draws before shipping.

Premium RedM Scripts — Multi-Framework