Debugging Tools

Summary
Debugging Tools
otlib
VardumpReturns useful, readable information about variables.
ThrowBadArg“Throws” an error similar to the lua standard error of “bad argument #x to <fn_name> (<type> expected, got <type>)”.
CheckArgUsed to check to see if a function argument matches what is expected.

otlib

Summary
VardumpReturns useful, readable information about variables.
ThrowBadArg“Throws” an error similar to the lua standard error of “bad argument #x to <fn_name> (<type> expected, got <type>)”.
CheckArgUsed to check to see if a function argument matches what is expected.

Vardump

function Vardump(...)

Returns useful, readable information about variables.

Parameters

...Accepts any number of parameters of any type and prints them one by one.

Returns

A readable string serialization of the data passed in.

Example

Vardump( { "foo", apple="green", floor=41, shopping={ "milk", "cookies" } } )

returns the string...

(table: array size=1, total values=4)
  1 = "foo"
  "apple" = "green"
  "floor" = 41
  "shopping" = (table: array size=2, total values=2)
    1 = "milk"
    2 = "cookies"

Notes

  • A string will always be surrounded by quotes and a number will always stand by itself.  This is to make it easier to identify numbers stored as strings.
  • Array size and total size are shown in the table header.  Array size is the result of the pound operator (#) on the table, total size is the result of Count.  Array size is useful debug information when iterating over a table with ipairs or fori.

Revisions

v1.00Initial.

ThrowBadArg

function ThrowBadArg(argnum,
fn_name,
expected,
data,
throw_level)

”Throws” an error similar to the lua standard error of “bad argument #x to <fn_name> (<type> expected, got <type>)”.

Parameters

argnumThe optional argument number that was bad.
fn_nameThe optional string of the function name being called.
expectedThe optional string or list table of the type(s) you expected.
dataOptional and any type, the actual data you got.
throw_levelThe optional number of how many levels up to throw the error.  Defaults to 3.

Returns

Never returns, throws an error.

Revisions

v1.00Initial.

CheckArg

function CheckArg(argnum,
fn_name,
expected,
data,
throw_level)

Used to check to see if a function argument matches what is expected.  If it doesn’t, call ThrowBadArg.  This function is primarily useful at the beginning of a function definition to ensure that the correct type of data was passed in.

Parameters

argnumThe optional argument number that was bad.
fn_nameThe optional string of the function name being called.
expectedThe optional string or list table of the type(s) you expected.
dataOptional and any type, the actual data you got.
throw_levelThe optional number of how many levels up to throw the error.  Defaults to 4.

Returns

Never returns if the data is bad since it throws an error.  Otherwise returns true.

Revisions

v1.00Initial.
function Vardump(...)
Returns useful, readable information about variables.
function ThrowBadArg(argnum,
fn_name,
expected,
data,
throw_level)
“Throws” an error similar to the lua standard error of “bad argument #x to fn_name (type expected, got type)”.
function CheckArg(argnum,
fn_name,
expected,
data,
throw_level)
Used to check to see if a function argument matches what is expected.
function Count(t)
Counts the number of elements in a table using pairs.
Close