Helpful scripts

General discussion about LÖVE, Lua, game development, puns, and unicorns.
Post Reply
User avatar
Sheepolution
Party member
Posts: 264
Joined: Mon Mar 04, 2013 9:31 am
Location: The Netherlands
Contact:

Helpful scripts

Post by Sheepolution »

Sometimes a script is too simple for you to turn into a library, or maybe they are depended on another library's features. But that doesn't mean we should keep them for ourselves. What are some scripts that have really helped you and is since then in almost all your projects?

I'll start with a few examples:

Asset (uses Classic, like most of my scripts)

Code: Select all

local Asset = Object:extend()

local function stripString(base, s)
	local a = s:sub(base:len()+2,s:len())
	return a:sub(0, a:find("%.")-1)
end


local function loadFiles(base, dir, files)
	dir = dir or base
	local items = love.filesystem.getDirectoryItems(dir)
	for i,v in ipairs(items) do
		local file = dir .. "/" .. v
		if love.filesystem.isFile(dir .. "/" .. v) then
			files[
			stripString(base, file)
		 	] = file
		else
			loadFiles(base, dir .. "/" .. v, files)
		end
	end

	return files
end


function Asset:new()
	self.imgCache = {}
	self.imgDirs = loadFiles("images", nil,{})
end

function Asset:image(url, force)
	if not self.imgDirs[url] then error('The image "' .. url .. '" does not exist!') end
	local img
	if force or not self.imgCache[url] then
		img = love.graphics.newImage(self.imgDirs[url])
		self.imgCache[url] = img
	else
		img = self.imgCache[url]
	end
	return img
end


function Asset:__tostring()
	return "Asset"
end


return Asset
Allows me to do this:

Code: Select all

img = Asset:image("nameOfImage")

-- Instead of...
img = love.graphics.newImage("images/nameOfImage.png")
And for input (uses Classic and Lume):

Code: Select all

local Input = Object:extend()

function Input:new()
	self._pressed = {}
	self._released = {}
	self._custom = {}
end


function Input:reset()
	self._pressed = {}
	self._released = {}
end


function Input:isPressed(...)
	return lume.any({...}, function (a) return self._custom[a] and lume.any(self._custom[a], function (b) return lume.find(self._pressed, b) end) end)
end


function Input:isReleased(...)
	return lume.any({...}, function (a) return self._custom[a] and lume.any(self._custom[a], function (b) return lume.find(self._released, b) end) end)
end


function Input:isDown(...)
	return lume.any({...}, function (a) return self._custom[a] and lume.any(self._custom[a], function (b) return love.keyboard.isDown(b) end) end)
end


function Input:set(name, t)
	self._custom[name] = t
end


function Input:inputpressed(input)
	table.insert(self._pressed, input)
	if not self._custom[input] then
		self._custom[input] = {input}
	end
end


function Input:inputreleased(input)
	table.insert(self._released, input)
end


function Input:__tostring()
	return "Input | pressed: " .. (unpack(self._pressed) or " ") .. ", released: " .. (unpack(self._released) or " ")
end


return Input
Which allows me to do this:

Code: Select all

Input:set("jump",{"space","up","w"})
Input:isPressed("jump")
Last edited by Sheepolution on Sat Jul 04, 2015 3:01 pm, edited 3 times in total.
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: Helpful scripts

Post by Robin »

You're not setting the cache, tho.
Help us help you: attach a .love.
User avatar
Sheepolution
Party member
Posts: 264
Joined: Mon Mar 04, 2013 9:31 am
Location: The Netherlands
Contact:

Re: Helpful scripts

Post by Sheepolution »

Robin wrote:You're not setting the cache, tho.
Oops, looks like that was an old version actually. Thanks for the tip.
User avatar
Inny
Party member
Posts: 652
Joined: Fri Jan 30, 2009 3:41 am
Location: New York

Re: Helpful scripts

Post by Inny »

I'm gradually evolving a underscore/lodash/moses style utility-belt library. I periodically upload it to this gist: https://gist.github.com/inmatarian/5ea8da95491b25c8c362
Post Reply

Who is online

Users browsing this forum: No registered users and 8 guests