Custom Savers Enemy Variables

Announcements, support questions, and discussion for the Dialogue System.
Post Reply
MeyOrMeyNot
Posts: 7
Joined: Mon May 16, 2022 5:49 am

Custom Savers Enemy Variables

Post by MeyOrMeyNot »

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)

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;
    }
}
User avatar
Tony Li
Posts: 20775
Joined: Thu Jul 18, 2013 1:27 pm

Re: Custom Savers Enemy Variables

Post by Tony Li »

Hi,

Add some Debug.Log() lines to your RecordData() and ApplyData() methods, or set breakpoints in your code editor, to make sure those methods are being called.

Change scenes using any of these techniques.

And make sure your EnemyStatsSaver component has a unique Key value.
Post Reply