## 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?

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?

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

### Re: Message Passing System?

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.

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

### Re: Message Passing System?

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.

kikito
Inner party member
Posts: 3153
Joined: Sat Oct 03, 2009 5:22 pm
Contact:

### Re: Message Passing System?

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?

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

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

### Re: Message Passing System?

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 )
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?

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 )
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.

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

### Re: Message Passing System?

I'm not sure how:

Code: Select all

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

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

### Re: Message Passing System?

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
li = love.image
lg = love.graphics

kikito
Inner party member
Posts: 3153
Joined: Sat Oct 03, 2009 5:22 pm