OK so, I had some time to test it out… man do I have a problem:
Apart from ignoring the ‘RestoreFromJToken()’ (it calls the function, but placing the boolean as a parameter causes issues with the Interface) function in ‘Inventory.cs’, I called the boolean I set up at the start of ‘Inventory.cs’, known as ‘boolean refresh = true’, and then recalled in ‘InventoryUI.cs’, in the following functions:
In Inventory.cs, you have the following:
public bool refresh = true;
// Next changed function:
public bool AddToFirstEmptySlot(InventoryItem item, int number)
{
refresh = true;
// If we picked up money, automatically send it to the pouch:
foreach (var store in GetComponents<IItemStore>()) {
number -= store.AddItems(item, number);
}
if (number <= 0) return true;
// ----------------------------------------------------------
int i = FindSlot(item);
if (i < 0)
{
return false;
}
slots[i].item = item;
slots[i].number += number;
if (inventoryUpdated != null)
{
if (refresh == true) inventoryUpdated();
}
return true;
}
// Next updated function:
public void RemoveFromSlot(int slot, int number)
{
refresh = true;
slots[slot].number -= number;
if (slots[slot].number <= 0)
{
slots[slot].number = 0;
slots[slot].item = null;
}
if (inventoryUpdated != null)
{
if (refresh == true) inventoryUpdated();
}
}
// Next function:
public bool AddItemToSlot(int slot, InventoryItem item, int number)
{
refresh = true;
if (slots[slot].item != null)
{
return AddToFirstEmptySlot(item, number); ;
}
var i = FindStack(item);
if (i >= 0)
{
slot = i;
}
slots[slot].item = item;
slots[slot].number += number;
if (inventoryUpdated != null)
{
if (refresh == true) inventoryUpdated();
}
return true;
}
// Next function:
public void RemoveItem(InventoryItem item, int number, bool refresh = true) {
refresh = true;
if (item == null) return;
for (int i = 0; i < slots.Length; i++) {
if (ReferenceEquals(slots[i].item, item)) {
slots[i].number -= number;
if (slots[i].number <= 0) {
slots[i].item = null;
slots[i].number = 0;
}
if (refresh == true) inventoryUpdated();
return;
}
}
}
// Next Function:
public void TransferAllInventory(Inventory otherInventory)
{
refresh = false;
if (otherInventory == null || otherInventory == this) return;
for (int i = 0; i < inventorySize; i++)
{
if (slots[i].item == null) continue;
for (int j = slots[i].number; j > 0; j--)
{
if (otherInventory.HasSpaceFor(slots[i].item))
{
otherInventory.AddToFirstEmptySlot(slots[i].item, 1);
slots[i].number--;
if (slots[i].number <= 0) slots[i].item = null;
}
else break;
}
}
if (refresh == true) {inventoryUpdated?.Invoke();
otherInventory.inventoryUpdated?.Invoke();}
}
And in ‘InventoryUI.cs’:
private void Awake()
{
bool refresh = (GetComponent<Inventory>().refresh = true);
// if we are dealing with the player Inventory, set it up (just don't do it for the Players' bank)
if (isPlayerInventory) {
selectedInventory = Inventory.GetPlayerInventory();
if (refresh) selectedInventory.inventoryUpdated += Redraw;
}
}
// Next function:
public bool Setup(GameObject user, bool refresh = true) {
if (user.TryGetComponent(out selectedInventory)) {
// if the object has an inventory (our bank in this case), set the Selected Inventory Up,
// Subscribe to the Redraw() changes, and then redraw the inventory:
if (refresh) selectedInventory.inventoryUpdated += Redraw;
Title.text = selectedInventory.name;
Redraw();
return true;
}
return false;
}
The problem is… my inventory size is now 25 slots (instead of 50), and nothing I pick off the ground is stored in the Inventory slots anymore. Please help
I have to say though, somewhere down the line of this attempt it did refresh at a significantly faster rate than usual, when I hit the ‘TransferAllToOtherInventory()’ function-triggering button, but I’m not sure why…