Page 1 of 1

LÖVE HÖÖKS

Posted: Mon Jul 06, 2015 8:19 am
by Nicholas Scott
I'm currently working on( Or more or less have a very good idea and some implementation ideas ) a hook system, it will allow you to add functions to be called whenever a love function is called. This can be extremely useful if you want to add something to a current function without overwriting what is already in the function. I will represent a small little example below, this is something quite similar to GMOD's hook system( if you're familiar with that, if not you should go check it out ).

This would add onto the beginning of the list of functions to call in the function love.update()

Code: Select all

local LV = {}

function LV.Update( dt )
	AVariableHere = AVariableHere + dt
end

hook.add( "loveupdate", "NicksUpdateAddition1", LV.Update )
This would overwrite the main function of the love.update()

Code: Select all

local LV = {}

local function LV.UpdateMain( dt )
	Server.UpTime = Server.UpTime + dt
end

hook.overWrite( "loveupdate", LV.UpdateMain )
The idea behind this is allowing more options, I myself have personally seen this as something that would be very useful in many cases. I'm going to make this regardless of what is said in this thread. However whether I release it to the public to use is up to you, do you think this is a good idea and should be posted on the forums for use by public, Please vote on the poll and/or leave a comment below of any ideas you might have for this! Thanks everyone :D -Nick

Re: LÖVE HÖÖKS

Posted: Mon Jul 06, 2015 3:21 pm
by airstruck
Am I doing it right?

Code: Select all

local function hook (name, callback)
    local f = love[name]
    love[name] = function (...)
        callback(...)
        return f(...)
    end
end

-- example
local foo = 0
hook('update', function (dt)
    foo = foo + dt
end)

Re: LÖVE HÖÖKS

Posted: Mon Jul 06, 2015 3:31 pm
by davisdude
@time thief:
Yours would work until you wanted to start removing hooks.

Re: LÖVE HÖÖKS

Posted: Mon Jul 06, 2015 3:35 pm
by Nicholas Scott
davisdude wrote:...start removing hooks.
He..Hehehehehehhehe You're a mind reader you know that ;) I've been working on this off and on last night( as I couldn't sleep cause I'm sick ;~; ) and I have hook.add( lovefunction, identifier, functionToBeCalled) and hook.remove( identifier ) aswell as a hook for every built in love. module/function. love.update, love.draw, etc. If you want I can post it as early access 1.0? If I work on this prominently and it becomes something useful I may just incorporate it into my own LOVE CONNECTION as it would be very useful for where my module needs to have net:update() called in the update, this would be very easy with hooks :D

Re: LÖVE HÖÖKS

Posted: Mon Jul 06, 2015 3:55 pm
by airstruck
Ahh yeah, removing them will be more of a pain. How's this?

Code: Select all

local hooks = {}

local function initHooks (name)
    if hooks[name] then return end
    local f = love[name]
    hooks[name] = {}
    love[name] = function (...)
        for k, callback in ipairs(hooks[name]) do
            callback(...)
        end
        return f(...)
    end
end

local function hook (name, callback)
    initHooks(name)
    local hookList = hooks[name]
    hookList[#hookList + 1] = callback
    return { name, callback }
end

local function unhook (nameCallbackPair)
    local name = nameCallbackPair[1]
    local callback = nameCallbackPair[2]
    local hookList = hooks[name]
    for k, v in ipairs(hookList) do
        if v == callback then
            table.remove(hookList, k)
            return
        end
    end
end

-- example
local foo = 0

local fooCounter = hook('update', function (dt)
    foo = foo + dt
end)

-- later...

unhook(fooCounter)