Custom Savers Enemy Variables
Posted: Fri Apr 05, 2024 9:33 am
Hi,
I recently started working on a turn based combat game and I want to save certain ints and bools of the enemy, I wrote a custom saver but it doesn't seem to be working. In the Battle Scene the Health and bool isDead gets set correctly in the EnemyController script but when I Load the Overworld scene the Enemy stats have reset. Am I missing something? Also this script and the EnemyController are on the Enemy GameObject.( I apply a DontDestroyOnLoad method with on Trigger so I take the enemy into the Battle Scene and change the health etc. Its probably not the smartest way but it works so far)
I recently started working on a turn based combat game and I want to save certain ints and bools of the enemy, I wrote a custom saver but it doesn't seem to be working. In the Battle Scene the Health and bool isDead gets set correctly in the EnemyController script but when I Load the Overworld scene the Enemy stats have reset. Am I missing something? Also this script and the EnemyController are on the Enemy GameObject.( I apply a DontDestroyOnLoad method with on Trigger so I take the enemy into the Battle Scene and change the health etc. Its probably not the smartest way but it works so far)
Code: Select all
using System;
using UnityEngine;
using PixelCrushers;
[RequireComponent(typeof(EnemyController))]
public class EnemyStatsSaver : Saver
{
[Serializable]
public class Data
{
public int health;
public bool isDead;
}
public override string RecordData()
{
var enemyController = GetComponent<EnemyController>();
var data = new Data();
data.health = enemyController.currentHealth;
data.isDead = enemyController.isDead;
return SaveSystem.Serialize(data);
}
public override void ApplyData(string s)
{
if (string.IsNullOrEmpty(s)) return; // If we didn't receive any save data, exit immediately.
var data = SaveSystem.Deserialize<Data>(s);
if (data == null) return; // If save data is invalid, exit immediately.
var enemyController = GetComponent<EnemyController>();
enemyController.currentHealth = data.health;
enemyController.isDead = data.isDead;
}
}