Search Unity

Question Mirror reference server objects in command

Discussion in 'Multiplayer' started by homelleon, Jan 3, 2024.

  1. homelleon

    homelleon

    Joined:
    Jan 9, 2019
    Posts:
    9
    Hello, I'm implementing MMO-like inventory. Now I don't need to store data in DB. So I just used NetworkBehaviour derived InventoryManager class with SyncList Defaults field

    Code (CSharp):
    1.  public readonly SyncDictionary<string, InventorySyncData> Defaults = new SyncDictionary<string, InventorySyncData>();
    2. ...
    3.         public override void OnStartServer()
    4.         {
    5.             foreach (var item in _config.Array)
    6.                 Defaults.Add(item.Data.Name, new InventorySyncData { Item = item.Data });
    7.         }
    8.  
    then I want to recieve some of these Default configs in command sent by ConcretePlayerInventory

    Code (CSharp):
    1.  
    2.         [Command]
    3.         private void PickupItemCmd(Ray ray, float reachDistance)
    4.         {
    5.             if (!Physics.Raycast(ray, out RaycastHit hit, reachDistance)) return;
    6.  
    7.             var worldObject = hit.collider.gameObject;
    8.             var itemContainer = worldObject.GetComponent<Item>();
    9.  
    10.             if (itemContainer == null) return;
    11.             //_manager = NetworkClient.connection.identity.GetComponent<InventoryManager>();
    12.             var pickupData = _manager.Defaults[itemContainer.Config.Data.Name];
    13.             pickupData.SlotIndex = 0;
    14.             pickupData.Amount = itemContainer.Amount;
    15.  
    But I get null pointer on _manager.Defaults because I recieve it by

    Code (CSharp):
    1.  
    2.         public override void OnStartLocalPlayer()
    3.         {
    4.             mainCamera = Camera.main;
    5.             _worldContainer = GameObject.FindGameObjectWithTag("World").transform;
    6.  
    7.             _manager = GameObject.FindGameObjectWithTag("Inventory").GetComponent<InventoryManager>();
    8.             _manager = NetworkClient.connection.identity.GetComponent<InventoryManager>();
    9.             _manager.Player = this;
    10.             Debug.Log("Manager1: " + _manager);
    11.             _inventorySlotsSync.Callback += _manager.OnInventoryChanged;
    12.             InitInventoryCmd(_manager.Slots.Count);
    13.         }
    How shoud I reference server only InventoryManager Object in player command object?