HUMP - yet another set of helpers

Showcase your libraries, tools and other projects that help your fellow love users.
User avatar
vrld
Party member
Posts: 917
Joined: Sun Apr 04, 2010 9:14 pm
Location: Germany
Contact:

Re: HUMP - yet another set of helpers

Post by vrld »

New feature: Signals and Slots (aka Observer Pattern)

For my Ludum Dare 23 entry I needed some sort messaging system. I considered using kikito's beholder.lua, but it seemed (no offense) just a bit too bloated and complex for my purposes.

A basic implementation of signals and slots is easy enough to do yourself. This is what I came up with (formatting changed to emphasize the brevity):

Code: Select all

local reg = setmetatable({}, {__index = function(t,k) local u = {}; rawset(t, k, u); return u end})
return {
	emit     = function(s, ...) for f in pairs(reg[s]) do f(...) end end,
	register = function(s, f)   reg[s][f] = f; return f end,
	remove   = function(s, f)   reg[s][f] = nil end,
	clear    = function(s)      reg[s] = {} end,
}
With this you can do stuff like

Code: Select all

-- elements.lua
signals.register('sensor-on',  function() self.value = true end)
signals.register('sensor-off', function() self.value = false end)
-- bot.lua
if self.level:sense(p) then
	signals.emit('sensor-on')
else
	signals.emit('sensor-off')
end
which helps to decouple your code.

The above is still the basic core of hump.signal, but I added two nifty features:
  • Support for registry instances (akin to hump.timer).
  • Support for lua string patterns: You can emit, remove from and clear multiple signals that match a pattern.
The documentation can be found here. I hope you find this as useful as I did. ^^
I have come here to chew bubblegum and kick ass... and I'm all out of bubblegum.

hump | HC | SUIT | moonshine
User avatar
Charles Randall
Prole
Posts: 34
Joined: Wed May 09, 2012 2:52 pm

Re: HUMP - yet another set of helpers

Post by Charles Randall »

I used the hump libs for my TOJam entry, they were super helpful. Thumbs up!
User avatar
Roland_Yonaba
Inner party member
Posts: 1563
Joined: Tue Jun 21, 2011 6:08 pm
Location: Ouagadougou (Burkina Faso)
Contact:

Re: HUMP - yet another set of helpers

Post by Roland_Yonaba »

Nice work.
Out of scope, but just to mention, you should definitely fix that.
Attachments
test.jpg
test.jpg (66.81 KiB) Viewed 4401 times
User avatar
vrld
Party member
Posts: 917
Joined: Sun Apr 04, 2010 9:14 pm
Location: Germany
Contact:

Re: HUMP - yet another set of helpers

Post by vrld »

Charles Randall wrote:I used the hump libs for my TOJam entry, they were super helpful. Thumbs up!
Good to hear! If you ran into annoyances using hump or have any feature requests, let me know.
Roland_Yonaba wrote:you should definitely fix that.
Yeah, I should. Thanks for noticing. ;)
I have come here to chew bubblegum and kick ass... and I'm all out of bubblegum.

hump | HC | SUIT | moonshine
edover
Prole
Posts: 2
Joined: Mon Aug 06, 2012 4:39 am

Re: HUMP - yet another set of helpers

Post by edover »

I have a small question about the documentation (and related quoted post)
vrld wrote: Timer.Interpolator() behaves differently
Instead of returning <status>, <function values> it now returns the function values if the interpolation is still going on or nil, if the interpolation has finished. This may result in the interpolation function never receiving the value 1 as fraction argument (so you better don't rely on that to happen).
I've added the interpolation function to my update method like so:

Code: Select all

result = fader(dt,fadevalue)
where fader is:

Code: Select all

  fader = Timer.Interpolator(3,function(frac,a)
        alphacolor = frac*a
    end)
and the image this function is linked to is fading properly. The problem is that the variable 'result' is ALWAYS nil.

Am I missing something? I was hoping to monitor the value of result and use it as a callback to trigger another event once the interpolation was finished.
Are there any plans for official callback functions for timer events?
I'm sorry if this is just a stupid request, I've honestly never dealt with Lua before so this has been an amazing learning experience.
User avatar
kexisse
Citizen
Posts: 56
Joined: Wed Jun 13, 2012 2:52 pm

Re: HUMP - yet another set of helpers

Post by kexisse »

Quick question about Hump.Signal. It looks super-cool.

I notice there's an emit_pattern, that will trigger all signals that match a pattern.
But I didn't see a signal_pattern.

I might be misunderstanding how the signal pattern works because I haven't used it before, but wouldn't that be useful?

So you register a function to be called every time '^update%-.*" is called.
edover
Prole
Posts: 2
Joined: Mon Aug 06, 2012 4:39 am

Re: HUMP - yet another set of helpers

Post by edover »

Just in case anyone has a similar issue (though that's doubtful, since it turned out to be a stupidly simple fix) here was my problem.

My function wasn't returning anything and should have looked more like this:

Code: Select all

fader = Timer.Interpolator(3,function(frac,a)
        alphacolor = frac*a
        return frac*a
    end)
Thanks vrld!
User avatar
vrld
Party member
Posts: 917
Joined: Sun Apr 04, 2010 9:14 pm
Location: Germany
Contact:

Re: HUMP - yet another set of helpers

Post by vrld »

Updates! And a new (awesome) feature!
  • Fixed a bug in gamestates, where callbacks would not propagate return values, which leads to faulty behavior with love.quit().
  • camera.lua is now independent the vector-light.lua.
  • Added camera:lookAt(x,y), camera:pos() and camera:rotation([angle]) to make using it less frustrating.
  • Removed Timer.Interpolator() and Timer.Oscillator(). Usage of these two was weird and complicated and there are better ways to do these things (e.g tweener, tween.lua or simply by yourself).
  • Added Timer.do_for(delta, func[, after]), which calls func(dt) for the next delta seconds and optionally after() after that.
Timer.do_for() basically removes the need for awful and weird flags in love.update(). For example:
  • Play an animation for 5 seconds and then stop:

    Code: Select all

    Timer.do_for(5, function(dt) animation:update(dt) end)
  • Shake the camera when something explodes:

    Code: Select all

    local orig_x, orig_y = camera:pos()
    Timer.do_for(1, function()
        camera:lookAt(orig_x + math.random(-2,2), orig_y + math.random(-2,2))
    end, function()
        -- reset camera position
        camera:lookAt(orig_x, orig_y)
    end)
  • Make the player invincible for some time and flash the sprite to show that he is

    Code: Select all

    player.isInvincible = true
    local t = 0
    player.timer:do_for(3, function(dt)
        t = t + dt
        player.visible = (t % .2) < .1 -- player is visible for 0.1 seconds, then hidden for 0.1 seconds, ...
    end, function()
        -- make sure the player is visible after three seconds
        player.visible = true
        player.isInvincible = false
    end)
I have come here to chew bubblegum and kick ass... and I'm all out of bubblegum.

hump | HC | SUIT | moonshine
User avatar
Przemator
Party member
Posts: 107
Joined: Fri Sep 28, 2012 6:59 pm

Re: HUMP - yet another set of helpers

Post by Przemator »

I'm losing my mind here... Please help. When I write this:

Code: Select all

vector = require "hump.vector"
local a = vector(10, 20)
local b = vector(30, 40)
local dst = vector.dist(a, b)
I get an error: attempt to call a field 'dist' (a nil value)

What am I doing wrong?
User avatar
vrld
Party member
Posts: 917
Joined: Sun Apr 04, 2010 9:14 pm
Location: Germany
Contact:

Re: HUMP - yet another set of helpers

Post by vrld »

Przemator wrote:What am I doing wrong?
The correct way is to use the colon notation,

Code: Select all

a:dist(b)
See also the example in the documentation of vector:dist(other).
I have come here to chew bubblegum and kick ass... and I'm all out of bubblegum.

hump | HC | SUIT | moonshine
Post Reply

Who is online

Users browsing this forum: No registered users and 0 guests