Page 1 of 1

Callback hooks.

Posted: Mon Aug 11, 2008 1:40 am
by Kaze
We need a way to "hook" to the update, draw, mousepress, etc callbacks.. As to make additional scripts more simple to use/implement.. Something like this:

Code: Select all

function updateB()

end
love.system.hook("update", updateB)
The console script, for example, needs to have

Code: Select all

Console:draw()
in the draw callback, where it could have just been included, if we had hooks..

I'm requesting this as I've run into the same situation, I've made a slider-type control, which works quite well, besides the fact that it's kindof a bitch to use right now (As you'll see below).

Code: Select all

love.filesystem.include("sliders.lua")
function load()
	Slider = love.controls.slider(100, 100)
end
function update(dt)
	Slider:Update(dt)
end
function draw()
	Slider:Draw()
end
function mousepressed(x, y, button)
	Slider:MousePressed(x, y, button)
end
function mousereleased(x, y, button)
	Slider:MouseReleased(x, y, button)
end
Image

Re: Callback hooks.

Posted: Mon Aug 11, 2008 5:05 pm
by rude
This can be done in pure Lua. See the source of the attatched .love.

Warning: a global callback must be present before load() is called, otherwise LÖVE will deem it not present and never call it.

Re: Callback hooks.

Posted: Tue Sep 02, 2008 6:01 pm
by conman420
That would be incredibly useful if you just included that with love.

Re: Callback hooks.

Posted: Tue Sep 02, 2008 8:47 pm
by emonk
I think the idea is that Love provides the stuff that is difficult/slow to do from Lua, and we do the rest. Providing a hooking mechanism for Update, Draw, etc. is simple enough to do in Lua.

Personally I'd rather have control over it from my script than have to rely on the engine to get it right all of the time. I can write a hooking system that tracks the various widgets required for different game states and have the appropriate stuff showed when the game state changes.

That said... there's nothing stopping me from having my hooking system hooked into the Love's hooking system :D

Re: Callback hooks.

Posted: Wed Sep 03, 2008 7:10 pm
by Green_Hell
emonk wrote:I can write a hooking system that tracks the various widgets required for different game states and have the appropriate stuff showed when the game state changes.
Could you eventually write a Tutorial with a sample game. I'd really love to see the example explained. I can't figure it out myself and I'm not experienced enough to understand it in anybody's code.

Thanks.

Re: Callback hooks.

Posted: Tue Jul 06, 2010 12:21 pm
by schme16
rude can I get a repost of the callbacks thing, the forum at the attachment; thanks mate!

Re: Callback hooks.

Posted: Tue Jul 06, 2010 1:02 pm
by Robin
TheLinx has his own hook thingy, you might be interested in that.

Re: Callback hooks.

Posted: Tue Jul 06, 2010 8:57 pm
by kikito
Disclaimer: What follows is shameless self-promotion.

PÄSSION has a hook-mechanism, called Beholder. It can work independently from PÄSSION, as long as you "register the events". This is done automatically when you require passion/init.lua, but you can do it yourself like this:

Code: Select all

 -- Showing only presses below. Releases are done exactly the same way
function love.keypressed(key)
  Beholder.trigger('keypressed_' .. key) -- Notice that I use Beholder.trigger, not Beholder:trigger here
end

function love.mousepressed(x, y, button)
  Beholder.trigger('mousepressed_' .. button, x, y)
end

function love.joystickpressed(joystick, button)
  Beholder.trigger('joystickpressed_' .. joystick .. '_' .. button)
end
Beholder can work outside PÄSSION, but it needs MiddleClass. This is how you use it with a class:

Code: Select all

Player = class('Player')
Player:include(Beholder)  -- Includes beholder on the class. Adds methods 'observe' and 'stopObserving'
function Player:initialize(x,y)
  super.initialize(self)
  self:setPosition(x,y)
  self:observe('keypressed_up', 'jump') -- binds the "jump" method to the "up" keypress
end
function Player:jump()
  ... -- do the jump
end
There's also a method for stopping observation of an event (stopObserving). It is usually a good idea to invoke it before destroying an object.

Note that you can also define your own custom events: 'level_finished', 'player_died' or 'pause', for example.

In short:

Code: Select all

   -- trigger the event
   Beholder.trigger(eventName, ...)
   -- observe one event (in a method)
   self:observe(eventName, funtionOrMethodName, ...)
   -- stop observing that event
   self:stopObserving(eventName)