Blank
Blank
I am using these scripts (added below) to save and load the items
from each container by assigning an id for each container. However, when containers are
stored in player inventory, and respawned in the game by dropping it from player
inventory, a new id is assgined, and the one originally stored is not preserved. It should
either assign the id that corresponds to the list of items from that container, or containers
should move between inventories retaining their id throughout. What do you suggest?
"using UnityEngine;
[Header("Item Information")]
[Header("Custom Actions")]
[System.Serializable]
None,
Store,
Examine,
Use,
Drop,
Open
[System.Serializable]
"
"using System.Collections.Generic;
using UnityEngine;
itemDictionary.Add(item.itemName, item);
else
{
if (itemDictionary != null && itemDictionary.TryGetValue(itemName, out var item))
return item;
return null;
}"
"using System.Collections.Generic;
using UnityEngine;
[System.Serializable]
[System.Serializable]
if (Instance == null)
Instance = this;
DontDestroyOnLoad(gameObject);
else
Destroy(gameObject);
savedContainers[saveData.containerID] = saveData;
Debug.Log($"Container {saveData.containerID} saved with
{saveData.inventoryItems.Count} items.");
container.LoadContainerData(saveData, itemDatabase);
else
}
// Optional: Load all data from persistent storage
}"
"using System;
using System.Collections.Generic;
using UnityEngine;
[System.Serializable]
[Header("Container Settings")]
[Header("Initial Items")]
[Header("UI")]
if (string.IsNullOrEmpty(containerID))
containerID = Guid.NewGuid().ToString();
if (uiManager == null)
uiManager = FindObjectOfType<UIManager>();
if (uiManager == null)
{
Debug.LogWarning("UIManager not found in the scene. UI will not update.");
if (saveManager != null)
saveManager.LoadContainer(this, FindObjectOfType<ItemDatabase>());
else
return false;
}
if (containerInventory.ContainsKey(item))
containerInventory[item] += quantity;
else
containerInventory[item] = quantity;
currentWeight += itemTotalWeight;
if (refreshUI)
UpdateUI();
return true;
containerInventory[item] -= quantityToRemove;
if (containerInventory[item] <= 0)
{
containerInventory.Remove(item);
UpdateUI();
RemoveItemFromContainer(itemData, 1);
playerInventory.AddItem(itemData);
// Update UI
UpdateUI();
uiManager.sidePanel.SetActive(false);
}
uiManager.PopulateItemList(this);
return currentWeight;
containerID = containerID,
};
foreach (var kvp in containerInventory)
saveData.inventoryItems.Add(new ItemSaveData
quantity = kvp.Value
});
return saveData;
containerID = saveData.containerID;
containerInventory.Clear();
currentWeight = 0f;
if (item != null)
}
}
UpdateUI();
if (saveManager != null)
saveManager.SaveContainer(this);
}"