Page 1 of 2

Message Passing System?

Posted: Mon Apr 09, 2012 7:26 pm
by Redshft
Hello everyone,
I'm new to the Love engine, so please bear with me.

Does love.event support custom events? Such as messages being passed between game objects? If I had a rocket that exploded, could I use love.event to send a damage command to all objects in the blast radius?

I see that there is a function called love.event.push(e, a,b,c,d), would I use this to accomplish message passing?

Re: Message Passing System?

Posted: Mon Apr 09, 2012 9:37 pm
by Robin
I think it's possible, but I wouldn't do it if I were you. It adds a dependency on an implementation detail. How love.event works could very well change in future versions of LÖVE, and then you have a problem for your game if you want to upgrade.

Besides, it can't be hard to implement something nice for message passing in Lua, without touching the LÖVE internals at all.

Re: Message Passing System?

Posted: Mon Apr 09, 2012 9:59 pm
by Redshft
Right, that was my second thought. I have an implementation in C++ that I could convert to Lua when I learn enough about the language.

Re: Message Passing System?

Posted: Mon Apr 09, 2012 10:19 pm
by kikito
Just in case it helps, beholder.lua can do the message passing part (it doesn't do the "limit the listeners to an area around the player" part).

Re: Message Passing System?

Posted: Mon Apr 09, 2012 10:36 pm
by Redshft
Ah, Thanks Kikito! Thanks what I was looking for. This is much appreciated. Code is so much cleaner with message passing. :awesome:

Re: Message Passing System?

Posted: Tue Apr 10, 2012 12:23 am
by Inny
Just a side note, Beholder seems to have a global state associated with it, which may or may not be what you want. I myself prefer non-globals whenever possible, so I generally do something like this (or which metatables) when I want an observer pattern:

Code: Select all

function newSignal()
  local actors = {}
  return {
    send = function(...)
      for _, actor in ipairs(actors) do
        actor(...)
      end
    end,
    register = function( actor )
      table.insert( actors, actor )
    end
  }
end
And then example usage:

Code: Select all

goblin = {}
function goblin:slot( message )
  print("goblin received", message)
end

ruffian = {}
ruffian.signal = newSignal()
ruffian.signal.register( function(message) goblin:slot(message) end )

ruffian:signal( "zomg" )
I used to be a Qt programmer, so it's how I think.

Re: Message Passing System?

Posted: Tue Apr 10, 2012 3:27 am
by Redshft
Inny wrote:Just a side note, Beholder seems to have a global state associated with it, which may or may not be what you want. I myself prefer non-globals whenever possible, so I generally do something like this (or which metatables) when I want an observer pattern:

Code: Select all

function newSignal()
  local actors = {}
  return {
    send = function(...)
      for _, actor in ipairs(actors) do
        actor(...)
      end
    end,
    register = function( actor )
      table.insert( actors, actor )
    end
  }
end
And then example usage:

Code: Select all

goblin = {}
function goblin:slot( message )
  print("goblin received", message)
end

ruffian = {}
ruffian.signal = newSignal()
ruffian.signal.register( function(message) goblin:slot(message) end )

ruffian:signal( "zomg" )
I used to be a Qt programmer, so it's how I think.
I'm just on chapter 7 of the Lua book, so I haven't gotten to OOP and how it works in LUA. So I don't entirely understand this.

Re: Message Passing System?

Posted: Tue Apr 10, 2012 11:21 am
by kraftman
I'm not sure how:

Code: Select all

ruffian:signal( "zomg" )
works, since signal is a table?

Re: Message Passing System?

Posted: Tue Apr 10, 2012 12:18 pm
by Nixola
Probably signal's metatable has a __call field, that represents the function that gets called when you call signal
http://www.lua.org/pil/13.html

Re: Message Passing System?

Posted: Tue Apr 10, 2012 12:55 pm
by kikito
I think inni meant ruffian.signal.send( "zomg" ).

Nevertheless, it is possible to create a "callable" table in Lua using metatables. That's probably what he meant with "(or which metatables)".