[library] Attachment - A Callback Helper Utility for LÖVE

Showcase your libraries, tools and other projects that help your fellow love users.
Apprehentice
Prole
Posts: 5
Joined: Tue Mar 03, 2015 8:07 pm
Contact:

[library] Attachment - A Callback Helper Utility for LÖVE

Post by Apprehentice »

Introduction
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()
For simplicity, you may want to do this instead:

Code: Select all

local attachment = require("attachment")()
Attaching
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)
The attach function returns the function that was added so that you may detach it later, if necessary.

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)
This will have Attachment run through the object and register anything that looks like a LÖVE callback.

Detaching
To detach a function from an event, do

Code: Select all

attachment:detach("event name", function)
This will run through the event called "event name" and remove the given function if it is found.

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"
Other Environments
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)
This can be useful if, for example, you don't want Attachment to consume LÖVE's callbacks.

Code: Select all

local attachment = require("attachment")({})
License
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
Last edited by Apprehentice on Tue Mar 10, 2015 7:15 pm, edited 2 times in total.
Muris
Party member
Posts: 131
Joined: Fri May 23, 2014 9:18 am

Re: [library] BootyCall - A Callback Helper Utility for LÖVE

Post by Muris »

not to sound mean, but what does this do different than creating a function to a variable?

I mean:

Code: Select all

function a()
  print("hello")
end
b=a
b() -- prints hello
I mean that is literally what it feels that your hook does from the post. Assigns a function to a variable.Maybe I am missing a point. I only looked at your examples without going deeper.
Apprehentice
Prole
Posts: 5
Joined: Tue Mar 03, 2015 8:07 pm
Contact:

Re: [library] BootyCall - A Callback Helper Utility for LÖVE

Post by Apprehentice »

Muris wrote:not to sound mean, but what does this do different than creating a function to a variable?

I mean:

Code: Select all

function a()
  print("hello")
end
b=a
b() -- prints hello
I mean that is literally what it feels that your hook does from the post. Assigns a function to a variable.Maybe I am missing a point. I only looked at your examples without going deeper.
The difference here is that this library will add your function to a list so that it can be called when the associated hook is called, so if you did

Code: Select all

bootycall:hook("update", function()
  print("hello")
end)

bootycall:hook("update", function()
  print("world")
end)
it would call the first function and then the second every time love.update is called.
No, it's not entirely necessary, but personally, I prefer hooks to singular callbacks, being introduced to Lua through Garry's Mod.
User avatar
ArchAngel075
Party member
Posts: 319
Joined: Mon Jun 24, 2013 5:16 am

Re: [library] BootyCall - A Callback Helper Utility for LÖVE

Post by ArchAngel075 »

I personally use hooks and events myself in my games since i use a moddable self made engine of sorts,
Dynamically added objects or "mods" by users can hook into the drawing and update function of a engine using a update_event or draw_event
Also helps when objects themselves generate events that ill be called, or key events etc. The advantage i find is that a mod can specify new events (onNewGame, onLoadMap, onPacketRCV -for multiplayer codes-) which makes modding much much easier than having to hardcode the hooks myself.
User avatar
T-Bone
Inner party member
Posts: 1492
Joined: Thu Jun 09, 2011 9:03 am

Re: [library] BootyCall - A Callback Helper Utility for LÖVE

Post by T-Bone »

Can I be rude enough to suggest changing the name to something more family friendly?
Apprehentice
Prole
Posts: 5
Joined: Tue Mar 03, 2015 8:07 pm
Contact:

Re: [library] BootyCall - A Callback Helper Utility for LÖVE

Post by Apprehentice »

T-Bone wrote:Can I be rude enough to suggest changing the name to something more family friendly?
Do you have any suggestions?
davisdude
Party member
Posts: 1154
Joined: Sun Apr 28, 2013 3:29 am
Location: North Carolina

Re: [library] BootyCall - A Callback Helper Utility for LÖVE

Post by davisdude »

You could call it something like "attached" is something like that, if you're trying to make it love themed, since people in love can become overly attached.
As a bonus, you can call hook/unhook attach/detach.
not to contribute to the "bad" library names, but "hooker" also comes to mind...
GitHub | MLib - Math and shape intersections library | Walt - Animation library | Brady - Camera library with parallax scrolling | Vim-love-docs - Help files and syntax coloring for Vim
Apprehentice
Prole
Posts: 5
Joined: Tue Mar 03, 2015 8:07 pm
Contact:

Re: [library] BootyCall - A Callback Helper Utility for LÖVE

Post by Apprehentice »

davisdude wrote:You could call it something like "attached" is something like that, if you're trying to make it love themed, since people in love can become overly attached.
As a bonus, you can call hook/unhook attach/detach.
not to contribute to the "bad" library names, but "hooker" also comes to mind...
Sounds great! Submit an issue on the repo and I'll make the changes.
User avatar
TsT
Party member
Posts: 161
Joined: Thu Sep 25, 2008 7:04 pm
Location: France
Contact:

Re: [library] Attachment - A Callback Helper Utility for LÖV

Post by TsT »

Hello,

I forked your library and rewrite some part of code. See my pull request

I also made my own lib to manage LÖVE callbacks : love modular
I use a different approach. I define a passive format to export a module : We can create simple love module without my library.
My library read the "export" structure to get all callbacks and do all the appropriate actions to setup LÖVE callbacks.

Regards,
My projects current projects : dragoon-framework (includes lua-newmodule, lua-provide, lovemodular, , classcommons2, and more ...)
Apprehentice
Prole
Posts: 5
Joined: Tue Mar 03, 2015 8:07 pm
Contact:

Re: [library] Attachment - A Callback Helper Utility for LÖV

Post by Apprehentice »

TsT wrote:Hello,

I forked your library and rewrite some part of code. See my pull request

I also made my own lib to manage LÖVE callbacks : love modular
I use a different approach. I define a passive format to export a module : We can create simple love module without my library.
My library read the "export" structure to get all callbacks and do all the appropriate actions to setup LÖVE callbacks.

Regards,
Thanks, my approach was a bit sloppy. I also fixed that broken assert.
Post Reply

Who is online

Users browsing this forum: No registered users and 5 guests