Hello, I am trying to update the number of players on a server. Right now my matchmaking and queue seem to be working, but the number of players on the server never shows. its always zero. How do I update it? Currently I am NOT using mirror as when I had tried to use it, it wasn't working for me.
Here is my UserProfile.cs class that calls the server when the user logs in successfully and so on.
using System;
using System.Collections;
using System.Collections.Generic;
using PlayFab;
using PlayFab.ClientModels;
using UnityEngine;
using UnityEngine.UI;
using PlayFab.MultiplayerModels;
using TMPro;
using Fungus;
public class UserProfile : MonoBehaviour
{
// public AgentListener agentListener;
public Configuration configuration;
public Flowchart flowchart;
public static UserProfile instance;
public static UserProfile UP;
public static int glimmers;
public Text ErrorMessage;
[Header("UI")]
public TMP_Text glimmersValueText;
[SerializeField] ProfileData profileData;
void OnEnable()
{
UserAccountManager.OnSignInSuccess.AddListener(SignIn);
UserAccountManager.OnUserDataRetrieved.AddListener(UserDataRetrieved);
ErrorMessage.text = "";
if(UserProfile.UP == null)
{
UserProfile.UP = this;
}
else
{
if(UserProfile.UP != this)
{
Destroy(this.gameObject);
}
}
DontDestroyOnLoad(this.gameObject);
}
private void Awake() {
instance = this;
}
void OnDisable()
{
UserAccountManager.OnSignInSuccess.RemoveListener(SignIn);
UserAccountManager.OnUserDataRetrieved.RemoveListener(UserDataRetrieved);
}
void OnEnable(ModifyUserVirtualCurrencyResult result) {
Debug.Log("Game started!");
instance.GetVirtualCurrencies();
}
private void SignIn()
{
RequestMultiplayerServer();
GetUserData();
GetVirtualCurrencies();
ErrorMessage.text = "";
RequestMatchmaking();
}
private string matchmakingTicket = "";
private float ticketTimer = 0;
void RequestMatchmaking()
{
CreateMatchmakingTicketRequest requestData = new CreateMatchmakingTicketRequest();
requestData.Creator = new MatchmakingPlayer {
Entity = new PlayFab.MultiplayerModels.EntityKey {
Id = PlayFabSettings.staticPlayer.EntityId,
Type = PlayFabSettings.staticPlayer.EntityType,
},
Attributes = new MatchmakingPlayerAttributes {
DataObject = new {
latencies = new object[] {
new {
region = "EastUs",
latency = 100,
},
},
},
},
};
requestData.QueueName = "myqueue"; // Matchmaking Queue Name
requestData.GiveUpAfterSeconds = 120; // 120 seconds
PlayFabMultiplayerAPI.CreateMatchmakingTicket( requestData, OnCreateMatchmakingTicketResult, OnCreateMatchmakingTicketError );
}
private void OnCreateMatchmakingTicketResult( CreateMatchmakingTicketResult response )
{
CheckMatchmakingTicket( response.TicketId );
}
private void OnCreateMatchmakingTicketError( PlayFabError error )
{
Debug.Log( error.ErrorMessage );
}
private void CheckMatchmakingTicket( string ticketId )
{
Debug.Log( "Checking ticket " + ticketId );
matchmakingTicket = ticketId;
GetMatchmakingTicketRequest requestData = new GetMatchmakingTicketRequest();
requestData.QueueName = "myqueue"; // Matchmaking Queue Name
requestData.TicketId = ticketId;
PlayFabMultiplayerAPI.GetMatchmakingTicket( requestData, OnCheckMatchmakingTicketResult, OnCheckMatchmakingTicketError );
}
private void OnCheckMatchmakingTicketResult( GetMatchmakingTicketResult response )
{
bool queueTicketCheck = false;
switch( response.Status )
{
case "Matched":
ErrorMessage.text = "Found Match!";
Debug.Log( "Found Match " + response.MatchId );
matchmakingTicket = "";
JoinMatch( response.MatchId );
break;
case "WaitingForMatch":
ErrorMessage.text = "Waiting for match";
Debug.Log( "Waiting for match..." );
queueTicketCheck = true;
break;
case "WaitingForPlayers":
ErrorMessage.text = "Waiting for players";
Debug.Log( "Waiting for players..." );
queueTicketCheck = true;
break;
case "WaitingForServer":
ErrorMessage.text = "Waiting for server";
Debug.Log( "Waiting for server..." );
queueTicketCheck = true;
break;
case "Canceled":
ErrorMessage.text = "Canceled";
Debug.Log( "Canceled..." );
matchmakingTicket = "";
break;
default:
break;
}
if( queueTicketCheck )
{
ticketTimer = 6.0f;
}
}
static private List<ConnectedPlayer> Players = new List<ConnectedPlayer>();
private void OnCheckMatchmakingTicketError( PlayFabError error )
{
Debug.Log( error.ErrorMessage );
}
private void JoinMatch( string matchId )
{
Debug.Log( "Joining Match..." );
GetMatchRequest requestData = new GetMatchRequest();
requestData.QueueName = "myqueue"; // Matchmaking Queue Name
requestData.MatchId = matchId;
PlayFabMultiplayerAPI.GetMatch( requestData, OnGetMatchResult, OnGetMatchError );
Debug.Log(Players);
}
private void OnGetMatchResult( GetMatchResult response )
{
Debug.Log(response);
Debug.Log(response.ServerDetails);
Debug.Log(response.ServerDetails.IPV4Address);
Debug.Log(response.ServerDetails.Ports);
Debug.Log(response.ServerDetails.Region);
ErrorMessage.text = $"{response.Members[0].Entity.Id} vs {response.Members[1].Entity.Id}";
}
private void OnGetMatchError( PlayFabError error )
{
Debug.Log( error.ErrorMessage );
}
private void RequestMultiplayerServer(){
Debug.Log("[ClientStartUp].RequestMultiplayerServer");
RequestMultiplayerServerRequest requestData = new RequestMultiplayerServerRequest();
requestData.BuildId = configuration.buildId;
requestData.SessionId = System.Guid.NewGuid().ToString();
requestData.PreferredRegions = new List<string>() { AzureRegion.EastUs.ToString() };
PlayFabMultiplayerAPI.RequestMultiplayerServer(requestData, OnRequestMultiplayerServer, OnRequestMultiplayerServerError);
}
private void OnRequestMultiplayerServer(RequestMultiplayerServerResponse response)
{
Debug.Log(response.BuildId);
Debug.Log(response.ToString());
}
private void OnRequestMultiplayerServerError(PlayFabError error)
{
Debug.Log(error.ToString());
}
void Update()
{
// Update matchmaking ticket check
if( ticketTimer > 0 && matchmakingTicket != "" )
{
ticketTimer -= Time.deltaTime;
if( ticketTimer <= 0.0f )
{
CheckMatchmakingTicket( matchmakingTicket );
}
}
}