Possibly useful code snippets

Showcase your libraries, tools and other projects that help your fellow love users.
Post Reply
Form
Prole
Posts: 3
Joined: Wed Jan 29, 2014 12:16 am

Possibly useful code snippets

Post by Form »

While writing games I often come across some cool things that you can do with Lua, so I figured why not post them here.

If anyone else wants to join in feel free to, but as a legal warning I would just like to state that all code I post in this thread will be under the Whatever the Fuck you please Permissive License (sorry for the vulgarity). Anyone posting in this thread should also include a, small, permissive license, too.

Code: Select all

local WAIT = coroutine.wrap(function(f, returnvar)
	local starttime = {}
	local duration = {}
	local counter = 0
	local timecheck = false

	local wordcount = 0
	for i in love.filesystem.lines("main.lua") do 
		if string.find(i, "WAIT%b()") then
			wordcount = wordcount + 1
		end
	end

	while #starttime < wordcount do
		local time = coroutine.yield()
		
		starttime[#starttime + 1] = love.timer.getTime()
		duration[#duration + 1] = time
	end
			
	while true do
		timecheck = false

		if love.timer.getTime() >= starttime[#starttime - counter] + duration[#duration - counter] then
			starttime[#starttime - counter] = love.timer.getTime()
			timecheck = returnvar or true
		end
		counter = counter + 1
		if counter >= #starttime then 
			counter = 0
		end
		coroutine.yield(timecheck)
	end
end)
This counts how many times WAIT() appears on your main.lua, so it will break if you use a call to it that doesn't exist in plain text on your main.lua. Besides that it works great.
User avatar
Karai17
Party member
Posts: 930
Joined: Sun Sep 02, 2012 10:46 pm

Re: Possibly useful code snippets

Post by Karai17 »

Lua's require function acts just like any normal function. You can pass in variables. require uses the "..." list of arguments so you can pass data into a file while loading it.

Code: Select all

local lib = require("some.lib", 123, "qwerty")
You'll notice that the first argument is the path to the lib in question. Assuming you only use the path and no other args, and your lib needs to call several other files (not just the init file), you can do a hilarious thing:

Code: Select all

-- main.lua
local lib = require("some.lib")

-- some/lib/init.lua
local path = ... .. "." -- "some.lib."

local other_thing = require(path .. "file")
... .. "." makes me laugh every time.
STI - An awesome Tiled library
LÖVE3D - A 3D library for LÖVE 0.10+

Dev Blog | GitHub | excessive ❤ moé
User avatar
CaptainMaelstrom
Party member
Posts: 163
Joined: Sat Jan 05, 2013 10:38 pm

Re: Possibly useful code snippets

Post by CaptainMaelstrom »

Recursive "find in table" function. Here, tbl is the table to look in, val is the value to find (can be a table, too), and depth is how deep to check for values, default being 1.

Code: Select all

function table.find(tbl,val,depth)
	local function table.equal(a,b)
		assert(type(a)=='table' and type(b)=='table', 'Input values must be tables')
		for k,v in pairs(a) do
			if b[k] ~= v then return false end
		end
		return true
	end
	assert(type(tbl)=='table' ,"First argument must be a table")
	assert(val ,"Second argument (value to find) must not be nil:" .. tostring(val) .. ' ' .. type(val))
	local depth = depth or 1		--how deep to go recursively
	local j
	if type(val)~='table' then
		for i,v in pairs(tbl) do
			if type(v)~='table' or depth==1 then
				if val==v then return i end
			else
				j = {table.find(tbl[i],val,depth-1)}
				if j then
					if next(j) then return i,unpack(j) end
				end
			end
		end
	else
		for k,v in pairs(tbl) do
			if type(v)=='table' then
				if table.equal(v,val) then
					return k
				else
					if depth > 1 then
						j = {table.find(v,val,depth-1)}
					end
					if j then
						if next(j) then return k,unpack(j) end
					end
				end
			end
		end
	end
	return nil
end
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: Possibly useful code snippets

Post by Robin »

That doesn't work, for several reasons. Have you ever tested it? Because I have doubts it would actually parse.
Help us help you: attach a .love.
User avatar
Roland_Yonaba
Inner party member
Posts: 1563
Joined: Tue Jun 21, 2011 6:08 pm
Location: Ouagadougou (Burkina Faso)
Contact:

Re: Possibly useful code snippets

Post by Roland_Yonaba »

Well, this is possibly something lots of people have already find out, or have better ways to do it.
I was writing something today, and I needed to pass a vararg to a function. This function was supposed to catch the first arg of the passed-in vararg, pass it to another function that would also return a function, and forward the rest of the args to the resulting function and return the latter call result.
Still with me ?

Without magic, I would have written this:

Code: Select all

local function foo(...)
  local arg = {...}   -- collect all args to a table
  local first_arg = arg[1] -- get the first arg
  table.remove(arg,1) -- remove it from the table
  local fcall = bar(first_arg) -- pass it to another func, returns a func named fcall
  return fcall(unpack(arg)) -- return the result of fcall with the rest of the args
end
I shortened it to the following:

Code: Select all

local function foo(...)
  local arg = {...}
  return bar(arg[1])(select(2,unpack(arg)))
end
In case someone has a better proposal, I'd love to see that :)
User avatar
bartbes
Sex machine
Posts: 4946
Joined: Fri Aug 29, 2008 10:35 am
Location: The Netherlands
Contact:

Re: Possibly useful code snippets

Post by bartbes »

I do!

Code: Select all

local function foo(a, ...)
    return bar(a)(...)
end
User avatar
Roland_Yonaba
Inner party member
Posts: 1563
Joined: Tue Jun 21, 2011 6:08 pm
Location: Ouagadougou (Burkina Faso)
Contact:

Re: Possibly useful code snippets

Post by Roland_Yonaba »

bartbes wrote:I do!
Meh...! I hate you bartbes :)
Beautiful. Elegant. Short. And simple. I'll take it. Thanks!
Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 0 guests