Custom AttributeManager Saver Issues
Posted: Tue Jan 07, 2025 5:03 am
Hi Tony
I've searched high and wide and cannot solve the following issue:
- Using UIS/UCC Savers.
- Have implemented some custom savers from the Saver class successfully.
However, having problems with the following:
1. Flashlight, pickup. Have attached the DestructibleSaver (On Destroy/Destroy).
2. On the Flashlight prefab the player uses, I have an Attribute named Battery, controlled via the UCC AttributeManager.
3. I'm trying to save/load that data.
4. I've created a custom Saver that is a stripped down version of your UCC Saver, as I see you can handle Attributes there (and the UCCSaver works fine for the many Attributes I have on the character). Script below.
The above is able to SAVE the data, no problem. However I cannot manage to get the data to load. I've tried many things including subscribing to events.
I also have other issues, which I'll mention as an FYI.
- Some times, the Flashlight prefab re-appears after load, other times it doesn't.
- I end up with NULL references to the custom Saver script on the Flashlight (I guess the instance of it dies during the LOAD state).
Your guidance on this is really appreciated.
Thanks, clinton
--
I've searched high and wide and cannot solve the following issue:
- Using UIS/UCC Savers.
- Have implemented some custom savers from the Saver class successfully.
However, having problems with the following:
1. Flashlight, pickup. Have attached the DestructibleSaver (On Destroy/Destroy).
2. On the Flashlight prefab the player uses, I have an Attribute named Battery, controlled via the UCC AttributeManager.
3. I'm trying to save/load that data.
4. I've created a custom Saver that is a stripped down version of your UCC Saver, as I see you can handle Attributes there (and the UCCSaver works fine for the many Attributes I have on the character). Script below.
The above is able to SAVE the data, no problem. However I cannot manage to get the data to load. I've tried many things including subscribing to events.
I also have other issues, which I'll mention as an FYI.
- Some times, the Flashlight prefab re-appears after load, other times it doesn't.
- I end up with NULL references to the custom Saver script on the Flashlight (I guess the instance of it dies during the LOAD state).
Your guidance on this is really appreciated.
Thanks, clinton
--
Code: Select all
using System;
using System.Collections.Generic;
using UnityEngine;
using PixelCrushers;
using Opsive.UltimateCharacterController.Traits;
namespace desiderium
{
[RequireComponent(typeof(AttributeManager))]
public class AttributeSaver : Saver
{
[Serializable]
public class Data
{
public List<float> attributeValues = new List<float>();
public List<float> attributeMins = new List<float>();
public List<float> attributeMaxes = new List<float>();
}
private Data m_Data = new Data();
public override string RecordData()
{
var attributeManager = GetComponent<AttributeManager>();
if (attributeManager == null)
{
return string.Empty;
}
m_Data.attributeValues.Clear();
m_Data.attributeMins.Clear();
m_Data.attributeMaxes.Clear();
for (int i = 0; i < attributeManager.Attributes.Length; i++)
{
var attr = attributeManager.Attributes[i];
m_Data.attributeValues.Add(attr.Value);
m_Data.attributeMins.Add(attr.MinValue);
m_Data.attributeMaxes.Add(attr.MaxValue);
}
var serialized = SaveSystem.Serialize(m_Data);
return serialized;
}
public override void ApplyData(string dataString)
{
if (string.IsNullOrEmpty(dataString)) return;
var newData = SaveSystem.Deserialize<Data>(dataString);
if (newData == null)
{
return;
}
var attributeManager = GetComponent<AttributeManager>();
if (attributeManager == null)
{
return;
}
var attrCount = attributeManager.Attributes.Length;
var dataCount = newData.attributeValues.Count;
var count = Mathf.Min(attrCount, dataCount);
bool hasMinData = (newData.attributeMins.Count == dataCount);
bool hasMaxData = (newData.attributeMaxes.Count == dataCount);
for (int i = 0; i < count; i++)
{
if (hasMinData) attributeManager.Attributes[i].MinValue = newData.attributeMins[i];
if (hasMaxData) attributeManager.Attributes[i].MaxValue = newData.attributeMaxes[i];
attributeManager.Attributes[i].Value = newData.attributeValues[i];
}
}
public override void Start()
{
SaveSystem.loadEnded += OnLoadEnded;
}
public override void OnDisable()
{
SaveSystem.loadEnded -= OnLoadEnded;
}
private void OnLoadEnded()
{
SaveSystem.ApplySavedGameData();
}
}
}