Sprite references not saving on load.

Discussion and help for Easy Save 3
Post Reply
StaticCG
Posts: 3
Joined: Sat Dec 31, 2022 4:42 am

Sprite references not saving on load.

Post by StaticCG »

Hi!

First off, this asset is amazing. Thank you for your great work.

Serialization sucks, especially in Unity so this is great.

What I'm trying to do is load tiles from a save file, however the reference to the sprite gets lost on load.

Here is my SaveSystem script:

Code: Select all

using System;
using System.Collections;
using System.Collections.Generic;
using System.Threading;
using UnityEngine;
using UnityEngine.Tilemaps;

public class SaveSystem : MonoBehaviour
{
    public Player player;
    public Transform playerTransform;
    public Crop[] crops;
    public Item[] inventoryItems;
    public Inventory inventory;
    public TilemapRenderer floorTilemapRenderer;
    public Tilemap floorTilemap;

    public PlayerBalance pBalance;

    public Sprite femaleSprite;

    void OnApplicationPause(bool pauseStatus)
    {
        if (pauseStatus)
            Save();
    }

    private void Awake()
    {
        //Load player name and gender even if we have never saved.
        player.name = ES3.Load<string>("PlayerName");
        bool gender = ES3.Load<bool>("Gender");

        if (gender)
        {
            //if true female character sprites should be loaded
            player._animator.runtimeAnimatorController = player.femaleAnimator;
            player.gameObject.GetComponent<SpriteRenderer>().sprite = femaleSprite;
        } 
        
        if (ES3.KeyExists("PlayerPosistion"))
        {
            floorTilemap = ES3.Load<Tilemap>("FloorTilemap");
            BoundsInt bounds = ES3.Load<BoundsInt>("Bounds");
            TileBase[] allTiles = ES3.Load<TileBase[]>("AllTiles");
            floorTilemap.SetTilesBlock(bounds, allTiles);

            inventory.hoeEquipped = ES3.Load<Item>("HoeEquipped");
            inventory.shovelEquipped = ES3.Load<Item>("ShovelEquipped");
            List<Item> tempArray = ES3.Load<List<Item>>("Inventory");
            inventory.items.Clear();
            foreach(Item item in tempArray)
            {
                inventory.items.Add(item);
            }
            ES3.LoadInto("PlayerPosistion", playerTransform);

            //Load all crop data
            for(int i = 0; i < crops.Length; i++)
            {
                string cropName = crops[i].cropName;
                crops[i].isEmpty = ES3.Load<bool>("crop" + i + "isEmpty");
                crops[i].readyToHarvest = ES3.Load<bool>("crop" + i + "readyToHarvest");
                crops[i].growthTime = ES3.Load<float>("crop" + i + "growthTime");
                crops[i].interval = ES3.Load<float>("crop" + i + "interval");
                crops[i].spriteIndex = ES3.Load<int>("crop" + i + "spriteIndex");
                crops[i].cropName = ES3.Load<string>("crop" + i + "cropName");
                crops[i].emptySlot = ES3.Load<UnityEngine.Sprite>("crop" + i + "emptySlot");
                crops[i].spriteRenderer = ES3.Load<UnityEngine.SpriteRenderer>("crop" + i + "spriteRenderer");
                crops[i].animator = ES3.Load<Animator>("crop" + i + "animator");
                crops[i].playerAnimator = ES3.Load<Animator>("crop" + i + "playerAnimator");
                crops[i].inventory = ES3.Load<Inventory>("crop" + i + "inventory");
                crops[i].isInsideCrop = ES3.Load<bool>("crop" + i + "isInsideCrop");
                crops[i].counter = ES3.Load<float>("crop" + i + "counter");
                crops[i].spriteAtlas = ES3.Load<Sprite[]>("crop" + i + "spriteAtlas");
                switch (cropName)
                {
                    case "Wheat":
                        crops[i].wheatAtlas.CopyTo(crops[i].spriteAtlas, 0);
                        break;
                    case "Red Apple":
                        crops[i].redAppleTreeAtlas.CopyTo(crops[i].spriteAtlas, 0);
                        break;
                    case "Blue Apple":
                        crops[i].blueAppleTreeAtlas.CopyTo(crops[i].spriteAtlas, 0);
                        break;
                    case "Turnip":
                        crops[i].turnipAtlas.CopyTo(crops[i].spriteAtlas, 0);
                        break;
                    case "Orange":
                        crops[i].orangeTreeAtlas.CopyTo(crops[i].spriteAtlas, 0);
                        break;
                    case "Green Apple":
                        crops[i].greenAppleTreeAtlas.CopyTo(crops[i].spriteAtlas, 0);
                        break;
                }
            }
            pBalance.gold = Int32.Parse(ES3.Load("Gold").ToString());
        }
    }

    private void Start()
    {
            InvokeRepeating("Save", 180f, 180f);
    }

    public void Save()
    {
        try
        {
            BoundsInt bounds = floorTilemap.cellBounds;
            TileBase[] allTiles = floorTilemap.GetTilesBlock(bounds);
            //Get tiles in tilemap and then save them
            ES3.Save("AllTiles", allTiles);
            ES3.Save("Bounds", bounds);
            ES3.Save("FloorTilemap", floorTilemap);
            ES3.Save("FloorTilemapRenderer", floorTilemapRenderer);
            ES3.Save("HoeEquipped", inventory.hoeEquipped);
            ES3.Save("ShovelEquipped", inventory.shovelEquipped);
            ES3.Save("PlayerPosistion", playerTransform);
            ES3.Save("Gold", pBalance.gold);
            ES3.Save("Inventory", inventory.items);
            //Save all crop data
            if (crops.Length != 0) { 
                for (int i = 0; i < crops.Length; i++)
                {
                    ES3.Save("crop" + i + "isEmpty", crops[i].isEmpty);
                    ES3.Save("crop" + i + "readyToHarvest", crops[i].readyToHarvest);
                    ES3.Save("crop" + i + "growthTime", crops[i].growthTime);
                    ES3.Save("crop" + i + "interval", crops[i].interval);
                    ES3.Save("crop" + i + "spriteIndex", crops[i].spriteIndex);
                    ES3.Save("crop" + i + "cropName", crops[i].cropName);
                    ES3.Save("crop" + i + "emptySlot", crops[i].emptySlot);
                    ES3.Save("crop" + i + "spriteAtlas", crops[i].spriteAtlas);
                    ES3.Save("crop" + i + "spriteRenderer", crops[i].spriteRenderer);
                    ES3.Save("crop" + i + "animator", crops[i].animator);
                    ES3.Save("crop" + i + "playerAnimator", crops[i].playerAnimator);
                    ES3.Save("crop" + i + "inventory", crops[i].inventory);
                    ES3.Save("crop" + i + "isInsideCrop", crops[i].isInsideCrop);
                    ES3.Save("crop" + i + "counter", crops[i].counter);
                }
            }
            Debug.Log("Successfully saved game data!");
        }
        catch(Exception ex)
        {
            Debug.Log("Failed to save game data!\n"+ex);
        }
    }

}
The errors are :
Reference for UnityEngine.Sprite with ID 4735998202259677281 could not be found in Easy Save's reference manager
and
Reference for UnityEngine.Texture2D with ID 1871183610814004230 could not be found in Easy Save's reference manager.


Thank you for your time!
User avatar
Joel
Moodkie Staff
Posts: 4826
Joined: Wed Nov 07, 2012 10:32 pm

Re: Sprite references not saving on load.

Post by Joel »

Hi there,

This usually means that a reference to the Sprite and Texture doesn’t exist in the scene prior to runtime.

In this case if you right-click the Sprites and Textures and select Easy Save 3 > Add References to Manager while your scene is open, it will add those references to the manager so that they can be loaded.

All the best,
Joel
Joel @ Moodkie Interactive
Purchase Easy Save | Contact | Guides | Docs | Getting started
StaticCG
Posts: 3
Joined: Sat Dec 31, 2022 4:42 am

Re: Sprite references not saving on load.

Post by StaticCG »

Joel wrote: Sat Dec 31, 2022 9:38 am Hi there,

This usually means that a reference to the Sprite and Texture doesn’t exist in the scene prior to runtime.

In this case if you right-click the Sprites and Textures and select Easy Save 3 > Add References to Manager while your scene is open, it will add those references to the manager so that they can be loaded.

All the best,
Joel
Hi Joel,

So, do I add a reference for each tile in my assets folder? These tiles are being set at runtime as the player can modify the terrain. The tilemap sets the tile at the grid location below the player. So a new tile is added, but doesnt exist in the scene until its loaded by

Code: Select all

TileBase[] allTiles = ES3.Load<TileBase[]>("AllTiles");
How do I add a reference to these sprites and texture2D files to the ES3 manager?

Thank you!
User avatar
Joel
Moodkie Staff
Posts: 4826
Joined: Wed Nov 07, 2012 10:32 pm

Re: Sprite references not saving on load.

Post by Joel »

Hi there,

You right click the Sprite in the Assets folder and select Easy Save 3 > Add References to Manager.

You can try right-clicking the Tile in the Assets folder also, which should gather the dependencies of that Tile.

All the best,
Joel
Joel @ Moodkie Interactive
Purchase Easy Save | Contact | Guides | Docs | Getting started
StaticCG
Posts: 3
Joined: Sat Dec 31, 2022 4:42 am

Re: Sprite references not saving on load.

Post by StaticCG »

Hello again Joel!

I added references to the sprites, and the tile. However its still not finding the reference to the tile, and the tiles are not loading properly.
UnityEngine.Tilemaps.Tile with ID 8828674674294541922 could not be found in Easy Save's reference manager
I've added the sprites, the texture2d file and the tile itself to the reference manager, however it still throws this error. Any ideas?

Footage of the error in question: https://streamable.com/aysxxk
User avatar
Joel
Moodkie Staff
Posts: 4826
Joined: Wed Nov 07, 2012 10:32 pm

Re: Sprite references not saving on load.

Post by Joel »

Hi there,

Please could you replicate this in a new project with a simple scene and private message it to me with instructions.

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