- Home /
Question by
SparksCool · May 28, 2019 at 12:58 PM ·
loadingvaluesaveloadbinaryformatterstats
i need to return multiple types of values from my save and load functions
my saving script works except that to store my players position i need to use a float and all the other values are in a int array heres the data script and save script and master script that all the player data goes through:
using System;
using System.Collections;
using System.Collections.Generic;
using System.Runtime.Serialization.Formatters.Binary;
using UnityEngine;
[System.Serializable] public class PlayerData {
public int[] PlayerStats;
public float[] PlayerLocation;
public PlayerData (MasterScript player) {
PlayerStats = new int[9];
PlayerStats[0] = player.PlayerLevel;
PlayerStats[1] = player.PlayerXPNeed;
PlayerStats[2] = player.PlayerStamMax;
PlayerStats[3] = player.PlayerStamina;
PlayerStats[4] = player.PlayerMaxHP;
PlayerStats[5] = player.PlayerHP;
PlayerStats[6] = player.PlayerEXP;
PlayerStats[7] = player.PlayerAmmo;
PlayerStats[8] = player.PlayerAmmoMax;
PlayerLocation = new float[2];
PlayerLocation[0] = player.Player.position.x;
PlayerLocation[0] = player.Player.position.z;
}
}
using System;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using System.Xml.Serialization;
using UnityEngine;
public static class SaveSystem {
public static void SavePlayer (MasterScript MS) {
BinaryFormatter bf = new BinaryFormatter ();
FileStream stream = new FileStream (Application.persistentDataPath + "/player.sav", FileMode.Create);
PlayerData data = new PlayerData (MS);
bf.Serialize (stream, data);
stream.Close ();
}
public static int[] LoadPlayer ()
{
if (File.Exists (Application.persistentDataPath + "/player.sav")) {
BinaryFormatter bf = new BinaryFormatter ();
FileStream stream = new FileStream (Application.persistentDataPath + "/player.sav", FileMode.Open);
PlayerData data = bf.Deserialize (stream) as PlayerData;
stream.Close ();
return data.PlayerStats;
} else {
Debug.LogError ("Save File Not Found in " + Application.persistentDataPath);
return null;
}
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[System.Serializable] public class MasterScript : MonoBehaviour {
public int PlayerHP;
public int PlayerMaxHP;
public int PlayerStamMax;
public int PlayerStamina;
public int PlayerLevel;
public int PlayerEXP;
public int PlayerXPNeed;
public int PlayerAmmo;
public int PlayerAmmoMax;
public PlayerHP PHP;
public PlayerAmmo PAM;
public PlayerEXPManager PEXP;
public PlayerStamina PSM;
public Transform Player;
private void Start () {
}
private void Update () {
PlayerHP = PHP.playerCurrentHealth;
PlayerMaxHP = PHP.playerMaxHealth;
PlayerStamMax = PSM.MaxStamina;
PlayerStamina = PSM.CurrentStamina;
PlayerLevel = PEXP.level;
PlayerEXP = PEXP.CurrentEXP;
PlayerXPNeed = PEXP.EXPRequiredForLevelUp;
PlayerAmmo = PAM.PlayerCurrentAmmo;
PlayerAmmoMax = PAM.PlayerMaxAmmo;
}
public void SavePlayer () {
SaveSystem.SavePlayer (this);
}
public void LoadPlayer () {
int[] loadedStats = SaveSystem.LoadPlayer ();
PEXP.level = loadedStats[0];
PEXP.EXPRequiredForLevelUp = loadedStats[1];
PSM.MaxStamina = loadedStats[2];
PSM.CurrentStamina = loadedStats[3];
PHP.playerMaxHealth = loadedStats[4];
PHP.playerCurrentHealth = loadedStats[5];
PEXP.CurrentEXP = loadedStats[6];
PAM.PlayerCurrentAmmo = loadedStats[7];
PAM.PlayerMaxAmmo = loadedStats[8];
}
}
Add comment
Comment
Answer by cs120319992 · May 29, 2019 at 11:02 AM
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using UnityEngine;
namespace SERRVIEX.Data
{
[System.Serializable]
internal static class SaveSystem
{
public enum Path { PersistentDataPath, TemporaryCachePath }
public enum Extension { data, file, local, saves }
public static string GetPath(Path path)
{
if (path == Path.TemporaryCachePath)
return Application.temporaryCachePath;
return Application.persistentDataPath;
}
public static string GetExtension(Extension type)
{
switch(type)
{
case Extension.data: return ".data";
case Extension.file: return ".file";
case Extension.local: return ".local";
case Extension.saves: return ".saves";
default: return ".data";
}
}
/// <summary>
/// Save any serializable object with name, type and path.
/// </summary>
/// <param name="obj">Any serializable object (ex: 'int, float, string, bool')</param>
/// <param name="name">File name (ex: 'Level_1')</param>
/// <param name="extension">Extension file (ex: 'Level_1.data)</param>
/// <param name="path">File path</param>
public static void Save(object obj, string name, Extension extension, Path path)
{
var _extension = GetExtension(extension);
var _path = GetPath(path);
if (!File.Exists(_path + "/" + name + _extension))
File.Create(_path + "/" + name + _extension).Dispose();
var bf = new BinaryFormatter();
using (var fs = File.Open(_path + "/" + name + _extension, FileMode.Open, FileAccess.Write))
{
bf.Serialize(fs, obj);
Debug.Log("[SaveSystem]: Object succesful serialized: " + _path + "/" + name + _extension);
}
}
/// <summary>
/// Load saved obj with name, extension and path.
/// </summary>
/// <returns>The load.</returns>
/// <param name="obj">Any serializable object (ex: 'int, float, string, bool')</param>
/// <param name="name">File name (ex: 'Level_1')</param>
/// <param name="extension">Extension file (ex: 'Level_1.data)</param>
/// <param name="path">File path</param>
/// <typeparam name="T">The 1st type parameter.</typeparam>
public static T Load<T>(T obj, string name, Extension extension, Path path)
{
var output = default(T);
var _extension = GetExtension(extension);
var _path = GetPath(path);
if (!File.Exists(_path + "/" + name + _extension))
{
Debug.Log("File " + _path + "/" + name + _extension + " not found! Returning default value");
return output;
}
var bf = new BinaryFormatter();
using (var fs = File.Open(_path + "/" + name + _extension, FileMode.Open))
{
output = (T)bf.Deserialize(fs);
Debug.Log("[SaveSystem]: Object succesful deserialized");
}
return output;
}
}
}
using UnityEngine;
using SERRVIEX.Data;
using SERRVIEX.Serialization;
public class Example : MonoBehaviour
{
public class MyClass {
public int PlayerHP;
public int PlayerMaxHP;
public int PlayerStamMax;
public int PlayerStamina;
public int PlayerLevel;
public int PlayerEXP;
public int PlayerXPNeed;
public int PlayerAmmo;
public int PlayerAmmoMax;
}
MyClass myClass;
private void Start()
{
SaveSystem.Save(myClass, "FileName", SaveSystem.Extension.data, SaveSystem.Path.PersistentDataPath);
myClass = SaveSystem.Load(myClass, "FileName", SaveSystem.Extension.data, SaveSystem.Path.PersistentDataPath);
}
}