question

software1103 avatar image
software1103 asked

Issue in creating Entity group

Hi,

I've met a issue trying to make an entity group for guild system in Unity.

I've seen and used this script as my starting point.

https://api.playfab.com/docs/tutorials/entities/entity-groups

And I've read this data being similar to my issue.

https://community.playfab.com/questions/19351/problem-creating-entity-group-for-clan-system.html.

But this doesn't seem like correct resolution for my case, so I want your great help.

When clicking the CreateRoom Button, I'm gonna store user entity key but I fail now.

...
using PlayFab.ClientModels;


...

    public void OnClickCreateRoom()
    {
       ...

        PlayFab.GroupsModels.EntityKey enkey = new PlayFab.GroupsModels.EntityKey { Id = "123" + RoomCreate.Instance.roomName.GetComponent<TMP_InputField>().text.Trim(), Type = "group" };

        GuildController.CreateGroup(RoomCreate.Instance.roomName.GetComponent<TMP_InputField>().text.Trim(), enkey);
    }
    ...

Follow script is GuildController script that I modified my starting point script.

using PlayFab.GroupsModels;
using System;
using System.Collections.Generic;
using UnityEngine;

[Serializable]
public class GuildController
{
    
    public static readonly HashSet<KeyValuePair<string, string>> EntityGroupPairs = new HashSet<KeyValuePair<string, string>>();
    public static readonly Dictionary<string, string> GroupNameById = new Dictionary<string, string>();


    public static EntityKey EntityKeyMaker(string entityId)
    {
        return new EntityKey { Id = entityId, Type = "title_player_account" };
    }


    private static void OnSharedError(PlayFab.PlayFabError error)
    {
        Debug.LogError(error.GenerateErrorReport());
    }




    public void ListGroups(EntityKey entityKey)
    {
        var request = new ListMembershipRequest { Entity = entityKey };
        PlayFab.PlayFabGroupsAPI.ListMembership(request, OnListGroups, OnSharedError);
    }
    private void OnListGroups(ListMembershipResponse result)
    {
        var prevRequest = (ListMembershipRequest)result.Request;
        foreach (var pair in result.Groups)
        {
            GroupNameById[pair.Group.Id] = pair.GroupName;
            EntityGroupPairs.Add(new KeyValuePair<string, string>(prevRequest.Entity.Id, pair.Group.Id));
        }
    }


    public static void CreateGroup(string groupName, EntityKey entityKey)
    {
        // A player-controlled entity creates a new group
        var request = new CreateGroupRequest { GroupName = groupName, Entity = entityKey };
        PlayFab.PlayFabGroupsAPI.CreateGroup(request, OnCreateGroup, OnSharedError);
    }
    private static void OnCreateGroup(CreateGroupResponse result)
    {
        Debug.Log("Group Created: " + result.GroupName + " - " + result.Group.Id);


        var prevRequest = (CreateGroupRequest)result.Request;
        EntityGroupPairs.Add(new KeyValuePair<string, string>(prevRequest.Entity.Id, result.Group.Id));
        GroupNameById[result.Group.Id] = result.GroupName;
    }

    public void DeleteGroup(string groupId)
    {
        var request = new DeleteGroupRequest { Group = EntityKeyMaker(groupId) };
        PlayFab.PlayFabGroupsAPI.DeleteGroup(request, OnDeleteGroup, OnSharedError);
    }
    private void OnDeleteGroup(EmptyResponse result)
    {
        var prevRequest = (DeleteGroupRequest)result.Request;
        Debug.Log("Group Deleted: " + prevRequest.Group.Id);


        var temp = new HashSet<KeyValuePair<string, string>>();
        foreach (var each in EntityGroupPairs)
            if (each.Value != prevRequest.Group.Id)
                temp.Add(each);
        EntityGroupPairs.IntersectWith(temp);
        GroupNameById.Remove(prevRequest.Group.Id);
    }

    public void InviteToGroup(string groupId, EntityKey entityKey)
    {
        // A player-controlled entity invites another player-controlled entity to an existing group
        var request = new InviteToGroupRequest { Group = EntityKeyMaker(groupId), Entity = entityKey };
        PlayFab.PlayFabGroupsAPI.InviteToGroup(request, OnInvite, OnSharedError);
    }
    public void OnInvite(InviteToGroupResponse result)
    {
        var prevRequest = (InviteToGroupRequest)result.Request;
        var request = new AcceptGroupInvitationRequest { Group = EntityKeyMaker(prevRequest.Group.Id), Entity = prevRequest.Entity };
        PlayFab.PlayFabGroupsAPI.AcceptGroupInvitation(request, OnAcceptInvite, OnSharedError);
    }

    public void OnAcceptInvite(EmptyResponse result)
    {
        var prevRequest = (AcceptGroupInvitationRequest)result.Request;
        Debug.Log("Entity Added to Group: " + prevRequest.Entity.Id + " to " + prevRequest.Group.Id);
        EntityGroupPairs.Add(new KeyValuePair<string, string>(prevRequest.Entity.Id, prevRequest.Group.Id));
    }




    public void ApplyToGroup(string groupId, EntityKey entityKey)
    {
        var request = new ApplyToGroupRequest { Group = EntityKeyMaker(groupId), Entity = entityKey };
        PlayFab.PlayFabGroupsAPI.ApplyToGroup(request, OnApply, OnSharedError);
    }
    public void OnApply(ApplyToGroupResponse result)
    {
        var prevRequest = (ApplyToGroupRequest)result.Request;

        var request = new AcceptGroupApplicationRequest { Group = prevRequest.Group, Entity = prevRequest.Entity };
        PlayFab.PlayFabGroupsAPI.AcceptGroupApplication(request, OnAcceptApplication, OnSharedError);
    }
    public void OnAcceptApplication(EmptyResponse result)
    {
        var prevRequest = (AcceptGroupApplicationRequest)result.Request;
        Debug.Log("Entity Added to Group: " + prevRequest.Entity.Id + " to " + prevRequest.Group.Id);
    }




    public void KickMember(string groupId, EntityKey entityKey)
    {
        var request = new RemoveMembersRequest { Group = EntityKeyMaker(groupId), Members = new List<EntityKey> { entityKey } };
        PlayFab.PlayFabGroupsAPI.RemoveMembers(request, OnKickMembers, OnSharedError);
    }
    private void OnKickMembers(EmptyResponse result)
    {
        var prevRequest = (RemoveMembersRequest)result.Request;
        Debug.Log("Entity kicked from Group: " + prevRequest.Members[0].Id + " to " + prevRequest.Group.Id);
        EntityGroupPairs.Remove(new KeyValuePair<string, string>(prevRequest.Members[0].Id, prevRequest.Group.Id));
    }
}

But this error occurs like follow.

/Group/CreateGroup: No group profile found at 123wer

I want your help. Thanks.

10 |1200

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

Andy avatar image
Andy answered

Oh, I know what's going on. Looking through your code more closely, you're providing the entity key of the not-yet-created group to the CreateGroup API. That entity key doesn't exist.

Instead, the Entity request property is intended to represent the actor. In this case it would be the entity key of a player. If you're just calling it in the context of the current player, you don't even need to provide an entity key.

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.

software1103 avatar image software1103 commented ·

Thanks @Andy for your continuous reply.

Do you mean that I should create the group at first?

I know being create new group by using the method,CreateGroup(string groupName,EntityKey entityKey), in my starting script.

https://api.playfab.com/docs/tutorials/entities/entity-groups

Is this false?

If false, how do you create the group? I hope you consider my playfab skill is young. thanks.

0 Likes 0 ·
brendan avatar image brendan software1103 commented ·

CreateGroup is the correct API call to use. What Andy is pointing out is that your line:

        PlayFab.GroupsModels.EntityKey enkey = new PlayFab.GroupsModels.EntityKey { Id = "123" + RoomCreate.Instance.roomName.GetComponent<TMP_InputField>().text.Trim(), Type = "group" };

Is invalid, as you're trying to set the Entity ID to something you construct ("123" + the roomName). The ID of all Entities in PlayFab are generated by our service - you don't arbitrarily set them. In this specific case, you would not specify the Entity for this call - just the GroupName.

0 Likes 0 ·
Andy avatar image
Andy answered

One scenario where you could be encountering that error is when you're creating and deleting a group by the same id repeatedly. If you're doing that, please ensure you're waiting for the delete response and then pausing briefly before calling create. If you're not deleting the group, can you try a different id?

4 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.

software1103 avatar image software1103 commented ·

Thanks for your advice, @Andy.

Would you like to explain about what you mean in more detail?

I only need to create the new group, not delete to group.

Please help me...

0 Likes 0 ·
Andy avatar image Andy ♦♦ software1103 commented ·

What I'm saying is that the only reason I could see why you'd be hitting that error is if a group with that id was created at some point and later deleted. Are you hitting the same error on every call? Have you tried a different id?

0 Likes 0 ·
software1103 avatar image software1103 Andy ♦♦ commented ·

Yes. I am hitting the same error on every call. You asked if I have tried a different id.

Of course. I tried every call with different id, but there is the same result.

This is sad thing. Would you have proper resolution,@Andy?

0 Likes 0 ·
Show more comments

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.