Skip to content

Instantly share code, notes, and snippets.

@evaporei
Created July 26, 2024 17:43
Show Gist options
  • Save evaporei/833f11028bbe7f7cfb13386680212023 to your computer and use it in GitHub Desktop.
Save evaporei/833f11028bbe7f7cfb13386680212023 to your computer and use it in GitHub Desktop.
dump tables love2d
local Game = require('game')
local game = Game.new()
local function count_all(f)
local seen = {}
local count_table
count_table = function(t)
if seen[t] then return end
f(t)
seen[t] = true
for k,v in pairs(t) do
if type(v) == "table" then
count_table(v)
elseif type(v) == "userdata" then
f(v)
end
end
end
count_table(_G)
end
local global_type_table = nil
local function type_name(o)
if global_type_table == nil then
global_type_table = {}
for k,v in pairs(_G) do
global_type_table[v] = k
end
global_type_table[0] = "table"
end
return global_type_table[getmetatable(o) or 0] or "Unknown"
end
local function type_count()
local counts = {}
local enumerate = function (o)
local t = type_name(o)
counts[t] = (counts[t] or 0) + 1
end
count_all(enumerate)
return counts
end
function love.load()
game:setup()
end
function love.resize(w, h)
game:resize(w, h)
end
function love.keypressed(key)
game:handleKeyPressed(key)
end
local timer = 0
local run = true
local function dump(o)
if type(o) == 'table' then
local s = '{ '
for k,v in pairs(o) do
if type(k) ~= 'number' then k = '"'..k..'"' end
s = s .. '['..k..'] = ' .. dump(v) .. ','
end
return s .. '} '
else
return tostring(o)
end
end
function love.update(dt)
timer = timer + dt
if timer > 5 then
if run then
print(dump(type_count()))
run = false
end
end
game:update(dt)
end
function love.draw()
game:render()
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment