Share your favourite helper functions

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
User avatar
Nsmurf
Party member
Posts: 191
Joined: Fri Jul 27, 2012 1:58 am
Location: West coast.

Re: Share your favourite helper functions

Post by Nsmurf »

More snippets on my blog!

This time point-object collisions. They're fairly simple, but still quite useful functions.
OBEY!!!
My Blog
UE0gbWUgd2l0aCB0aGUgd29yZCAnSE1TRycgYXMgdGhlIHN1YmplY3Q=
User avatar
substitute541
Party member
Posts: 484
Joined: Fri Aug 24, 2012 9:04 am
Location: Southern Leyte, Visayas, Philippines
Contact:

Re: Share your favourite helper functions

Post by substitute541 »

I got (physics, and electricity) based snippets on my blog.
Currently designing themes for WordPress.

Sometimes lurks around the forum.
User avatar
vrld
Party member
Posts: 917
Joined: Sun Apr 04, 2010 9:14 pm
Location: Germany
Contact:

Re: Share your favourite helper functions

Post by vrld »

Proxy tables for lazy loading of resources:

Code: Select all

local function Proxy(f)
	return setmetatable({}, {__index = function(t,k)
		local v = f(k)
		t[k] = v
		return v
	end})
end
Doesn't look like much, but is very, very useful:

Code: Select all

State = Proxy(function(path) return require('states.' .. path) end)
Gamestate.switch(State.menu)

Image = Proxy(function(path) return love.graphics.newImage('img/'..path..'.png') end)
love.graphics.draw(Image.powerup, x,y)

-- nested proxy
Font  = Proxy(function(arg)
	if tonumber(arg) then return love.graphics.newFont(arg) end
	return Proxy(function(size) return love.graphics.newFont('font/'..arg..'.ttf', size) end)
end)
love.graphics.setFont(Font[20])
love.graphics.setFont(Font.slkscr[25])
Interrupt:

Code: Select all

function Interrupt(info)
	local old = {}
	local base = info.__base or love
	info.__base = nil
	for k,v in pairs(info) do
		old[k]  = base[k]
		base[k] = function(...)
			return v(old[k], ...)
		end
	end

	return function()
		for k,v in pairs(info) do
			base[k] = old[k]
		end
	end
end
To make a simple pause screen:

Code: Select all

local continue -- this part is a little weird though :/
continue = Interrupt{
	update = function() end,
	keypressed = function(_, key)
		if key == 'p' then continue() end
	end,
	draw = function(draw)
		draw()
		love.graphics.setColor(0,0,0,200)
		love.graphics.rectangle('fill', 0,0, SCREEN_WIDTH, SCREEN_HEIGHT)
		love.graphics.setColor(120, 255, 120)
		love.graphics.setFont(Font.slkscr[40])
		love.graphics.printf('PAUSE', 0,SCREEN_HEIGHT/2-20, SCREEN_WIDTH, 'center')
	end
}
I have come here to chew bubblegum and kick ass... and I'm all out of bubblegum.

hump | HC | SUIT | moonshine
User avatar
Roland_Yonaba
Inner party member
Posts: 1563
Joined: Tue Jun 21, 2011 6:08 pm
Location: Ouagadougou (Burkina Faso)
Contact:

Re: Share your favourite helper functions

Post by Roland_Yonaba »

Useful indeed. I used a similar function to the nested proxy (for font loading) in my assets loader. Which was, after all, inspired by your Proxy function.

Inspiraception ?
stout
Citizen
Posts: 64
Joined: Sun Oct 07, 2012 4:42 pm

Re: Share your favourite helper functions

Post by stout »

I don't know if this counts, and there is probably a better way to do this, but

Code: Select all

local total = 0 -- initialize/clear the variable
for rowIndex, row in ipairs(worldMap) do
	for columnIndex, quadID in ipairs(row) do
		if quad ~= 1 then
			total = total + 1 -- increase total for every non-ocean tile
		end
	end
end
if total > 50 then -- if there at least this many non-ocean tiles then allow next step
-- next step(s)
I'd been wanting to come up with a way of determining if a certain percentage of tiles in the tilemap were non-ocean/"blank", and this is how I did it. In this case my ocean tiles are 1, and 2, 3, etc are other things. Originally I was going to go through and add the tiles together, but then realized that higher-value tiles would throw that off. Could easily be adapted for string-based tilemaps, and the total check could be made into a variable percentage rather than a static number.
Last edited by stout on Sat Nov 24, 2012 5:18 pm, edited 1 time in total.
User avatar
Ubermann
Party member
Posts: 146
Joined: Mon Nov 05, 2012 4:00 pm

Re: Share your favourite helper functions

Post by Ubermann »

This is something like an alarm clock.

It basically counts the time passed and "ticks" when passed XXXX seconds.

Its made of two functions: the first to count the time, the second to watch the time passed:

VARIABLES NEEDED:

Code: Select all

oneTick = 0 -- time will be added to this
tickHappened = false -- when a tick happens, set this to true
alarm = 1 -- set alarm to 1 second
COUNT THE TIME ( usually in love.update() ):

Code: Select all

function countTime()
    oneTick = oneTick + love.timer.getDelta() -- ad time passed to our counter
    watchTicks() -- this function will watch the ticks and tell you when a tick passed
end
WATCH TICKS AND ALARM ( usually in your basic app functions .lua ):

Code: Select all

function watchTicks()
    if oneTick >= alarm then -- if time passed is > or = to alarm then...
        tickHappened = true -- ring the bell
        oneTick = 0 -- restart tick time counter
    else
        tickHappened = false -- if time passed is not > or = to the alarm, then the tick didn't happen
    end
end

Pretty simple algorithm but really useful.
Also remember that the "alarm" is only active for one deltaTime, so if you need to do some events after XXXX time, but only once before the next event, this is perfect for you.
User avatar
Ref
Party member
Posts: 702
Joined: Wed May 02, 2012 11:05 pm

Re: Share your favourite helper functions

Post by Ref »

Cubic spline fit of data points.
Nothing original. Just an adaption to Lua.

Code: Select all

function cubicSpline( tab, nsteps )
	-- Original Basic version by Charles O'Neill 2002
	-- 'tab' is original table of { x, y } values
	-- 'x' values don't have to be uniformly spaced
	-- 'spline' is the returned table of calculated {x,y} pairs
	local nsteps = nsteps or 10		-- number of points-1 created btw initial points
	local A, B, C, D, h, S = {}, {}, {}, {}, {}, {}
	for i = 1, #tab-1  do			-- width of each 'x' interval
		h[i] = tab[i+1][1] - tab[i][1]
		end
	for i = 1, #tab - 2 do
		A[i] = h[i+1]
		B[i] = h[i]
		C[i] = 6 * (( tab[i+2][2] - tab[i+1][2])/h[i+1] - (tab[i+1][2] - tab[i][2]) / h[i])
		D[i] = 2 * ( h[i] + h[i+1] )
		end
	ntdma = #tab - 2				-- tdma = Tre-Diagonal-Matrix-Algorithm
	for i = 2, ntdma do
		local R = B[i] / D[i-1]
		C[i] = C[i] - R * C[i-1]
		D[i] = D[i] - R * A[i-1]
		end
	C[ntdma] = C[ntdma] / D[ntdma]
	for i = ntdma - 1, 1, -1 do
		C[i] = (C[i] - A[i] * C[i+1]) / D[i]
		end
	for i = 2, #tab - 1 do			-- changes from zero based index to 1
		S[i] = C[i-1]		-- S is the second derivative
		end
	S[1], S[#tab] = 0, 0			-- set end coordinates
	for i = 1, #tab - 1 do			-- calculate cubic coordinates
		A[i] = (S[i+1] - S[i]) / (6 * h[i] )
		B[i] = S[i] / 2
		C[i] = (tab[i+1][2] - tab[i][2]) / h[i] - (2 * h[i] * S[i] + h[i] * S[i+1]) / 6
		D[i] = tab[i][2]
		end
	local spline, count = {}, 1
	for i = 1, #tab - 1 do
		for j = 1, nsteps do
			local x		= tab[i][1] + ( h[i] / nsteps ) * ( j - 1 )
			local dif		= x - tab[i][1]
			local y	        = A[i] * dif ^ 3 + B[i] * dif ^ 2 + C[i] * dif + D[i]
			spline[count]    = { floor(x), floor(y) }
			count		= count + 1
			end
		end
	spline[ #spline + 1 ] = { tab[ #tab ][ 1 ], tab[ #tab ][ 2 ] } -- adds last point to table
	return spline
	end
Edited: as per Robin's comments
Last edited by Ref on Sun Nov 25, 2012 11:07 pm, edited 2 times in total.
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: Share your favourite helper functions

Post by Robin »

FYI,
Ref wrote:

Code: Select all

	local A = {} B = {} C = {} D = {} h = {} S = {}
Ref wrote:

Code: Select all

		j = i - 1 -- three times!
Ref wrote:

Code: Select all

		R = B[i] / D[i-1]
B, D, D, h, S, j and R are all globals here.
Help us help you: attach a .love.
User avatar
Ref
Party member
Posts: 702
Joined: Wed May 02, 2012 11:05 pm

Re: Share your favourite helper functions

Post by Ref »

Robin wrote:FYI,B, D, D, h, S, j and R are all globals here.
Thanks Robin for pointing out my error.
Question:
Are a,b,c local if you use local a,b,c = {},{},{} or do you have to specify each one local separately?
User avatar
Inny
Party member
Posts: 652
Joined: Fri Jan 30, 2009 3:41 am
Location: New York

Re: Share your favourite helper functions

Post by Inny »

vrld wrote:Proxy tables for lazy loading of resources:

Code: Select all

local function Proxy(f)
	return setmetatable({}, {__index = function(t,k)
		local v = f(k)
		t[k] = v
		return v
	end})
end
Just out of curiosity, why did you use "Proxy" to name that function instead of Memoize?
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot] and 92 guests