Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Third Party Ufps Photon Choosing Classes

Discussion in 'Multiplayer' started by Lethalbear, May 26, 2016.

  1. Lethalbear

    Lethalbear

    Joined:
    May 26, 2016
    Posts:
    4
    How would i go about being able to chose a class before entering the room. Such as an assult class which contains the guns (Machine gun and pistol only) or recon (Sniper and pistol). Thanks in advance
     
  2. Kamil-Says

    Kamil-Says

    Joined:
    Jun 30, 2014
    Posts:
    154
    Save Selected class name in playerprefs, weapon manager will read the class name on player instantiate and load the weapons for selected class from resources folder, and instantiate them. That's the easiest way
     
  3. Lethalbear

    Lethalbear

    Joined:
    May 26, 2016
    Posts:
    4
    Right so ive chosen the class (preselected the pistol and machine gun), but this will do it for every player that spawns. He doesnt get the option to chose which gun class he wants. I want it so the player at the menu gets to preselect what class he wants
     
  4. Kamil-Says

    Kamil-Says

    Joined:
    Jun 30, 2014
    Posts:
    154
    Yes simple because the the other players do not know which weapons you have quiped this means you have to send your own "loadout" to them so when you call the remote procedure call with your loadout settings the other players will know which weapons they have to load.
     
  5. Lethalbear

    Lethalbear

    Joined:
    May 26, 2016
    Posts:
    4
    So how will i go about doing this, sorry im abit new to the whole ufps scene :)
     
  6. Kamil-Says

    Kamil-Says

    Joined:
    Jun 30, 2014
    Posts:
    154
    Im not using pun, but photon.

    That's where you set up your load out items and call loadallaviliableweapons:

    Code (CSharp):
    1. if (photonView.isMine)
    2.         {
    3.             LoadoutManager SerializedLoadOut = new LoadoutManager("PistolGun33","SergaSniper","WaveShotgun12","SergaSniper");
    4.             byte[] loadOutBuff = SerializedLoadOutView.SerializeLoadOut(SerializedLoadOut);
    5.             photonView.RPC("LoadAllAviliableWeapons", PhotonTargets.AllBuffered,loadOutBuff);
    6.         }
    Here you load the content:

    Code (CSharp):
    1. [PunRPC]
    2.     private void LoadAllAviliableWeapons(byte[] loadOutDataStream,PhotonMessageInfo actorInfo)
    3.     {
    4.         for(int j =1; j < weaponSlot.Length; j++)
    5.         {
    6.             if(weaponSlot[j] != null)
    7.             {
    8.                 weaponSlot[j] = null;
    9.             }
    10.         }
    11.         for (int i = 1; i < weaponSlot.Length; i++)
    12.         {
    13.             KamoUnityItem UnityItem;
    14.             LoadoutManager DeserializedLoadOut = (LoadoutManager)SerializedLoadOutView.DeserializeLoadOut(loadOutDataStream);
    15.             if (DeserializedLoadOut.TryGetWeaponItem(i, out UnityItem))
    16.             {
    17.                 weaponSlot[i] = new WeaponSlot(UnityItem, this.WeaponCameraPoint.transform, NetworkPoint.transform, actorInfo.sender.name);
    18.             }
    19.             else
    20.             {
    21.                 Debug.LogError(string.Format("Couldn't initialize weapon slot: {0}", i));
    22.             }
    23.         }
    24.     }
    Serialize & Deserialize your load out:

    Code (CSharp):
    1. public static class SerializedLoadOutView
    2. {
    3.     public static byte[] SerializeLoadOut(System.Object loadOutObject)
    4.     {
    5.         BinaryFormatter binaryF = new BinaryFormatter();
    6.         using (MemoryStream memoryStream = new MemoryStream())
    7.         {
    8.             binaryF.Serialize(memoryStream, loadOutObject);
    9.  
    10.             return memoryStream.ToArray();
    11.         }
    12.     }
    13.  
    14.     public static System.Object DeserializeLoadOut(byte[] dataStream)
    15.     {
    16.         using (MemoryStream memoryStream = new MemoryStream())
    17.         {
    18.             BinaryFormatter binaryF = new BinaryFormatter();
    19.  
    20.             memoryStream.Write(dataStream, 0, dataStream.Length);
    21.             memoryStream.Seek(0, SeekOrigin.Begin);
    22.  
    23.             return (System.Object)binaryF.Deserialize(memoryStream);
    24.         }
    25.     }
    26. }
    I can't explain all or give more code about the core functions but this should give you small over view how it works!
     
  7. Lethalbear

    Lethalbear

    Joined:
    May 26, 2016
    Posts:
    4
    this is all in one new script or what?
     
  8. Kamil-Says

    Kamil-Says

    Joined:
    Jun 30, 2014
    Posts:
    154
    There are few classes that working together but as I said, it's only for over view