LÖVE HÖÖKS

General discussion about LÖVE, Lua, game development, puns, and unicorns.
Post Reply

Should this be posted after creation?

Yes
3
100%
No
0
No votes
Yes, but with some changes( Inform of what that is in the replies )
0
No votes
 
Total votes: 3

Nicholas Scott
Prole
Posts: 49
Joined: Sun Jun 07, 2015 9:12 am

LÖVE HÖÖKS

Post by Nicholas Scott » Mon Jul 06, 2015 8:19 am

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

User avatar
airstruck
Party member
Posts: 650
Joined: Thu Jun 04, 2015 7:11 pm
Location: Not being time thief.

Re: LÖVE HÖÖKS

Post by airstruck » Mon Jul 06, 2015 3:21 pm

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)

davisdude
Party member
Posts: 1154
Joined: Sun Apr 28, 2013 3:29 am
Location: North Carolina

Re: LÖVE HÖÖKS

Post by davisdude » Mon Jul 06, 2015 3:31 pm

@time thief:
Yours would work until you wanted to start removing hooks.
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

Nicholas Scott
Prole
Posts: 49
Joined: Sun Jun 07, 2015 9:12 am

Re: LÖVE HÖÖKS

Post by Nicholas Scott » Mon Jul 06, 2015 3:35 pm

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

User avatar
airstruck
Party member
Posts: 650
Joined: Thu Jun 04, 2015 7:11 pm
Location: Not being time thief.

Re: LÖVE HÖÖKS

Post by airstruck » Mon Jul 06, 2015 3:55 pm

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)

Post Reply

Who is online

Users browsing this forum: No registered users and 23 guests