Page 1 of 1

Possibly useful code snippets

Posted: Sun Jun 01, 2014 1:14 am
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.

Re: Possibly useful code snippets

Posted: Sun Jun 01, 2014 1:20 am
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.

Re: Possibly useful code snippets

Posted: Tue Jun 03, 2014 6:09 pm
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

Re: Possibly useful code snippets

Posted: Wed Jun 04, 2014 1:13 pm
by Robin
That doesn't work, for several reasons. Have you ever tested it? Because I have doubts it would actually parse.

Re: Possibly useful code snippets

Posted: Sat Jun 07, 2014 9:48 am
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 :)

Re: Possibly useful code snippets

Posted: Sat Jun 07, 2014 9:51 am
by bartbes
I do!

Code: Select all

local function foo(a, ...)
    return bar(a)(...)
end

Re: Possibly useful code snippets

Posted: Sat Jun 07, 2014 11:43 am
by Roland_Yonaba
bartbes wrote:I do!
Meh...! I hate you bartbes :)
Beautiful. Elegant. Short. And simple. I'll take it. Thanks!