Hi!
I wanted to share with you a library I’m working with for easy and safe messaging in my Defold projects. At first, I was using Dispatcher by Critique Gaming and Pigeon is its evolution.
It was meant to be my birthday gift to the community, I am only late few days, because, you know, documentation! (at least it is extensive and comprehensive, I hope!)
Pigeon is very easy to use, and you can directly replace all your msg.post with it even, but it offers of course much more, main advantages are:
- Easy to use subscription system
- Safe checks for defined messages (it can check if data contains proper keys and type is correct)
- Comes with all Defold system messages defined (so you won’t be able to send a wrong message using it)
- You can define your own messages as well. Defining message data is optional.
- You can send messages to all subscribers or to specific urls directly (while still checking data corectness, if the message is defined)
- You can use strings or hashes. (All strings are pre-hashed internally anyway thanks to amazing Defold-Hashed by @sergey.lerg ).
- It comes with documentation, examples and some functional tests!
- I use the module in Witchcrafter!
Simplest usage:
pigeon.subscribe("test_message") -- subscribe where you want the message to be received
pigeon.send("test_message") -- send to all subscribers
With message definition (data is verified when sending, so receivers can assume they always get correct data and no additional check have to be done in on_message):
pigeon.define("test_message", {test_value = "string"}) -- define message to require table with at least one key "test_value" of type "string"
pigeon.send("test_message", {test_value = "Hello World!"}) -- message data is verified before sending
Replace Defold’s msg.post
with pigeon.send_to
:
pigeon.send_to(msg.url(), "test_message", {test_value = "Hello World!"})
-- is same as:
msg.post(msg.url(), "test_message", {test_value = "Hello World!"})
Then in on_message you can receive messages as usual.
For 'in-depth-ers", there is letters
module that defines letters (messages) to be checked by Pigeon. You can directly extend this module, instead of defining every message with pigeon.define() in runtime
EDIT:
Submitted to Asset Portal
Also, I wonder what do you think about setting dependency to log, but explicitly, by user?
I use log everywhere and for the purpose of this library I removed this dependency (but left Defold-Hashed, as it is really small and essential actually). You can replace internal logging system, that by default uses just print()
function with log module by @Pkeod with call:
local log = require "log.log"
pigeon.set_dependency_module_log(log)
And it will be printing data with correct level, tag and could also save logs in file, depending on how you set up log
I already have reworked also DefSave and am thinking about using the same idea for logging there. I looked up this idea a little bit from Monarch, where logging is enabled, by actually assigning function print
to internal log, otherwise nothing is happening (logs are disabled).
As logging is generally a good part of any library, this could be a unified way of adding dependency for logging.
What are your thoughs? Are you using log or other modules for logging?