question

tomgeorgin avatar image
tomgeorgin asked

Keep count of certain player events and have the game query PlayFab to know how many players sent that same event

Hello,

I'm looking for pointers on the best way to approach a problem. I'm very much new to PlayFab.

In the game we're making, players will be making choices. We want to aggregate all of the players that made Choice A and all of the players that made Choice B given a choice to make.

At some point, we also want to be able to know what proportion of players picked A or B (so query the database).

That means 2 things:
- I need to know how to react to a WritePlayerEvent being sent and store data inside PlayFab (such as internal counters or data in general)
- I need to query it from the game

How do I go about doing this please?

Thanks a lot!

Btw, I'll post here all of the things I've already read that didn't seem to solve my problem, or at least not entirely:

https://api.playfab.com/playstream
https://api.playfab.com/docs/tutorials/landing-players/players-actions

I also dabbled in the "Rules" on the Manager but that didn't work out too well... the field "Event Type" is confusing.

Cheers,
Tom

10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Citrus Yan avatar image
Citrus Yan answered

Hi, I may have come up with a solution for you. The basic idea is that when the player makes a choice, write a custom event using WritePlayerEvent API, then PlayFab will trigger an action (a Rule is defined to react to the custom player event) to update the number of each choice. Here are the steps I took to achieve your requirements:

    1. Define a new rule, you can name it “RecordA”, the “Event Type” field should be a custom event you wrote using WritePlayerEvent API, for instance, you can name it “title.xxxxx(your title id).player_choice_A”:

    2. For Actions, Action type is “ExecuteCloudScript”, the CloudScript function is a function I wrote to update the number of each choice, here is the code:

    handlers.RecordChoiceA = function(args){
        var recordA = server.GetTitleData({
            Keys:["ChoiceA"]
        }).Data.ChoiceA;
        var count = parseInt(recordA);
        count ++;
        server.SetTitleData({
            Key: "ChoiceA",
            Value: count
        });
        
    }											
    

    3. Define two Title Data, RecordA and RecordB, for instance, set the value to be 2.


    4. Call WritePlayerEvent API, for instance, let EventName field be “player_choice_A”. PlayFab will generate an event called “title.xxxx.player_choice_A” and triggers Action defined in Rule “RecordA”, then the function will be executed, related title internal data will be updated. You will find that RecordA defined in Title Data is 3. For choice B, you can set a similar Rule to record it.

    From the client, when the player makes choice A/B, write player event “player_choice_A/B” to record the number of choice A/B, when you need to know the proportion of each choice, just call GetTitleData API to get the number of each choice and calculate the proportion.

    By the way, as far as I am aware, the Event Type is just the name of the events, for custom events, you must start with "title.TitleId". Each event type has a set of properties that are included as part of event's data wherever it is sent. You can view these properties and build rules for triggering actions based on their values in the PlayStream tab of the Game Manager.Here is the doc you may find helpful: https://docs.microsoft.com/en-us/gaming/playfab/features/analytics/metrics/playstream-events#custom-event-overview


    1.jpg (31.2 KiB)
    3 comments
    10 |1200

    Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

    tomgeorgin avatar image tomgeorgin commented ·

    Everytime I create a custom Event Type by using the suggested format (title.id.name), and hit save, if I reopen the Rule, the thing is gone from the list and the field is empty...

    Also, where can I check the logs since I just trigger the function Hello World for now that's provided with the sample CloudScript and it's logging stuff, but I can't seem to find the log window.

    Thanks!

    0 Likes 0 ·
    Citrus Yan avatar image Citrus Yan tomgeorgin commented ·

    Yeah, I encountered this problem too, not sure what's the matter though, I will inform our product about this.

    For the logs, you can see them in [Analytics] -> [Event History].

    0 Likes 0 ·
    franklinchen avatar image franklinchen tomgeorgin commented ·

    The issue as custom event type field is empty when re-editing rules is fixed

    0 Likes 0 ·
    tomgeorgin avatar image
    tomgeorgin answered

    Hi

    Thank you for the detailed answer.

    I was dabbling in some of what you said and was in the process of trying out. The biggest question mark for me was the name of custom event, it felt like I had to make it match my player events that I send from C++. Which kind of makes sense since I should be able to set that up before the first event is sent and PlayFab is aware it exists, but at the same time, once events have been sent at least once, it'd be nice to have them in that dropdown menu.


    Anyway, I'm testing this out, for the first part: sending data to playfab and reacting to it.

    Once I got that working, I'll try what you suggested to get the information.

    Thank you for your help! I'll come and close this if all of this works out.


    Best regards,
    Tom

    10 |1200

    Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

    Fuzail Sarang avatar image
    Fuzail Sarang answered

    Better way to do this is to use a custom event to update player statistics. Then you can define segments on a player statistic. Which gives you the players in that segment. You can also use a leader board to see the total players in that segment. I wouldn't personally use Title Data and try an update Title Data because of the scenario of multiple players trying to update the title data count. Title Data is not meant for constantly changing values.

    2 comments
    10 |1200

    Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

    tomgeorgin avatar image tomgeorgin commented ·

    Does that involve the same type of operations though? I'd still have to create a Rule to trigger a script?

    Or are you suggesting that I can do all of that directly from my game's code without actually writing CloudScript?

    Thank you

    0 Likes 0 ·
    Citrus Yan avatar image Citrus Yan commented ·

    Yeah, title data is not meant for constantly changing value, I totally missed that. Thanks for noticing:)

    0 Likes 0 ·

    Write an Answer

    Hint: Notify or tag a user in this post by typing @username.

    Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.