Angel 3.2
A 2D Game Prototyping Engine
Classes | Public Member Functions | Static Public Member Functions | Static Protected Attributes | List of all members
Switchboard Class Reference

The central class which handles delivery of Messages. More...

#include <Switchboard.h>

Public Member Functions

void Broadcast (Message *message)
 
void DeferredBroadcast (Message *message, float delay)
 
void Update (float dt)
 
const bool SubscribeTo (MessageListener *subscriber, const String &messageType)
 
const bool UnsubscribeFrom (MessageListener *subscriber, const String &messageType)
 
const std::set< MessageListener * > GetSubscribersTo (const String &messageName)
 
const StringSet GetSubscriptionsFor (MessageListener *subscriber)
 
void SendAllMessages ()
 

Static Public Member Functions

static SwitchboardGetInstance ()
 

Static Protected Attributes

static Switchboards_Switchboard = NULL
 

Detailed Description

This class is where all Messages pass through to get to their subscribers. It manages subscribers lists, delivery, and broadcast of messages.

Like the World, it uses the singleton pattern; you can't actually declare a new instance of a Switchboard. To access messaging in your world, use "theSwitchboard" to retrieve the singleton object. "theSwitchboard" is defined in both C++ and Lua.

If you're not familiar with the singleton pattern, this paper is a good starting point. (Don't be afraid that it's written by Microsoft.)

http://msdn.microsoft.com/en-us/library/ms954629.aspx

Definition at line 55 of file Switchboard.h.

Member Function Documentation

Switchboard & Switchboard::GetInstance ( )
static

Used to access the singleton instance of this class. As a shortcut, you can just use "theSwitchboard".

Returns
The singleton

Definition at line 42 of file Switchboard.cpp.

void Switchboard::Broadcast ( Message message)

Send a Message to all the MessageListeners who have subscribed to Messages of that particular name. All Messages are sent at the end of the current frame, outside the Update loop. (Which means you can safely remove objects from the World in response to a Message.)

Parameters
messageThe message to send

Definition at line 51 of file Switchboard.cpp.

void Switchboard::DeferredBroadcast ( Message message,
float  delay 
)

Lets you send a Message after a designated delay. Oftentimes you want something to happen a little while after an event, and it can be be easier to simply defer the sending of the Message rather than make the MessageListener responsible for implementing the delay.

Parameters
messageThe message to send
delayAmount of time (in seconds) to wait before sending

Definition at line 56 of file Switchboard.cpp.

void Switchboard::Update ( float  dt)

Takes the same form as the Renderable::Update function, but Switchboard is not a Renderable. This function gets called by the World to let the Switchboard know about the passage of time so it can manage deferred broadcasts.

Parameters
dtThe amount of time elapsed since the last frame

Definition at line 62 of file Switchboard.cpp.

const bool Switchboard::SubscribeTo ( MessageListener subscriber,
const String &  messageType 
)

Sign a MessageListener up to receive notifications when Messages of a specific class are broadcast through the Switchboard.

Parameters
subscriberThe MessageListener to sign up
messageTypeThe name of the Message it's interested in
Returns
True if the MessageListener was successfully subscribed – could be false if the subscription was attempted while messages were being delivered (in which case the subscription will start when this round of delivery is done) or if the MessageListener was already subscribed to Messages of that name.

Definition at line 80 of file Switchboard.cpp.

const bool Switchboard::UnsubscribeFrom ( MessageListener subscriber,
const String &  messageType 
)

Lets a MessageListener stop receiving notifications of specific name. MessageListeners automatically unsubscribe from all their Messages when their destructors are called, so you don't have to worry about this when destroying an object; this would only be called directly in user code when you no longer care about a particular Message.

Parameters
subscriberThe MessageListener that doesn't want to get these Messages anymore
messageTypeThe name of the Message they're tired of hearing about
Returns
True if the MessageListener was successfully unsubscribed – could be false if the unsubscription was attempted while messages were being delivered (in which case the subscription will be removed when this round of delivery is done) or if the MessageListener was not subscribed to Messages of that name.

Definition at line 93 of file Switchboard.cpp.

const std::set< MessageListener * > Switchboard::GetSubscribersTo ( const String &  messageName)

Get a list of all MessageListeners subscribed to Messages with a given name.

Parameters
messageNameThe Message you care about
Returns
A std::set of objects subscribed

Definition at line 120 of file Switchboard.cpp.

const StringSet Switchboard::GetSubscriptionsFor ( MessageListener subscriber)

Get a list of all Message subscriptions for a certain MessageListener

Parameters
subscriberThe MessageListener you care about
Returns
A StringSet of all their subscriptions

Definition at line 132 of file Switchboard.cpp.

void Switchboard::SendAllMessages ( )

Immediately sends all Messages to the appropriate subscribers. Called by the World at the end of each frame; you shouldn't call this directly in your game code.

Definition at line 144 of file Switchboard.cpp.


The documentation for this class was generated from the following files: