Here's a function that will transform whatever you pass it into a table that will hopefully be understandable by a human. It'll take any table or other Lua value, so you can, for example, print it:
print(ins({1,2,3,foo=bar}),1,1) printh(ins({1,2,3,foo=bar})) |
The only parameter besides the string to be transformed is the depth.
local deep={l1={l2={l3={l4="!"}}}}} print(ins(deep)) --prints everything print(ins(deep,2)) --stops at l2 and prints {_} |
As the author of the inspect.lua library, I use it a lot on my day job when I need to debug Lua, and I really miss it when debugging in Pico-8. I found some previous efforts on this direction, but I was missing a lot:
- Marking repeated tables, functions, threads and userdata with numbers
- Handling nested tables / avoiding printing repeated tables
- Sorting keys sensibly
- Printing metatable data
- Presenting arrays as comma-separated single lines, and hashes in multiple-span lines
I made ins' output take less horizontal space than inspect.lua, so it adapts better to PICO-8's output. And I trimmed features(string escaping, preprocessing).
Still, at ~700 tokens, it's ... a chunky boy. You probably shouldn't add it to your project permanently. Think of it as a "debug artillery" function: deploy it to a tab in your game, it should be able to handle whatever you throw at it and return a digestible string. Once you have used it to destroy your problem, remove it.
PS: My biggest surprise was that PICO-8 has no built-in sorting function. So I bundled one. Oh and there is no concat, either. The garbage collector must be having fun with all those strings :)
EDIT: fixed bug in isidentifier function
[Please log in to post a comment]