Callback hooks.

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
User avatar
Kaze
Party member
Posts: 189
Joined: Sat Jul 19, 2008 4:39 pm
Location: Dublin, Ireland

Callback hooks.

Post 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
User avatar
rude
Administrator
Posts: 1052
Joined: Mon Feb 04, 2008 3:58 pm
Location: Oslo, Norway

Re: Callback hooks.

Post 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.
Attachments
callback.love
(1.12 KiB) Downloaded 378 times
User avatar
conman420
Prole
Posts: 33
Joined: Sun Aug 31, 2008 8:46 pm

Re: Callback hooks.

Post by conman420 »

That would be incredibly useful if you just included that with love.
emonk
Prole
Posts: 24
Joined: Tue Aug 12, 2008 11:43 am

Re: Callback hooks.

Post 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
Sarcasm - just one of the services I offer.
Green_Hell
Citizen
Posts: 94
Joined: Thu Feb 21, 2008 1:11 am

Re: Callback hooks.

Post 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.
>>I love LÖVE.<<
User avatar
schme16
Party member
Posts: 127
Joined: Thu Oct 02, 2008 2:46 am

Re: Callback hooks.

Post by schme16 »

rude can I get a repost of the callbacks thing, the forum at the attachment; thanks mate!
My Development Diary - http://shanegadsby.info
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: Callback hooks.

Post by Robin »

TheLinx has his own hook thingy, you might be interested in that.
Help us help you: attach a .love.
User avatar
kikito
Inner party member
Posts: 3153
Joined: Sat Oct 03, 2009 5:22 pm
Location: Madrid, Spain
Contact:

Re: Callback hooks.

Post 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)
When I write def I mean function.
Post Reply

Who is online

Users browsing this forum: Bing [Bot], Google [Bot] and 64 guests