I’m just beginning to learn Defold and it seems to me that the message passing system, while good in a lot of ways, also results in a lot of boilerplate. I’m a JS/TS developer with no experience in lua yet so I may have missed something here.
It seems to me that scripts in Defold repeat the same pattern over and over. Script 1 has a piece of data and script 2 needs to know when it changes. This is the message passing pattern
- Script 1 creates a piece of data
- Something causes data to change on script 1, we write code that posts a message to script 2
- Script 2 implements on_message to handle the message about the changed data coming from script
This bit of boilerplate where script 1 explicitly passes a message to script 2 could be replaced much more cleanly with the observable pattern. That would look like:
- Script one creates an observable piece of data
- Script two (and possibly many others) observes that piece of data and is notified when it changes.
This is the pattern I use all the time in JS with Mobx (and mobx-state-tree). In JS with mobx the pattern is even nicer – mobx automatically tracks which pieces of data have been referenced in an observed function and re-runs it when any of that data changes. You don’t even have to specify which pieces of data to observe, it tracks that automatically and on first use it feels like magic but it’s also extremely efficient.
It seems like a mobx style observable pattern would be a perfect fit for Defold to reduce seemingly tedious message passing boilerplate. I’m new to Defold and lua so I’m probably missing something. Is there a reason this pattern isn’t used?