Have you ever found yourself chugging through the tedium that is callbacks? It seems that every library wants a piece of your program loop and you have to give it to them or nothing will work. For this reason, I wrote a handy utility I like to call Attachment!
What is it?
Attachment is a callback helper utility for LÖVE that allows you to easily attach to other libraries and objects, indiscriminately, and do what matters most!
Usage
Shamelessly ripped from the readme
To use Attachment, require it into your project, create an event handler, and attach some functions to it.
Creating an Event Handler
Creating a Attachment event handler is as simple as calling the Attachment table.
Code: Select all
local attachment = require("attachment")
local handler = attachment()
Code: Select all
local attachment = require("attachment")()
The function for attaching functions is used like this
Code: Select all
attachment:attach("event name", function)
Code: Select all
local attachment = require("attachment")()
attachment:attach("draw", function()
love.graphics.setFont(love.graphics.newFont(12))
love.graphics.setColor(0, 255, 0)
love.graphics.print("Hello, World!", 0, 0)
end)
With Attachment, you can also add all of an object or module's callbacks to a Attachment event handler by calling attachment:attachObject
Code: Select all
local attachment = require("attachment")()
local othermodule = require("othermodule")
attachment:attachObject(othermodule)
Detaching
To detach a function from an event, do
Code: Select all
attachment:detach("event name", function)
Calling
You can register your own events with Attachment if you'd like. To do so, simply attach a function to the desired event and then call the event somewhere in your code.
For example:
Code: Select all
local attachment = require("attachment")()
attachment:attach("foo", function() print("bar") end)
attachment:call("foo") -- Outputs "bar"
If, for some reason, your main environment is not love, you can pass your environment to Attachment as the first argument when creating your Attachment event handler.
Code: Select all
local environment = require("environment")
local attachment = require("attachment")(environment)
Code: Select all
local attachment = require("attachment")({})
Attachment is licensed under the MIT License. See the LICENSE file distributed with the library for details.
Download
Download as a Zip
View on GitHub