Message Passing System?

General discussion about LÖVE, Lua, game development, puns, and unicorns.
Redshft
Prole
Posts: 12
Joined: Mon Apr 09, 2012 7:21 pm

Message Passing System?

Post by Redshft » Mon Apr 09, 2012 7:26 pm

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?

User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: Message Passing System?

Post by Robin » Mon Apr 09, 2012 9:37 pm

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.
Help us help you: attach a .love.

Redshft
Prole
Posts: 12
Joined: Mon Apr 09, 2012 7:21 pm

Re: Message Passing System?

Post by Redshft » Mon Apr 09, 2012 9:59 pm

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.

User avatar
kikito
Inner party member
Posts: 3153
Joined: Sat Oct 03, 2009 5:22 pm
Location: Madrid, Spain
Contact:

Re: Message Passing System?

Post by kikito » Mon Apr 09, 2012 10:19 pm

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

Redshft
Prole
Posts: 12
Joined: Mon Apr 09, 2012 7:21 pm

Re: Message Passing System?

Post by Redshft » Mon Apr 09, 2012 10:36 pm

Ah, Thanks Kikito! Thanks what I was looking for. This is much appreciated. Code is so much cleaner with message passing. :awesome:

User avatar
Inny
Party member
Posts: 652
Joined: Fri Jan 30, 2009 3:41 am
Location: New York

Re: Message Passing System?

Post by Inny » Tue Apr 10, 2012 12:23 am

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.

Redshft
Prole
Posts: 12
Joined: Mon Apr 09, 2012 7:21 pm

Re: Message Passing System?

Post by Redshft » Tue Apr 10, 2012 3:27 am

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.

User avatar
kraftman
Party member
Posts: 277
Joined: Sat May 14, 2011 10:18 am

Re: Message Passing System?

Post by kraftman » Tue Apr 10, 2012 11:21 am

I'm not sure how:

Code: Select all

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

User avatar
Nixola
Inner party member
Posts: 1940
Joined: Tue Dec 06, 2011 7:11 pm
Location: Italy

Re: Message Passing System?

Post by Nixola » Tue Apr 10, 2012 12:18 pm

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
lf = love.filesystem
ls = love.sound
la = love.audio
lp = love.physics
lt = love.thread
li = love.image
lg = love.graphics

User avatar
kikito
Inner party member
Posts: 3153
Joined: Sat Oct 03, 2009 5:22 pm
Location: Madrid, Spain
Contact:

Re: Message Passing System?

Post by kikito » Tue Apr 10, 2012 12:55 pm

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

Post Reply

Who is online

Users browsing this forum: No registered users and 306 guests