Re: Save System Question ;)
Posted: Mon May 16, 2022 9:22 am
I started adding the changes to my own saver script as recommended...
However, I'm not quite certain how to "hook" the GetItemFromID from its current location to this one...
Its in a script file called InventoryItem.cs (here is its FULL content unedited)
so there is a "public static InventoryItem GetFromID(string itemID)"
I replaced your line :
GetItemFromID(string itemID)
with this :
pickup.item = InventoryItem.GetFromID(data.itemID);
However, now i'm getting a weird error in the data section at the top : (see screenshot)
However, I'm not quite certain how to "hook" the GetItemFromID from its current location to this one...
Its in a script file called InventoryItem.cs (here is its FULL content unedited)
Code: Select all
using System;
using System.Collections.Generic;
using UnityEngine;
using static GameDevTV.Inventories.Equipment;
namespace GameDevTV.Inventories
{
/// <summary>
/// A ScriptableObject that represents any item that can be put in an
/// inventory.
/// </summary>
/// <remarks>
/// In practice, you are likely to use a subclass such as `ActionItem` or
/// `EquipableItem`.
/// </remarks>
public abstract class InventoryItem : ScriptableObject, ISerializationCallbackReceiver
{
// CONFIG DATA
[Tooltip("Auto-generated UUID for saving/loading. Clear this field if you want to generate a new one.")]
[SerializeField] string itemID = null;
[Tooltip("Item name to be displayed in UI.")]
[SerializeField] string displayName = null;
[Tooltip("Item description to be displayed in UI.")]
[SerializeField][TextArea] string description = null;
[Tooltip("The UI icon to represent this item in the inventory.")]
[SerializeField] Sprite icon = null;
[Tooltip("The prefab that should be spawned when this item is dropped.")]
[SerializeField] Pickup pickup = null;
[Tooltip("If true, multiple items of this type can be stacked in the same inventory slot.")]
[SerializeField] bool stackable = false;
[Tooltip("WWise Audio Pickup Clip Name.")]
[SerializeField] string WWisePickupClip = "";
[SerializeField] ItemCategory category;
// STATE
static Dictionary<string, InventoryItem> itemLookupCache;
// PUBLIC
public ItemCategory GetCategory() => category;
/// <summary>
/// Get the inventory item instance from its UUID.
/// </summary>
/// <param name="itemID">
/// String UUID that persists between game instances.
/// </param>
/// <returns>
/// Inventory item instance corresponding to the ID.
/// </returns>
public static InventoryItem GetFromID(string itemID)
{
if (itemLookupCache == null)
{
itemLookupCache = new Dictionary<string, InventoryItem>();
var itemList = Resources.LoadAll<InventoryItem>("");
foreach (var item in itemList)
{
if (itemLookupCache.ContainsKey(item.itemID))
{
Debug.LogError(string.Format("Looks like there's a duplicate GameDevTV.UI.InventorySystem ID for objects: {0} and {1}", itemLookupCache[item.itemID], item));
continue;
}
itemLookupCache[item.itemID] = item;
}
}
if (itemID == null || !itemLookupCache.ContainsKey(itemID)) return null;
return itemLookupCache[itemID];
}
/// <summary>
/// Spawn the pickup gameobject into the world.
/// </summary>
/// <param name="position">Where to spawn the pickup.</param>
/// <param name="number">How many instances of the item does the pickup represent.</param>
/// <returns>Reference to the pickup object spawned.</returns>
public Pickup SpawnPickup(Vector3 position, int number)
{
var pickup = Instantiate(this.pickup);
pickup.transform.position = position;
pickup.Setup(this, number);
return pickup;
}
public Sprite GetIcon()
{
return icon;
}
public string GetItemID()
{
return itemID;
}
public bool IsStackable()
{
return stackable;
}
public string GetDisplayName()
{
return displayName;
}
public string GetDescription()
{
return description;
}
public string GetWWiseString()
{
return WWisePickupClip;
}
// PRIVATE
void ISerializationCallbackReceiver.OnBeforeSerialize()
{
// Generate and save a new UUID if this is blank.
if (string.IsNullOrWhiteSpace(itemID))
{
itemID = System.Guid.NewGuid().ToString();
}
}
void ISerializationCallbackReceiver.OnAfterDeserialize()
{
// Require by the ISerializationCallbackReceiver but we don't need
// to do anything with it.
}
}
}
I replaced your line :
GetItemFromID(string itemID)
with this :
pickup.item = InventoryItem.GetFromID(data.itemID);
However, now i'm getting a weird error in the data section at the top : (see screenshot)