Log In  

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 {_}

Cart #ins_inspectforpico8-2 | 2021-10-18 | Code ▽ | Embed ▽ | License: CC4-BY-NC-SA
1

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

P#98806 2021-10-17 23:37 ( Edited 2021-10-18 20:34)


[Please log in to post a comment]

Follow Lexaloffle:          
Generated 2024-04-18 09:32:15 | 0.014s | Q:12