Scripting/Debugging Tools

Not a help request, but a contribution that the community may find useful. I’ve written a module that contains some useful routines that help in debugging, table manipulation, and so on… So far, there’s only four, but I plan to add more when the need arises.

--
--
-- A collection of useful utility routines.
--
--
local utilityMod = {}


-- Recursively copies a table into a new table and
-- returns it.
utilityMod.revursiveCopy = function(dataTable)
	local tableCopy = {}
	for index, value in pairs(dataTable) do
		if type(value) == "table" then
			value = utilityMod.recursiveCopy(value)
		end
		tableCopy[index] = value
	end
	return tableCopy
end

-- Merges data from table2 into table1, overwriting values that
-- share the same index.  Returns the merged table.  The input
-- tables are not modified.
utilityMod.mergeTable = function(table1, table2)
	local tableCopy = {}
	tableCopy = utilityMod.revursiveCopy(table1)
	for index, value in pairs(table2) do
		tableCopy[index] = value
	end
	return tableCopy
end

-- Prints out a table.
utilityMod.printTable = function(tableData)
	table.foreach(tableData, function(key, value)
		print(key, "=", value)
	end)
end

-- Similar to print table, but prints it recursively.  Nested
-- tables are indented.  Tab is used for recursive indenting.
-- Do not use tab when calling.
utilityMod.printTableRecursive = function(tableData, tab)
	if tab == nil then
		tab = ""
	end
	for key, value in pairs(tableData) do
		if type(value) == "table" then
			print(string.format(tab) .. ">> Subtable: ", key)
			utilityMod.printTableRecursive(value, tab .. "\t")
		else
			print(string.format(tab), key, "=", value)
		end
	end
end

return utilityMod
1 Like

You should put this in #resources:community-resources

1 Like

Should be useful for some, though im sure most devs who end up needing something like this will have already created their own version. (The table deep copy stuff).

Also you can print the table directly, the output has support for tables that lets you expand it, at least for studio anyway.

Btw you forgot to return the tableCopy in the Merge function, just thought I’d let ya know to fix that :slight_smile:

1 Like

Thanks for pointing that out. I’m not sure how I missed that. I would have found it eventually though.