Page 8 of 15

Re: HUMP - yet another set of helpers

Posted: Wed May 16, 2012 1:53 pm
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. ^^

Re: HUMP - yet another set of helpers

Posted: Wed May 16, 2012 2:07 pm
by Charles Randall
I used the hump libs for my TOJam entry, they were super helpful. Thumbs up!

Re: HUMP - yet another set of helpers

Posted: Wed May 16, 2012 2:12 pm
by Roland_Yonaba
Nice work.
Out of scope, but just to mention, you should definitely fix that.

Re: HUMP - yet another set of helpers

Posted: Wed May 16, 2012 6:13 pm
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. ;)

Re: HUMP - yet another set of helpers

Posted: Mon Aug 06, 2012 4:52 am
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.

Re: HUMP - yet another set of helpers

Posted: Tue Aug 07, 2012 7:41 am
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.

Re: HUMP - yet another set of helpers

Posted: Thu Aug 09, 2012 4:21 pm
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!

Re: HUMP - yet another set of helpers

Posted: Sun Oct 07, 2012 12:02 pm
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)

Re: HUMP - yet another set of helpers

Posted: Fri Nov 02, 2012 11:17 am
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?

Re: HUMP - yet another set of helpers

Posted: Fri Nov 02, 2012 11:35 am
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).