i found the solution
Need Help with Save/Load Function
Hey, guys, I'm building Strategy/CityBuilding type of game and I'm trying to implement a saving system but saved items didn't instantiate I can't figure out what's problem I took this code from guide I read and change a little bit for my game but it didn't work here's code.
this is code on gameobject i want to save
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
public class EmptySpaceRegister : MonoBehaviour
{
public void Start()
{
GlobalControl.SaveEvent += SaveFunction;
}
public void OnDestroy()
{
GlobalControl.SaveEvent -= SaveFunction;
}
public void SaveFunction(object sender, EventArgs args)
{
EmptySpaceLocs emptyspace = new EmptySpaceLocs();
emptyspace.PositionX = transform.position.x;
emptyspace.PositionY = transform.position.y;
emptyspace.PositionZ = transform.position.z;
GlobalControl.Instance.GetListForScene().SavedEmptySpaces.Add(emptyspace);
}
}
this is GlobalController code that controls save system
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;
using UnityEngine.SceneManagement;
public class GlobalControl : MonoBehaviour
{
public static GlobalControl Instance;
public List<AllBuildingLocs> BuildingList;
public delegate void SaveDelegate(object sender, EventArgs args);
public static event SaveDelegate SaveEvent;
void Awake()
{
Application.targetFrameRate = 144;
if (Instance == null)
{
DontDestroyOnLoad(gameObject);
Instance = this;
}
else if (Instance != null)
{
Destroy(gameObject);
}
}
public void InitializeSceneList()
{
if(BuildingList == null)
{
print("building list is null");
BuildingList = new List<AllBuildingLocs>();
}
bool found = false;
for(int i = 0; i < BuildingList.Count; i++)
{
if (BuildingList[i].SceneID == SceneManager.GetActiveScene().buildIndex)
{
found = true;
print("Scene was found in saved lists!");
}
}
if (!found)
{
AllBuildingLocs newList = new AllBuildingLocs(SceneManager.GetActiveScene().buildIndex);
BuildingList.Add(newList);
print("Created new list!");
}
}
public AllBuildingLocs GetListForScene()
{
for (int i = 0; i < BuildingList.Count; i++)
{
if (BuildingList[i].SceneID == SceneManager.GetActiveScene().buildIndex)
return BuildingList[i];
}
print("Total list count: " + BuildingList.Count.ToString() + " , not found index: " + SceneManager.GetActiveScene().buildIndex.ToString());
return null;
}
public CurrencyData LocalCopyOfCurrency;
public bool IsSceneBeingLoaded = false;
public bool IsSceneBeingTransitioned = false;
public CurrencyManager currency;
public void CurrencyColb()
{
LocalCopyOfCurrency.Gold = currency.GoldData;
LocalCopyOfCurrency.Food = currency.FoodData;
LocalCopyOfCurrency.Wood = currency.WoodData;
LocalCopyOfCurrency.Stone = currency.StoneData;
}
public void FireSaveEvent()
{
GetListForScene().SavedEmptySpaces = new List<EmptySpaceLocs>();
if (SaveEvent != null)
SaveEvent(null, null);
}
public void SaveData()
{
if (!Directory.Exists("Saves"))
Directory.CreateDirectory("Saves");
FireSaveEvent();
BinaryFormatter formatter = new BinaryFormatter();
FileStream saveFile = File.Create("Saves/save.binary");
FileStream SaveObjects = File.Create("saves/saveObjects.binary");
CurrencyColb();
formatter.Serialize(saveFile, LocalCopyOfCurrency);
formatter.Serialize(SaveObjects, BuildingList);
saveFile.Close();
SaveObjects.Close();
print("Saved!");
}
public void LoadData()
{
BinaryFormatter formatter = new BinaryFormatter();
FileStream saveFile = File.Open("Saves/save.binary", FileMode.Open);
FileStream saveObjects = File.Open("Saves/saveObjects.binary", FileMode.Open);
LocalCopyOfCurrency = (CurrencyData)formatter.Deserialize(saveFile);
BuildingList = (List<AllBuildingLocs>)formatter.Deserialize(saveObjects);
saveFile.Close();
saveObjects.Close();
currency.GoldData = LocalCopyOfCurrency.Gold;
currency.FoodData = LocalCopyOfCurrency.Food;
currency.WoodData = LocalCopyOfCurrency.Wood;
currency.StoneData = LocalCopyOfCurrency.Stone;
print("Loaded");
}
public void SaveButton()
{
SaveData();
}
public void LoadButton()
{
LoadData();
}
}
and this is spawner code
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MapManager : MonoBehaviour
{
public GameObject EmptySpacePrefab;
void Start()
{
GlobalControl.Instance.InitializeSceneList();
if (GlobalControl.Instance.IsSceneBeingLoaded || GlobalControl.Instance.IsSceneBeingTransitioned)
{
AllBuildingLocs localList = GlobalControl.Instance.GetListForScene();
if (localList != null)
{
print("Saved potions count: " + localList.SavedEmptySpaces.Count);
for (int i = 0; i < localList.SavedEmptySpaces.Count; i++)
{
GameObject EmptySpace = (GameObject)Instantiate(EmptySpacePrefab);
EmptySpace.transform.position = new Vector3(localList.SavedEmptySpaces[i].PositionX,
localList.SavedEmptySpaces[i].PositionY,
localList.SavedEmptySpaces[i].PositionZ);
}
}
else
print("Local List was null!");
}
}
}
in case if you guys ask this is savedata code im using
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[System.Serializable]
public class CurrencyData
{
public float Gold;
public float Food;
public float Wood;
public float Stone;
}
[System.Serializable]
public class EmptySpaceLocs
{
public float PositionX, PositionY, PositionZ;
}
[System.Serializable]
public class AllBuildingLocs
{
public int SceneID;
public List<EmptySpaceLocs> SavedEmptySpaces;
public AllBuildingLocs(int newSceneID)
{
this.SceneID = newSceneID;
this.SavedEmptySpaces = new List<EmptySpaceLocs>();
}
}
Follow this Question
Related Questions
Saveing and Loading Problem 0 Answers
Save and load player position in main scene 1 Answer
How to load a score form a saved game vs. the level just completed 0 Answers
Trouble Saving and Loading Serialization 0 Answers
I am trying to make a dat file decoder to decode my game data but I can't figure it out please help 0 Answers