<< Back to Programming Forum   Search

Posts 1 - 4 of 4   
How to print any table when writing a mod: 8/4/2023 06:40:52


DanWL 
Level 63
Report
This prints any table (table, array and proxy objects).
The code is heavily modified from
https://stackoverflow.com/questions/41942289/display-contents-of-tables-in-lua#answer-41943392
https://www.warzone.com/wiki/Mod_API_Reference#Proxy_Objects
Thanks to Just_A_Dutchman_ https://www.warzone.com/Profile?p=42131172405&u=Just_A_Dutchman__1 for explaining why checking for proxyType on plain arrays gives error.

Usage:
tblprint(object);


Code:
local function p(obj, p2)
	function tprint(tbl, indent)
		if type(tbl) ~= 'table' then
			return tostring(tbl);
		end

		if not indent then
			indent = 0;
		end

		-- arrays dont have a proxy type
		if numKeys(tbl) ~= #tbl and tbl.proxyType then
			return DumpProxy(tbl, indent + 2);
		end

		local toprint = '{\r\n';
		indent = indent + 2;

		for k, v in pairs(tbl) do
			toprint = toprint .. string.rep(' ', indent)

			if type(k) == 'number' then
				toprint = toprint .. '[' .. k .. '] = ';
			elseif type(k) == 'string' then
				toprint = toprint  .. k ..  ' = ';
			end

			if type(v) == 'table' then
				if v.__proxyID then
					toprint = toprint .. DumpProxy(v, indent + 2) .. ',\r\n';
				else
					toprint = toprint .. tprint(v, indent + 2) .. ',\r\n';
				end
			elseif type(v) == 'string' then
				toprint = toprint .. '"' .. v .. '",\r\n';
			else
				toprint = toprint .. tostring(v) .. ',\r\n';
			end
		end

		toprint = toprint .. string.rep(' ', indent - 2) .. '}';

		return toprint;
	end

	function DumpProxy(obj, indent)
		if type(obj) ~= 'table' then
			return tostring(obj);
		end

		local str = '{';

		for _, key in pairs(obj.readableKeys) do
			if key ~= 'readableKeys' then
				str = str .. '\r\n' .. string.rep(' ', indent);
				if type(key) == 'string' then
					str = str .. key;
				else
					str = str .. '[' .. key .. ']';
				end

				str = str .. ' = ';

				local value = obj[key];

				if type(value) == 'table' then
					str = str .. tprint(value, indent);
				elseif type(value) == 'string' then
					str = str .. '"' .. value .. '"';
				else
					str = str .. tostring(value);
				end

				str = str .. ',';
			end
		end

		return str .. '\r\n' .. string.rep(' ', indent - 2) .. '}';
	end

	if type(obj) == 'table' then
		return tprint(obj, p2);
	else
		return tostring(obj);
	end
end

function tblprint(tbl)
	print(p(tbl));
end

function numKeys(tbl)
	local n = 0;

	for k, v in pairs(tbl) do
		n = n + 1;
	end

	return n;
end


Edited 8/4/2023 06:44:57
How to print any table when writing a mod: 8/5/2023 18:11:22


καλλιστηι 
Level 62
Report
Why does a print function have 100 lines?
How to print any table when writing a mod: 8/5/2023 18:48:29


DanWL 
Level 63
Report
Prints everything correctly indented and shows all keys and values, not just what the keys are.
How to print any table when writing a mod: 8/8/2023 23:27:04


JK_3 
Level 63
Report
Why does a print function have 100 lines?

Because all LUA containers are tables, and when you print a LUA table you get its memory address.

And everything the WZ api uses is tables inside tables inside tables, so its a pain to print anything useful.
Posts 1 - 4 of 4