dump function

Started by EK.IceFlake, Mar 17, 2017, 01:45 PM

Previous topic - Next topic

EK.IceFlake

Here's a simple function to help you debug
function dump(var, indent=0)
{
    if (indent == 0)
    {
        print("[unknown]: " + typeof var + " " + var);
        dump(var, indent + 1);
    }
    else
    {
        foreach (idx, value in var)
        {
            local isindexable = function(v) try { v.len(); return true; } catch (e) return false;
            local indents = "";
            for (local i = 0; i < indent; ++i) indents += "    ";
            print(indents + idx + ": " + typeof value + " " + value);
            if (isindexable(value)) dump(value, indent + 1);
        }
    }
}
Usage: dump(variable);
Example:
dump(["Hello", {Hello = "World"}, ["Hello", "World"], "World"]);Outputs:
[SCRIPT]  [unknown]: array (array : 0x000001CB2C3ADF00)
[SCRIPT]      0: string Hello
[SCRIPT]          0: integer 72
[SCRIPT]          1: integer 101
[SCRIPT]          2: integer 108
[SCRIPT]          3: integer 108
[SCRIPT]          4: integer 111
[SCRIPT]      1: table (table : 0x000001CB2C3AC320)
[SCRIPT]          Hello: string World
[SCRIPT]              0: integer 87
[SCRIPT]              1: integer 111
[SCRIPT]              2: integer 114
[SCRIPT]              3: integer 108
[SCRIPT]              4: integer 100
[SCRIPT]      2: array (array : 0x000001CB2C3AE2C0)
[SCRIPT]          0: string Hello
[SCRIPT]              0: integer 72
[SCRIPT]              1: integer 101
[SCRIPT]              2: integer 108
[SCRIPT]              3: integer 108
[SCRIPT]              4: integer 111
[SCRIPT]          1: string World
[SCRIPT]              0: integer 87
[SCRIPT]              1: integer 111
[SCRIPT]              2: integer 114
[SCRIPT]              3: integer 108
[SCRIPT]              4: integer 100
[SCRIPT]      3: string World
[SCRIPT]          0: integer 87
[SCRIPT]          1: integer 111
[SCRIPT]          2: integer 114
[SCRIPT]          3: integer 108
[SCRIPT]          4: integer 100

EK.IceFlake

Update: Prints the passed variable itself now.