question

halpar avatar image
halpar asked

Is there a way I can avoid callback hell

I am trying to implement an elo update system using playfab.My game is server authorative so i want the server to set this data. I store player ELO's in entity objects. I have 2 players. This means I call PlayFabDataAPI.GetObjects for player 1, then i call PlayFabDataAPI.GetObjects for player 2 in the successful return call back off original function. Then I have 2 more layers of SetObject api calls to finish updating the elos. This creates really ugly code. Is there anything I can do to avoid this callback hell in playfab?

Maybe is there a way i can call GetObjects functions together than after they both complete I can call set objects together?

apisentities
10 |1200

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

1 Answer

·
Gosen Gao avatar image
Gosen Gao answered

You can call PlayFabDataAPI.GetObjects synchronously for player 1 and player 2. Then before you need to call SetObject, check if both player's objects are acquired, confirm that the objects have been acquired then continue.

According to the last thread you post, I believe you are using Objects in the matchmaking system. We recommend that you use User as the queue’s Attribute source, and get player’s object and add info to the Attribute when creating matchmaking ticket. Then you can get player’s object info in the GetMatchmakingTicket’s response when matchmaking is completed.

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.

halpar avatar image halpar commented ·

Abour your first paragraph, when i try to set a bool "player1ObjectRecieved" in my class from the callback function, it doesnt allow it saying something along the lines of "callbacks cannot alter outside variables" however way i tried, with reference parameters and global variables i think i came across this issue. So how can i check if both objects are acquired? I think if i could do that i could have a seperate corooutine that checks wheter both objects are recieved than initiates the Set Objects call.

About your second paragraph, i need my game to be fully server authorative, so i disabled write access from my clients side, but if i use the User as my attribute source, they can send wrong elo information to my matchmaking which is undesired in my case. Am i misunderstanding your point?

0 Likes 0 ·
halpar avatar image halpar commented ·

Upon looking at the api a bit more, i have a new idea, would I be able to call GetMatch from the server whenmatchmaker allocates this server to retrieve the data objects of each of its players? Then I wouldnt have to call getObjects for each client and can just call setObject at the end of the match?

0 Likes 0 ·
Gosen Gao avatar image Gosen Gao halpar commented ·

You can call GetMatch in your server, but it only returns the Attribute you set when you call CreateMatchmakingTicket not the player's object.

As for the "callbacks cannot alter outside variables" issue, I can modify a global variable inside a callback function with this simple code sample.

bool playerOneElo;
void Start()
{
    PlayFabClientAPI.LoginWithCustomID(
        new LoginWithCustomIDRequest
        {
            CustomId = "Gosen"
        },
        onLoginSuccessful,
        onCommonError);

    Debug.Log(playerOneElo);
}

private void onLoginSuccessful(LoginResult result)
{
    PlayFabDataAPI.GetObjects(
        new PlayFab.DataModels.GetObjectsRequest
        {
            Entity = new PlayFab.DataModels.EntityKey
            {
                Id = result.EntityToken.Entity.Id,
                Type = result.EntityToken.Entity.Type
            }
        },
        onEloGet,
        onCommonError);
}

private void onEloGet(PlayFab.DataModels.GetObjectsResponse objects)
{
    playerOneElo = true;
    Debug.Log(playerOneElo);
}

0 Likes 0 ·
1.png (5.6 KiB)

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.