Page 3 of 8
Re: What techniques that everyone should know?
Posted: Sat Feb 15, 2014 11:41 pm
by Cartread
Using dt:
Code: Select all
Operator can be an [int] -- 1 = 1 second (dt+dt+dt)
dtCountDown = dtCountDown - dt
Body acts with [dt]
end
Do something once before dt hits an amount:
Code: Select all
if dtCountDown > 0 then
if dtCountDown <= dt -- last call
else
end
Colors:
Code: Select all
MIDDLE_GRAY= {128,128,128,255}
thingDraw(x,y,MIDDLE_GRAY)
Code: Select all
function Critter:thingDraw(x,y,colorIn)
love.graphics.setColor(colorIn)
end
Save/Load tables:
http://lua-users.org/wiki/SaveTableToFile
Re: What techniques that everyone should know?
Posted: Wed Feb 19, 2014 4:49 am
by OttoRobba
There is also the always useful variable swap!
Which is a lot simpler than
Code: Select all
tempVariable = x
x = y
y = tempVariable
tempVariable = nil
Re: What techniques that everyone should know?
Posted: Wed Feb 19, 2014 1:13 pm
by JovialFeline
A small trick for quickly toggling a boolean. For not thinking of it myself, I felt like a fool when I first stumbled on it.
Code: Select all
local isFuzzy = false
isFuzzy = not isFuzzy
print( tostring(isFuzzy) ) -- outputs "true"
Re: What techniques that everyone should know?
Posted: Wed Feb 19, 2014 4:56 pm
by Roland_Yonaba
I use a similar trick,
not not <value>, to convert
any type to a boolean. A truthy value would return true, a falsy value would return false.
Code: Select all
print(not not {}) --> true
print(not not nil) --> false
print(not not false) --> false
print(not not true) --> true
print(not not 1) --> true
print(not not function() end) --> true
print(not not '') --> true
Re: What techniques that everyone should know?
Posted: Thu Feb 20, 2014 2:37 am
by davisdude
The
LÖVE Blog (BLÖG?) also has some great techniques and such on it. I would recommend it.
Re: What techniques that everyone should know?
Posted: Thu Feb 20, 2014 6:14 pm
by Ref
Roland_Yonaba wrote:I use a similar trick,
not not <value>, to convert
any type to a boolean. A truthy value would return true, a falsy value would return false.
Code: Select all
print(not not {}) --> true
print(not not nil) --> false
print(not not false) --> false
print(not not true) --> true
print(not not 1) --> true
print(not not function() end) --> true
print(not not '') --> true
Couldn't resist!
Not not, who's there.
When not to use not not!
Re: What techniques that everyone should know?
Posted: Thu Feb 20, 2014 6:54 pm
by Jasoco
Code: Select all
print(not not "Who's there?")
> true
Hehe.
"True" who?
Re: What techniques that everyone should know?
Posted: Mon Feb 24, 2014 9:42 pm
by Tanner
I can't think of anywhere that I've used this with the Love2D api but there have been times when something requests a Lua callback and passes that callback something not super useful. For example, the
VBA lua scripting interface function for `memory.registerwrite` passes the memory address to the callback as opposed to the value written to the memory address. So having a function that proxies a better callback with that is passed the result of a common operation can result in much better function signatures.
Code: Select all
function readbyte_proxy(callback)
return function(address)
callback(memory.readbyte(address), address)
end
end
function print_direction(direction)
print("direction", direction)
end
memory.registerwrite(memory_addresses.character_direction, readbyte_proxy(print_direction))
Re: What techniques that everyone should know?
Posted: Tue Feb 25, 2014 9:36 am
by substitute541
Prevent your game from spazzing when you move the window:
Code: Select all
-- in love.update(dt)
dt = math.max(dt, 0.033333333)
Basically adds a lower limit of 30 fps to the framerate.
Re: What techniques that everyone should know?
Posted: Tue Feb 25, 2014 11:22 am
by Wojak
Inverting an array – useful if you have an array that don't change to often, but you need to check it's content very often:
Code: Select all
local isValidOryginal = {'text1','text2','text3'}
locla isValid = {}
for i=1, # isValidOryginal do -- every time isValidOryginal changes (once if it never changes)
isValid[isValidOryginal[i]] = true
end
isValidOryginal = nil -- optional if isValidOryginal never changes
--checking content:
if isValid['text1'] then --this is true
-- do stuff
end
if isValid['text4'] then-- this is nil (false)
-- do stuff
end
And it works with non string types as well