- Home /
PUN - Choose random Hero for player
So, the thing is: after a time, the players that don't choose a hero to play should receive a random hero. The heroes cannot be repeated. I use as exemple the DemoPickup and I created my on class:
using System;
using System.Collections.Generic;
using ExitGames.Client.Photon;
using UnityEngine;
using Hashtable = ExitGames.Client.Photon.Hashtable;
public class HeroAssigns : MonoBehaviour
{
public enum Hero : byte
{
none,
Cube,
Cube1,
Cube2,
Cube3,
Cube4,
Cube5
}
/// <summary>The main list of teams with their player-lists. Automatically kept up to date.</summary>
/// <remarks>Note that this is static. Can be accessed by PunTeam.PlayersPerTeam. You should not modify this.</remarks>
public static Dictionary<Hero, List<PhotonPlayer>> PlayersPerHero;
public static Dictionary<Hero, int> PlayersHero;
/// <summary>Defines the player custom property name to use for team affinity of "this" player.</summary>
public const string HeroPlayerProp = "hero";
#region Events by Unity and Photon
public void Start()
{
PlayersPerHero = new Dictionary<Hero, List<PhotonPlayer>>();
PlayersHero = new Dictionary<Hero, int>();
Array enumVals = Enum.GetValues(typeof(Hero));
foreach (var enumVal in enumVals)
{
PlayersPerHero[(Hero)enumVal] = new List<PhotonPlayer>();
PlayersHero[(Hero)enumVal] = new int();
}
}
/// <summary>Needed to update the team lists when joining a room.</summary>
/// <remarks>Called by PUN. See enum PhotonNetworkingMessage for an explanation.</remarks>
public void OnJoinedRoom()
{
UpdateHeroes();
}
/// <summary>Refreshes the team lists. It could be a non-team related property change, too.</summary>
/// <remarks>Called by PUN. See enum PhotonNetworkingMessage for an explanation.</remarks>
public void OnPhotonPlayerPropertiesChanged(object[] playerAndUpdatedProps)
{
UpdateHeroes();
}
#endregion
public static void UpdateHeroes()
{
Array enumVals = Enum.GetValues(typeof(Hero));
foreach (var enumVal in enumVals)
{
PlayersPerHero[(Hero)enumVal].Clear();
PlayersHero[(Hero)enumVal] = 0;
}
for (int i = 0; i < PhotonNetwork.playerList.Length; i++)
{
PhotonPlayer player = PhotonNetwork.playerList[i];
Hero playerHero = player.GetHero();
print(player.ID +" "+ playerHero);
PlayersPerHero[playerHero].Add(player);
PlayersHero[playerHero] ++;
}
}
}
/// <summary>Extension used for TeamAssigns and PhotonPlayer class. Wraps access to the player's custom property.</summary>
public static class heroExtensions
{
/// <summary>Extension for PhotonPlayer class to wrap up access to the player's custom property.</summary>
/// <returns>PunTeam.Team.none if no team was found (yet).</returns>
public static HeroAssigns.Hero GetHero(this PhotonPlayer player)
{
object heroId;
if (player.customProperties.TryGetValue(HeroAssigns.HeroPlayerProp, out heroId))
{
return (HeroAssigns.Hero)heroId;
}
return HeroAssigns.Hero.none;
}
/// <summary>Switch that player's team to the one you assign.</summary>
/// <remarks>Internally checks if this player is in that team already or not. Only team switches are actually sent.</remarks>
/// <param name="player"></param>
/// <param name="hero"></param>
public static void SetHero(this PhotonPlayer player, HeroAssigns.Hero hero)
{
if (!PhotonNetwork.connectedAndReady)
{
Debug.LogWarning("JoinTeam was called in state: " + PhotonNetwork.connectionStateDetailed + ". Not connectedAndReady.");
}
HeroAssigns.Hero currentHero = PhotonNetwork.player.GetHero();
if (currentHero != hero)
{
PhotonNetwork.player.SetCustomProperties(new Hashtable() { { HeroAssigns.HeroPlayerProp, (byte)hero } });
}
}
}
My problem is when the countdown finishes and its time do randomly choose the heroes, the SetHero doesn't set correctly, and always the last player don't set a hero.
IEnumerator Countdown()
{
while (countdown > 0f)
{
yield return new WaitForEndOfFrame();
countdown -= Time.deltaTime;
count.text = countdown.ToString("#.##");
}
if (countdown <= 0f)
{
if (HeroAssigns.PlayersHero[HeroAssigns.Hero.none]
> 0)
{
GameObject[] heroes = GameObject.FindGameObjectsWithTag("Heroes");
foreach (GameObject obj in heroes)
{
if (!obj.GetComponent<ChooseHero>().chosen)
{
list.Add(obj.GetComponent<ChooseHero>().hero);
}
}
for (int i = 0; i < PhotonNetwork.playerList.Length; i++)
{
PhotonPlayer player = PhotonNetwork.playerList[i];
int hNum = Random.Range(0, list.Count);
hero = (list[hNum]);
player.SetHero(hero);
list.RemoveAt(hNum);
}
}
//PhotonNetwork.LoadLevel("level");
}
Your answer
Follow this Question
Related Questions
PUN is being incredibly slow in builds 1 Answer
How To Destroy An Object Server Side When Using OnTriggerEnter 1 Answer
How to connect to our own dedicated server using photon networking in unity?(Self-hosted) 0 Answers
Photon - How to spawn enemies that don't have a PhotonView and be synced with other clients 1 Answer
Creating interactable enviornment items that are usable by all players in Multiplayer 2 Answers