Links

Use extension methods to check BlackboardNotes (or events)

This is a simple example, but it demonstrates how extension methods can make your classes smaller and easier to read.
As seen in the RPG demo project (CreationPortraitImages.cs & CreationExtensions.cs), when a BlackboardNote is passed to a ReceiveChange() method, we can sipmly call something very easy to read:
public override void ReceiveChange(BlackboardNote blackboardNote)
{
if (blackboardNote.IsNewActorSelected())
UpdatePortrait();
}
In this code, IsNewActorSelected() is an extension method for BlackboardNote objects. Check CreationExtensions.cs to see the code:
public static bool IsNewActorSelected(this BlackboardNote note)
{
return note.topic == gameData.activePlayerTopic &&
note.subject == gameData.activePlayerSubject;
}
In the demo project, we know that the BlackboardNote which contains the active player information will only be updated (and sent as a notitication) when it changes. So this extension method returns true if the topic and subject of the BlackboardNote match the topic and subject we have hard coded in the gameData class.
Last modified 8mo ago