Module m.broadcast

Module to simplify sending of a message to multiple receivers

Usage:

    -- script_a.script
    
    local broadcast = require "ludobits.m.broadcast"
    
    function init(self)
    	-- this script should react to "foo" and "bar" messages
    	broadcast.register("foo")
    	broadcast.register("bar", function(message, sender)
    		-- handle message
    	end)
    end
    
    function final(self)
    	broadcast.unregister("foo")
    	broadcast.unregister("bar")
    end
    
    function on_message(self, message_id, message, sender)
    	if broadcast.on_message(message_id, message, sender) then
    		-- message was handled
    		return
    	end
    	if message_id == hash("foo") then
    		-- handle message "foo"
    	end
    end
    
    
    -- script_b.script
    
    local broadcast = require "ludobits.m.broadcast"
    
    function update(self, dt)
    	if some condition then
    		-- broadcast a "foo" message to anyone listening
    		broadcast.send("foo")
    	end
    end
    

Functions

send (message_id, message) Send a message to all registered receivers
register (message_id, on_message_handler) Register the current script as a receiver for a specific message
unregister (message_id) Unregister the current script from receiving a previously registered message
on_message (message_id, message, sender) Forward received messages in scripts where the broadcast module is used and where registered messages have also provide a message handler function.


Functions

send (message_id, message)
Send a message to all registered receivers

Parameters:

  • message_id
  • message
register (message_id, on_message_handler)
Register the current script as a receiver for a specific message

Parameters:

  • message_id
  • on_message_handler Optional message handler function to call when a message is received. The function will receive the message and sender as it's arguments. You must call on_message for this to work
unregister (message_id)
Unregister the current script from receiving a previously registered message

Parameters:

  • message_id
on_message (message_id, message, sender)
Forward received messages in scripts where the broadcast module is used and where registered messages have also provide a message handler function. If no message handler functions are used then there is no need to call this function.

Parameters:

  • message_id
  • message
  • sender

Returns:

    true if the message was handled
generated by LDoc 1.4.6 Last updated 2017-11-27 06:48:33