Asset - Scripts [PUBSUB] xPublisher is yet another pubsub/broadcast system (FREE)

xDGameStudios

GameMaker Staff
GameMaker Dev.
[LINKS]

xPublisher (marketplace)

xPublisher (itch.io)

[API IMAGE]

[INFORMATION]


This is a Pub/Sub implementation for GMS2.3+
Did you ever need to send messages between instances?
Do you need your weather system to send a message to your entities saying it started raining?
Do you need your player to publish actions that will be handle by another managing system? (ie.: achievements)

[QUICK GUIDE]

1) You need to instantiate a Publisher, using the following code:
GML:
publisher = new Publisher();
publisher.registerChannel("test"); // registers a communication channel
2) Subscribe to a channel within that publisher...
GML:
testCallback = function(_params) { show_debug_message(_params) }
subscription = publisher.subscribe("test", testCallback, false);
3) Finally you can publish to that channel with a custom parameter and all subscribers will receive that message
GML:
publisher.publish("test", "Say hello world!!");
4) When you need to remove a subscription you can rely on the weak reference system that will automatically remove subscription to dead instances/structs OR if GMS2 GC doesn't kick in just cancel the subscription manually using:
GML:
subscription.cancel();
// or
publisher.unsubscribe(subscription);
NOTE: manual subscription removal is more reliable​

[USAGE]

This library provides a set of useful function that you can use to send messages through channels however you want.

GML:
publisher = new Publisher();
  
publisher.registerChannel(channel) // Registers a new communication channel
publisher.publish(channel, params) // Will publish a message (params) to that channel (triggers all channel subs)
subscription = publisher.subscribe(channel, callback, releaseRef) // Subscribes to a channel with a callback method
publisher.unsubscribe(subscription) // Removes a given subscription.
publisher.clearChannel(channel) // Removes all subscriptions to a channel
The subscription instance itself provides a set of utility functions that can be used to manipulate the event after it was scheduled:

GML:
subscription.cancel() // Cancels the current subscription.
[NOTES]

The demo project serves as an example for the Publisher system and all its code is inside the Demo folder of the asset pack. You don't need to include it to use the asset.

[COMPATIBILITY]

The asset is fully compatible with 2.3+ and is purely written in GML making it compatible with all exports available.
 
Last edited:
Top