Skip to content

Tutorial Debugging in OTClient

Majesty edited this page Feb 3, 2023 · 1 revision

I find these functions very useful while debugging Lua code scripts, especially if something goes wrong and I need a closer look at the current state of Lua tables in-game.

They are especially handy if the table is dynamic and continuously populated and manipulated live. Instead of reading potentially many lines of code and guessing how the table is manipulated, rows inserted and deleted etc, I can just dump it to console whenever I want and look at the information.

These functions are located in modules/corelib/util.lua:

  • dump(input)

    • Convert input data to human readable string
  • pdump(input)

    • execute dump(input) and print it to console. (print the dump)
  • tdump(title, input)

    • Beautify the output further by adding newlines before and after dump, as well as adding a title to the console to represent this data.
    • (print the dump better using a title)

The function names are short and obscure on purpose, once you get used to them, it is nice to be able to quickly and effortlessly use them.

Demo code:

tdump("My first tdump", {
    hello = "world"
})

Looks like this in console:

======================
=== My first tdump ===
======================
{ 
    ["hello"] = "world"
}
=======================

Demo code:

tdump("Player Position", g_game.getLocalPlayer():getPosition())

Looks like this in console:

=======================
=== Player Position ===
=======================
{ 
    ["y"] = 156,
    ["x"] = 175,
    ["z"] = 8
}
=======================

image

Notice that the dump output (with exception of userdata / function references) can be copy-pasted directly back into Lua environment (Lua table definition compliant) in a variable to if you need to simulate the state and test things out.

If you want to copy the text or inspect a table further, you can open up otclient.log to inspect dump results.

If the dump is small enough for a player message, you could use dump(input) to return the string without printing it to a console, and print it out to the player instead. (etc the player who triggers a talkaction). Alternatively, use pdump(input) and do both.