What techniques that everyone should know?

General discussion about LÖVE, Lua, game development, puns, and unicorns.
Cartread
Prole
Posts: 6
Joined: Sat May 19, 2012 8:17 pm

Re: What techniques that everyone should know?

Post 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
User avatar
OttoRobba
Party member
Posts: 104
Joined: Mon Jan 06, 2014 5:02 am
Location: Sao Paulo, Brazil

Re: What techniques that everyone should know?

Post by OttoRobba »

There is also the always useful variable swap!

Code: Select all

x,y = y,x
Which is a lot simpler than

Code: Select all

tempVariable = x
x = y
y = tempVariable
tempVariable = nil
User avatar
JovialFeline
Prole
Posts: 28
Joined: Wed Jan 08, 2014 2:32 pm

Re: What techniques that everyone should know?

Post 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"
User avatar
Roland_Yonaba
Inner party member
Posts: 1563
Joined: Tue Jun 21, 2011 6:08 pm
Location: Ouagadougou (Burkina Faso)
Contact:

Re: What techniques that everyone should know?

Post 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
davisdude
Party member
Posts: 1154
Joined: Sun Apr 28, 2013 3:29 am
Location: North Carolina

Re: What techniques that everyone should know?

Post by davisdude »

The LÖVE Blog (BLÖG?) also has some great techniques and such on it. I would recommend it. :)
GitHub | MLib - Math and shape intersections library | Walt - Animation library | Brady - Camera library with parallax scrolling | Vim-love-docs - Help files and syntax coloring for Vim
User avatar
Ref
Party member
Posts: 702
Joined: Wed May 02, 2012 11:05 pm

Re: What techniques that everyone should know?

Post 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! :joker:
User avatar
Jasoco
Inner party member
Posts: 3725
Joined: Mon Jun 22, 2009 9:35 am
Location: Pennsylvania, USA
Contact:

Re: What techniques that everyone should know?

Post by Jasoco »

Code: Select all

print(not not "Who's there?")

> true
Hehe.

"True" who?
User avatar
Tanner
Party member
Posts: 166
Joined: Tue Apr 10, 2012 1:51 am

Re: What techniques that everyone should know?

Post 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))
User avatar
substitute541
Party member
Posts: 484
Joined: Fri Aug 24, 2012 9:04 am
Location: Southern Leyte, Visayas, Philippines
Contact:

Re: What techniques that everyone should know?

Post 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.
Currently designing themes for WordPress.

Sometimes lurks around the forum.
Wojak
Party member
Posts: 134
Joined: Tue Jan 24, 2012 7:15 pm

Re: What techniques that everyone should know?

Post 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 ;)
Post Reply

Who is online

Users browsing this forum: Amazon [Bot] and 4 guests