How to access a List from the MasterClient

Hello. I am having trouble trying to sync many enemies and getting them to instantiate correctly on other clients. It works with a single player and on the Master Client but not on other clients. What I am trying to have set up is an Object Pooling method that reads what items are in the list from one list. However, I have no idea how to access the list that is formed on the MasterClient. IS there a way to sync a list of GameObjects or to retrieve a list that is only on the MasterClient? Thx for any assistance.

Answers

  • Hi, PVTarwater! Can I see your code please?
  • edited February 8
    RomanRaw said:

    Hi, PVTarwater! Can I see your code please?

    Certainly:
    /* 
     * ======================================================================================================================================================
     * OBJECT POOLING MP
     * This is a generic pooling class for online multiplayer that can pool any object. Attach this to an empty game object in the scene. In the inspector,
     * plug in the object to be pooled, how many are to be pooled at one time, and determine if the pool can be expanded at runtime.
     * ======================================================================================================================================================
     */
    
    using System.Collections.Generic;
    using UnityEngine;
    
    // This is the object class that determines a poolable object's base properties.
    [System.Serializable]
    public class PoolItem
    {
        public string name;
        public GameObject pooledObject;
        public int pooledAmount;
        public bool canGrow;
    }
    
    // The actual class that pools objects.
    public class ObjectPooling_MP : Photon.PunBehaviour
    {
        [SerializeField] private List<PoolItem> itemsToPool;
        public static ObjectPooling_MP currentPooling;  // Class reference is declared here.
        private List<GameObject> pooledObjects;
    
        private GameObject obj;
    
        private void Awake()
        {
            currentPooling = this;  // Creates a public reference to itself.
        }
    
        // Initializes the pooled object list.
        private void Start()
        {
            if (PhotonNetwork.isMasterClient)
            {
                MasterInitialize();
            }
    
            if (!PhotonNetwork.isMasterClient)
            {
                if (obj == null)
                {
                    Debug.LogError("No object to add to list!");
                }
            }
        }
        
        private void MasterInitialize()
        {
            pooledObjects = new List<GameObject>();
    
            foreach (PoolItem item in itemsToPool)
            {
                for (int i = 0; i < item.pooledAmount; i++)
                {
                    obj = PhotonNetwork.Instantiate(item.pooledObject.name, new Vector3(0, 0, 0), Quaternion.identity, 0);
                    obj.SetActive(false);
                    pooledObjects.Add(obj);               
                }
            }        
        }
    
        // Returns a pooled object from the pooled objects list.
        public GameObject GetPooledObject(string tag)
        {
            // This returns the object to be pooled.
            for (int i = 0; i < pooledObjects.Count; i++)
            {
                if (!pooledObjects[i].activeInHierarchy && pooledObjects[i].tag == tag)
                {
                    return pooledObjects[i];
                }
            }
    
            // This adds more objects to the pool if Can Grow is checked on.
            foreach (PoolItem item in itemsToPool)
            {
                if (item.pooledObject.tag == tag)
                {
                    if (item.canGrow)
                    {                    
                        obj = PhotonNetwork.Instantiate(item.pooledObject.name, new Vector3(0, 0, 0), Quaternion.identity, 0);
                        pooledObjects.Add(obj);
                        return obj;
                    }
                }
            }
    
            return null;
        }
    }
    The problem I am having is I have no idea how to access the list of game objects that all of the other clients need to be looking at so it knows what to turn off and on. I can't find anything online either on how to access a list with Photon so I am really curious how to do that. Would streaming the data help and if so, what variables would I need to set up and how would the other clients read that stream to get the correct data from the list?

    EDIT: Oh, I forgot to mention that the object pooling prefab is actually instantiated as a scene object since I tried before having it as a standard preloaded prefab which didn't work either.
Sign In or Register to comment.