Documentation

Did you find this helpful?

PlayStream Hooks: Rules, Conditions, & Actions

A PlayStream Rule allows you to react to a subset of one type of PlayStream Events in real time.

Requirements:

A quick Glossary of Relevant Terms:

  • PlayStream: The group of features that make up the PlayFab Event pipeline
    • A PlayStream Event is a json-formatted string describing an event about a Player or Title
    • PlayStream events have a maximum size based on your Automation feature tier
  • Rule: Performs extra logic in response to one type of PlayStream event in real time
    • The sum of: One Event Trigger, an optional list of conditions, and an optional list of actions
  • Trigger: The name of the event which activates this Rule
  • Condition: A content filter for Rules and other PlayStream features
    • Does a very lightweight evaluation of the PlayStream Event json object, and skips Events that don't match requirements
  • Action: The contextual work to be done on the appropriate entity

A Rule consists of exactly one Trigger, an optional list of conditions, and typically at least one action [not required but quite useless without it].

Triggers, Conditions, and Actions are also part of other systems: Bulk Actions and Tournament Leaderboards.

Example Case: Count Custom Events from the Client

In this example, we post the following custom event from the client:

public void WriteEvent() {
    PlayFabClientAPI.WritePlayerEvent(new WriteClientPlayerEventRequest {
        EventName = "ForumPostEvent",
        Body = new Dictionary<string, object> {
            { "Subject", "My First Post" },
            { "Body", "My awesome Post." }
        }
    }, LogSuccess, LogFailure);
}

In this case, the client is manually reporting a custom forum post event (this does not currently correspond to any automatic event in PlayFab or supported partners). We will use a PlayStream Rule to count the number of times that a player reports a forum post in this way.

Requirement: Your game must already be posting events of this type before the Game Manager GUI will allow you to create a Rule triggering on it.

Game Manager: Navigate to your title -> Automation -> Rules -> New Rule

Let's evaluate the new rule in this example piece by piece:

  • title.6195.ForumPostEvent
    • 6195 is the title I used while making this document. Your titleId will match your own title
    • ForumPostEvent is the "EventName" used in the example code above
    • This line will only be available if you have recently posted at least one "ForumPostEvent" in your title
  • The Condition specifically in this example has only minimal use
    • However, it does demonstrate filtering our custom event if the "Body" key is mapped to "Invalid string" in our "Body" in our "Body" dictionary
    • Filters are optional, and should remove irrelevant players from your Rule
  • The chosen action in this case increments a statistic counter by one
    • Requirement: The "Forum Posts" statistic must be defined under "Leaderboards" in the Game Manager

In the Game Manager PlayStream Debugger, you can watch as the trigger takes effect:

As you can see, the custom ForumPostEvent automatically triggers the statistic change we set up above.

Advanced: Cloud Script Actions

Many of the built-in actions are fairly simple, and might not give you the full power you need. We allow you to fully customize the Action logic with Cloud Script.

Let's edit the rule, remove the condition, set the Action to Execute Cloud Script, and Save Action

Likewise, if we post a new ForumPostEvent, we will see a Cloud Script execution:

Writing your Cloud Script to react to PlayStream Rules is an advanced topic covered in our advanced Cloud Script guide.



Did you find this helpful?