Hi,
This is my first time using the forums for help, so I will try to get to the point and hopefully, I am using the correct forum help section.
I have a Game Manager Script that Extends from a Singleton Script. Here is an example of that Manager script:
using UnityEngine;
using System.Collections;
public class GameManager : Singleton<GameManager> {
//Player HP
private float _playerHealth;
public float PlayerHealth
{
get { return _playerHealth; }
set { _playerHealth = value; }
}
//Player MP
private float _playerMagic;
public float PlayerMagic
{
get { return _playerMagic; }
set { _playerMagic = value; }
}
//
//Items for Getting and Setting:
//
//Number Sparkles
private int _numSparks;
public int NumSparks {
get { return _numSparks; }
set { _numSparks = value; }
}
//Number Hearts
private int _numHearts;
public int NumHearts {
get { return _numHearts; }
set { _numHearts = value; }
}
.
.
.[/code]
Taking an example from the script, I want to save the _numHearts value for permanence. But before I do that, another script accesses those values:
void OnTriggerEnter2D(Collider2D collider)
{
if (collider.gameObject.tag == "Player")
{
if(gameObject.name == "SparkleItem") {
SparklesPickup();
}
else if (gameObject.name == "HeartItem") {
HeartPickup();
}
.
.
.
//Picking up ALL in-game ITEMS. I.E. Coins, Sparkles, Mushrooms, Ore, ect.
private void SparklesPickup()
{
GameManager.Instance.NumSparks++;
SaveLoad.control.readSparks+=1;
Destroy (gameObject);
}
private void HeartPickup()
{
GameManager.Instance.NumHearts++;
SaveLoad.control.readHearts+=1;
Destroy (gameObject);
}
.
.
.
Then I have another script that is the UI output, and finally, I have the SaveLoad script:
using UnityEngine;
using System.Collections;
using System;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;
public class SaveLoad: MonoBehaviour {
public static SaveLoad control; //can access from anywhere now <<
public float health;
//public float experience;
//Attempt To save the Item --- Collects Between 2 levels: S
public int readSparks;
public int readHearts;
void Awake () {
if (control == null) {
DontDestroyOnLoad (gameObject);
control = this;
}
else if(control !=this) {
Destroy (gameObject);
}
}
///
/// Saving And Loading ///
[Serializable]
class PlayerData {
public float health;
//public float experience;
public int classSparks;
public int classHearts;
}
public void Save() {
BinaryFormatter bf = new BinaryFormatter ();
FileStream file = File.Create (Application.persistentDataPath + "/playerInfo.dat");
PlayerData mydata = new PlayerData ();
mydata.health = health;
//mydata.experience = experience;
mydata.classSparks = readSparks;
mydata.classHearts = readHearts;
bf.Serialize (file, mydata);
file.Close ();
}
public void Load() {
if(File.Exists(Application.persistentDataPath + "/playerInfo.dat")) {
BinaryFormatter bf = new BinaryFormatter();
FileStream file = File.Open (Application.persistentDataPath + "/playerInfo.dat", FileMode.Open); //<--file is a local var to Load
PlayerData mydata = (PlayerData)bf.Deserialize(file);
file.Close ();
health = mydata.health;
//experience = mydata.experience;
readSparks = mydata.classSparks;
readHearts = mydata.classHearts;
}
}
I shortened these scripts to post my question here, but I think the overall idea of what I’m trying to do is illustrated. How can I access the numHearts, for example, which is in the GameManager and store it in a container to write save data to, and later retrieve. I tried FindObjectOfType<>(); in the Start menu, and then assigning readHearts to = GAmeManager.Instance of _numHearts.
I am not sure how I should be handling this problem, actually. I have a hard time finding information on storing data to a binary file as it seems most tutorials cover Player Prefs. I tried using BinaryWriter too but I haven’t been able to make effective use out of it because I’m using it wrong. I just haven’t been able to figure it out yet.
Is someone able to notice what I’m doing wrong using this method? Otherwise I can re-explain or go into more detail.
Thanks!