benhumphreys wrote:I've found that building up a library of helper functions can really speed up programming and debugging in Löve/Lua.
I've called them "helper functions" in the title, but basically any techniques, modules, functions etc. that you use a lot and have saved you a lot of time would be great. Share them!
This is a great idea, however:
* It should be split into at least 3 threads:
1. Lua general purpose helpers.
2. Game-programming oriented lua helpers.
3. Löve-specific helpers.
I guess we should concentrate on 3., maybe 2., but there are probably resources on 2. on various web sites, no? For 1., the
Lua users' wiki is full of tips, examples, and "helpers" -- and other resources like libs.
* This can quickly inflate! to the point beeing unusable [*]. Maybe we could just quickly introduce helpers here (or in more specific threads) and from that build wiki pages with the material?
However, here are my first 2 contributions:
I always use "note" for debug output, that way I don't have to hunt for debug output lines in code when everything (endly) works fine (seems to).
And here's my printf:
Code: Select all
-- types that have a custom format (%d %f %s) (nil and boolean auto-convert)
local format_types = {["nil"]=true, boolean=true, number=true, string=true}
notef = function (fmt, ...)
--~ local items = table.pack(...)
local items = {...}
local item
for i = 1, #items do
item = items[i]
if not format_types[type(item)] then items[i] = tostring(item) end
end
print( format(fmt, unpack(items)) )
end
Usage:
Code: Select all
notef("%d | %8.3f | %s | %s | %s | %s",
1, -1.12345, "one", {1,2,3}, A{1,2,3}, function()end)
--> 1 | -1.123 | one | table: 0x9455e38 | A{1, 2, 3} | function: 0x949be48
''Note'': When LÖVE switches to Lua 5.2, we can get rid of tostring conversions, %s now auto-stringifies non-string arguments. Also, there is a highly useful "table.pack" func symetric to unpack (and which correctly handles nil's, unlike just surrouding with braces as in {...}).
Denis
[*] On my side, every "module" starts with "require stdlib" where stdlib is my personal Lua lib of helpers, which i'm just starting to recreate for the occasion of playing with Löve, and is already after a few days about 1000 LoC.
EDIT1: I also have write and writef too funcs which just do not add a newline; very handy to follow the behaviour of a sequence of code...
EDIT2: But my dream tool func is rather:
Code: Select all
local x = 1
note(x) --> "x : 1"
--> not just "1"
Option: add in parens the func name and line number of the "note" statement:
Code: Select all
f = function (x)
local x = 1
note(x) --> "x : 1 (f, 333)"
end