function object:init(x, y, visible)
self.x = x or 64
self.y = y or 64
self.visible = visible or true
end
function object:draw()
if self.visible then
-- drawing code goes here
end
end
This all works in my head, but it looks like saying object:new(32, 32, false) will make Lua use the "or true", meaning that I can't have a default visibility set.
Am I just showing my newness to Lua or is there some other bug I'm not seeing? I'm a bit under the weather right now (home from work, even) so I won't be offended at all if someone points out a tremendously stupid mistake or obvious error.
Nope, that works exactly how I want it to. Now I just have to figure out how to say that in English, so that I can remember it and/or explain it to other people down the line.
vrld wrote:Maybe too late, but if you want your code to look hackish, you could do this:
I thought about that, but it doesn't work: the default should be true, not false. Therefore, passing nil should be the same as passing true, instead of passing false.
I like slime's solution best. Simple, elegant, non-obfuscating.