Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by PrisVas · Jun 28, 2016 at 12:36 AM · c#multiplayerphotonmultiplayer-networkingmultiplayer networking

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");
         }




alt text

screenshot-4.png (65.3 kB)
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

180 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges