Strange behavior when loading from file after restarting

Discussion and help for Easy Save 3
Post Reply
okaybenji
Posts: 3
Joined: Mon Oct 28, 2019 9:36 pm

Strange behavior when loading from file after restarting

Post by okaybenji »

Hoping for some help with ES3.

I have 9 Prefabs, each with the ES3 Prefab Script and one child Cube. Each child cube has a Mesh Filter, Mesh Renderer, Box Collider, Rigidbody. I've tried this experiment both with those children having and not having the ES3 Prefab script attached. (Should it be attached or no?) The child cubes all have the Tag "Pillar".

Saving to and loading from a file works fine during a single game session, but if I stop the game then start it back up before loading, when I load some of the cubes disappear due to having their Mesh Filter changed from Cube to None. Other cubes do not disappear, but do have "Easy Save 3 Loaded GameObject" siblings appear in the hierarchy for some reason. These new siblings have a Mesh Filter with no mesh, a Mesh Renderer, and Box Collider, but for whatever reason no Rigidbody. There are always 5 cubes which get these siblings, and it is always the same 5.

Clicking on one of these siblings shows ES3 Inspector Info "This GameObject was created because Easy Save could not find a GameObject in the scene with the same instance ID as the GameObject the Component we are loading is attached to. To prevent this from being created, use the LoadInto methods to tell Easy Save what Component the data should be loaded into."

The thing is, I am using LoadInto. Here's the code (note that I am not testing the LoadFromCache method, only the LoadFromFile method):

Code: Select all

using System.IO;
using System.Linq;
using UnityEngine;

public interface ITaskable {};

public class SaveSystem : MonoBehaviour, ITaskable {
  static ES3File _es3;
  static string _path;
  static GameObject[] gameState;

  // Initialize the save cache.
  public void Start() {
    // Set the file path (this is ES3's default path, aka ES3Settings.directory).
   _path = Application.persistentDataPath + "/Saves/";

    // Get reference to the game state.
    // (For now, we are just tracking the pillars.)
    gameState = GameObject.FindGameObjectsWithTag("Pillar");

    // Create a new temporary save cache.
    _es3 = new ES3File("temp.es3");
  }

    // Retrieve save files, sorted by date/time.
  static IOrderedEnumerable<FileInfo> GetSaveFiles() => new DirectoryInfo(@_path).GetFiles("*.es3").OrderByDescending(x => x.LastWriteTime);

  // Save the game state to memory and file.
  public static void Save() {
    SaveToCache();
    SaveCacheToFile();
  }

  // Save the game state to memory.
  public static void SaveToCache() => _es3.Save<GameObject[]>("gameState", gameState);

  // Commit the save cache to the file system.
  public static void SaveCacheToFile() {
    // Create the Saves folder if it does not exist.
    System.IO.Directory.CreateDirectory(_path);

    int maxSaves = 3;

    // Name file with date.
    string filename = $"Saves/{System.DateTime.Now:yyyy-MM-dd_hh-mm-ss-tt}.es3";

    // Write the cache to file.
    _es3.Sync(filename);

    // Delete any save files after maxSaves.
    int i = 1;
    foreach (FileInfo file in GetSaveFiles()) {
      if (i > maxSaves) {
        file.Delete();
      }
      i++;
    }
  }

  // Load the save cache into the game.
  public static void LoadFromCache() => _es3.LoadInto<GameObject[]>("gameState", gameState);

  // Load the most recent save file.
  public static void LoadFromFile() {
    // Get the most recent save file.
    FileInfo file = GetSaveFiles().FirstOrDefault();

    // Bail if there are no save files.
    if (file == null) {
      return;
    }

    // Load the file into the game.
    ES3.LoadInto<GameObject[]>("gameState", file.ToString(), gameState);

    // Commit the current state into the save cache.
    SaveToCache();
  }
}
okaybenji
Posts: 3
Joined: Mon Oct 28, 2019 9:36 pm

Re: Strange behavior when loading from file after restarting

Post by okaybenji »

Just some additional details from further testing: I get the same behavior whether I use Load or LoadInto, and I get the same behavior whether the cubes are prefabs or unpacked (with or without the ES3 Prefab script removed). The only difference when they are not prefabs is that the siblings which appear are just called "Cube" and do not have the ES3 Inspector Info.
User avatar
Joel
Moodkie Staff
Posts: 4826
Joined: Wed Nov 07, 2012 10:32 pm

Re: Strange behavior when loading from file after restarting

Post by Joel »

Hi there,

It's not possible to tell what is happening from what you've said, but it sounds like there's not a reference to your Mesh at runtime. Please could you PM me a basic project which replicates this?

All the best,
Joel
Joel @ Moodkie Interactive
Purchase Easy Save | Contact | Guides | Docs | Getting started
okaybenji
Posts: 3
Joined: Mon Oct 28, 2019 9:36 pm

Re: Strange behavior when loading from file after restarting

Post by okaybenji »

The information I provided seems to me to be complete, but in any case I have sent you a PM with a project attached.
fishluck
Posts: 3
Joined: Mon Apr 20, 2020 9:41 pm

Re: Strange behavior when loading from file after restarting

Post by fishluck »

Hi,

I think my issue is similar to this -

I have an issue with saving meshes (I'm using autosave)
My project has a building tool, and prefabs for doors etc which are placed in the world at runtime. The doors are prefabs with easy save prefab and auto save components, and a child object with the mesh filter  and renderer.

When I place a door, save using autosave and then load again, the door object is intact except for the mesh filter - there is no longer a reference to a mesh, there is a reference to basically a null mesh but with the name of the game object where the mesh name is. (The mesh name was originally door4x8_mesh_00, it's on a gameobject called door, when I save and load the game, the mesh is called door but there is no mesh)

 I tried manually changing the mesh at runtime through the inspector and then autosaving, the mesh saved fine then. 

I've looked through the forums and have followed any advice I found, for instance enabling the prefab right clicking on it, checking that easy save is saving reference and value, and I also tried to keep a reference to the right mesh in the game so that the prefab would load it from there on Start, but it didn't work - the mesh remains with a reference to None.

My other prefabs are saving fine - I have other building objects and they are saving with all children and mesh filters etc, I don't see what is different about the doors.

These aren't procedural meshes or changed at runtime, they have just been placed in the scene.
I've also tried things like messing with the properties of Unity components MeshFilter, Mesh saved by EasySave but no luck.

Any help would be great, thank you.

** Edit - I found a fix which seems to be working but no idea why, I added a game object with an empty mesh filter as the first child of the prefab, and now it saves and loads the rest of the meshes perfectly.

I'm also seeing weird behaviour -every so often the components that can be checked / unchecked under the prefab tab in the easy save window become checked that I've unchecked. Just a few dotted around the list will be checked after definitely being unchecked. I have removed everything, the plugin, cleared player prefs, removed the components relating to easy save attached to my prefabs - these components keep getting re-selected.

I'd love to be able to clear any previous settings and just reset everything I've ever changed to do with the plugin, in case it's affecting other things / causing weird behaviour. I have been tinkering with the settings a lot over the past few days so I have likely made a mess of things somewhere.
Thanks!
User avatar
Joel
Moodkie Staff
Posts: 4826
Joined: Wed Nov 07, 2012 10:32 pm

Re: Strange behavior when loading from file after restarting

Post by Joel »

Hi there,

Please could you private message me a basic project and instructions which replicate this so I can see what is happening from my end?

All the best,
Joel
Joel @ Moodkie Interactive
Purchase Easy Save | Contact | Guides | Docs | Getting started
Post Reply