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?
Message Passing System?
- 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.
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.
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
- Location: Madrid, Spain
- 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.
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.
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:
And then example usage:
I used to be a Qt programmer, so it's how I think.
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
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" )
Re: Message Passing System?
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.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:
And then example usage: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
I used to be a Qt programmer, so it's how I think.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" )
Re: Message Passing System?
I'm not sure how:
works, since signal is a table?
Code: Select all
ruffian:signal( "zomg" )
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
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
ls = love.sound
la = love.audio
lp = love.physics
lt = love.thread
li = love.image
lg = love.graphics
- kikito
- Inner party member
- Posts: 3153
- Joined: Sat Oct 03, 2009 5:22 pm
- Location: Madrid, Spain
- Contact:
Re: Message Passing System?
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)".
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.
Who is online
Users browsing this forum: Ahrefs [Bot], pgimeno and 3 guests