Post by kingmob on Feb 19, 2019 21:26:14 GMT
Trying to get this inventory code to use pool boss instead of instantiating (the ultimate sin) and I'm running in to issues.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using VRInteraction;
namespace VRWeaponInteractor
{
public class InventoryStorage : MonoBehaviour
{
public GameObject prefab;
public string prefabString;
public bool infinte;
public int storageCapacity = 10;
public int initialStoredItems;
private int _storedItems;
private ItemSlot _itemSlot;
private Queue<GameObject> _disabledStoredItems = new Queue<GameObject>();
private string acceptedItemId;
public int storedItems
{
get { return _storedItems; }
set { _storedItems = value; }
}
void Start()
{
_itemSlot = GetComponent<ItemSlot>();
_storedItems = initialStoredItems;
if (_itemSlot == null)
{
Debug.LogError("Inventory Storage requires an item slot: " + name, gameObject);
return;
}
if (prefab == null)
{
Debug.LogError("No prefab on Inventory Storage: " + name, gameObject);
return;
}
VRInteractableItem item = prefab.GetComponentInChildren<VRInteractableItem>();
if (item == null)
{
Debug.LogError("No VRInteractbleItem on prefab object: " + name, gameObject);
return;
}
if (_itemSlot.GetSlotPosition(item) == null)
{
Debug.LogError("Prefab in Inventory Storage doesn't have an inventory slot accepted id: " + name, gameObject);
return;
}
acceptedItemId = item.itemId;
}
void Update()
{
if ((!infinte && storedItems == 0) || _itemSlot.hasItem) return;
if (!infinte) storedItems--;
GameObject itemObject;
if (_disabledStoredItems.Count == 0)
itemObject = Instantiate<GameObject>(prefab);
else itemObject = _disabledStoredItems.Dequeue();
VRInteractableItem item = itemObject.GetComponentInChildren<VRInteractableItem>();
_itemSlot.AddItemToSlot(item);
}
public bool TryAddItem(VRInteractableItem item)
{
if (item.itemId != acceptedItemId) return false;
if ((infinte || storedItems < storageCapacity) && !_disabledStoredItems.Contains(item.item.gameObject))
{
//Can store
if (!infinte) storedItems++;
item.item.gameObject.SetActive(false);
_disabledStoredItems.Enqueue(item.item.gameObject);
return true;
}
return false;
}
}
}
I keep replacing the instantiate line with the pool boss methods and it's not getting the desired results. Any help with switching this system up to using pool boss would be greatly appreciated. Thanks!