Skip to content

Publisher

Hyomoto edited this page Jun 24, 2021 · 15 revisions
Jump To Go Back Arguments Methods Variables

Publisher( channels... )

Publishers are message processing backends. Methods are bound to the channels, and called with the supplied parameters when a channel is notified they belong to. These methods are called at the scope they are bound, which means they are run as if they were a part of the subscribing instance. Thus a single instance can subscribe to multiple channels, or even the same channel multiple times if desired. NOTE: Publisher makes use of dynamic resources, and so should always be destroyed when no longer needed to prevent memory leaks which will slow down and eventually crash your game.

var _achievement	= new Publisher()

_publisher.subscriber_add( "kills", function( _number ) {
  global.kills_progress	+= _number;

  if ( global.kills_progress > 1000 ) {
    steam_set_achievement("kills1000");
  }
});
_achivement.channel_notify( "kills", 1 );

Arguments

Name Type Purpose
channels... string optional: If provided, will create channels with the given names.

Methods

Jump To top channel_notify channel_add channel_remove subscriber_add subscriber_remove destroy toString

channel_notify( channel, message )

Name Type Purpose
channel string The name of the channel to notify.
message mixed The message to pass to the the subscriber methods.

The channel, if it exists, will call all of its subscriber functions with message as an argument.


channel_add( channel )

Name Type Purpose
channel string The name of the channel to create.

Creates a new channel if it doesn't exist, and ignores this if it does.


channel_remove( channel )

Name Type Purpose
channel string The name of the channel to remove.

Destroys a channel if it exists, and ignores this if it does not.


subscriber_add( channel, method )

Returns: Subscriber

Name Type Purpose
channel string The channel to subscribe to.
method func The method that will be called when this channel is alerted.

Adds the given method to the specified channel. If the channel doesn't exist, it will be created.


subscriber_remove( channel, subscriber )

Name Type Purpose
channel string The channel to remove the subscriber from.
subscriber Subscriber The subscriber to remove from the channel.

Removes the given subscriber from the specified channel. The Subscriber is what is returned when the channel is subscribed to. ote: It is good practice to remove a subscriber when they are destroyed. Failure to do so could cause the program to crash unexpectedly.


destroy( )

Name Type Purpose
None

Cleans up the internal structures this Publisher can be safely garbage-collected.


toString( )

Name Type Purpose
None

Returns a string showing all the channels and their number of subscribers. Used for debugging purposes.


Variables

Jump To top
Name Type Initial Purpose
channels undef ds_map_create() No description.
Clone this wiki locally