Page 1 of 2

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

Posted: Tue Mar 03, 2015 8:35 pm
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

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

Posted: Wed Mar 04, 2015 8:54 am
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.

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

Posted: Wed Mar 04, 2015 4:37 pm
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.

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

Posted: Wed Mar 04, 2015 5:52 pm
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.

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

Posted: Sat Mar 07, 2015 6:13 am
by T-Bone
Can I be rude enough to suggest changing the name to something more family friendly?

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

Posted: Sat Mar 07, 2015 6:34 pm
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?

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

Posted: Sat Mar 07, 2015 7:35 pm
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...

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

Posted: Sat Mar 07, 2015 7:48 pm
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.

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

Posted: Mon Mar 16, 2015 5:56 pm
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,

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

Posted: Wed Mar 18, 2015 1:05 am
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.