A simple publisher/subscriber library for LiveCode

Posted  

This is a simple library I made to use in a game we’re making where we needed a way for objects to interact with each other without hard coded object references. This pattern is known as the publisher/subscriber pattern or the observer pattern.

In this pattern objects can register themselves as listeners for specific events. Once an object triggers an event, all objects that we’re listening for such event will receive a callback message. You can use this pattern to allow controls to react to events in your program without the need to hard code dispatch/send messages. A good example is a progress bar that listens for a message that is triggered by a download operation. The download routine would trigger this message without the knowledge of the progress bar, it is just broadcasting it like: “hey, I am downloading something” and the progress bar will listen for this broadcast and react to it. The code could be like:

on openCard put the long id of scrollbar "progress" into tTarget addEventListener "downloadProgress", "downloadProgressCallback", tTarget libURLSetStatusCallback "urlstatus" load URL myURL with message "downloadComplete" end openCard on urlstatus pURL, pStatus switch item 1 of pStatus case "loading" put 0 into tDataA["start"] put item 3 of pStatus into tDataA["end"] put item 2 of pStatus into tDataA["progress"] trigger "downloadProgress", tDataA break end switch end urlstatus on downloadComplete ## this is just cosmetic to guarantee that ## the progress bar goes to 100% once the download ## is completed. put 0 into tDataA["start"] put 100 tDataA["end"] put 100 into tDataA["progress"] trigger "downloadProgress", tDataA end downloadComplete

In the given scrollbar script:

on downloadProgressCallback pDataA set the startValue of me to pDataA["start"] set the endValue of me to pDataA["end"] set the thumbpos of me to pDataA["progress"] end downloadProgressCallback

This way we can change the UI that reacts to the download without touching the urlstatus message implementation. We could have many controls listening for the downloadProgress event. Also check out how both the urlstatus message and the downloadComplete message trigger the downloadProgress event. None of these messages have any prior knowledge of whom is receiving this event and nothing is hardcoded.

Using such patterns, you can decouple your user interface from the inner workings of your application easier than before. In our little game we have stuff such as objects broadcasting events like “exploded” and “died” so that other objects can react. Since in games you tend to create and destroy objects at runtime a lot, this library makes our life easier.

This library is released in the public domain. Use it as you see fit, can’t sue me. To download this library, use the link on the sidebar (or link below if you’re using a mobile device).

Did you enjoyed reading this content? Want to support me?

You can buy me a coffee at ko-fi.

Comments? Questions? Feedback?

You can reach out to me on Twitter, or Mastodon, Secure Scuttlebutt, or through WebMentions.

Mentions