- Home /
Saving layout and enemy stats with Binary Formatter
So I'm using a binary formatter to save the variables of my script as well as I can. When it comes to the player, everything works perfectly. However I also have an interchanging map aswell as a list of enemies that need to be added aswell. For example, you right click something to activate an animation and delete its collision box. The problem I'm having is, I really need to undo the animation and reset the collision box through a saving and loading system. It's not much of a reverse time rather then a reset to its original variables, including enemy health and location. I would like to be able to list a few enemies, there health, location, and everything else that needs changing, but in order to do that I would need to keep track of the scripts and where they are, but its impossible to save a script prefab, IE:
[System.Serializable]
public class EnemyVariables
{
public enemyEssentials ESScript;
public float enemyHealth;
public float locationX;
public float locationY;
public bool isAlive;
}
will not work. What I currently have is all of that before without actually saving it to my data. IE:
public void Save ()
{
Debug.Log ("Save was started");
BinaryFormatter bf = new BinaryFormatter ();
FileStream file = File.Create (Application.persistentDataPath + "/playerInfo.dat");
PlayerData data = new PlayerData ();
//SAVE DATA HERE//
data.CharacterLocationX = CharacterLocation.x;
data.CharacterLocationY = CharacterLocation.y;
data.Health = Health;
data.Experience = Experience;
data.Level = Level;
data.damageLevel = damageLevel;
data.healthLevel = healthLevel;
data.delayLevel = delayLevel;
data.dodgeLevel = dodgeLevel;
data.ugradeTokens = ugradeTokens;
data.CUGTokens = CUGTokens;
data.SpendableCUGTokens = SpendableCUGTokens;
data.CUGM = CUGM;
data.tutorialTaskVariables = tutorialTaskVariables;
for (int z = 0; z < enemyVariables.Count; z++) {
if (enemyVariables [z].ESScript != null) {
enemyVariables [z].locationX = enemyVariables [z].ESScript.transform.position.x;
enemyVariables [z].locationY = enemyVariables [z].ESScript.transform.position.y;
enemyVariables [z].enemyHealth = enemyVariables [z].ESScript.Health;
enemyVariables [z].isAlive = enemyVariables [z].ESScript.gameObject.activeSelf;
}
}
bf.Serialize (file, data);
file.Close ();
}
as you should be able to tell, enemyVariables is not being saved into the saving function otherwise it would work, but it will save temporarily until you exit and re-enter the game. I was curious if there was a work around without having to use exact gameobjects, transforms or scripts so I can get this working. I also would like to know if there is a way to save the map layout for re-enters because I don't currently have that. Of course I could consider it a "Feature" to re-load the specific stats and not the map layout itself but getting things as polished as I can would be even more beautiful...
Thank you so much in advance! - Seth Albertus
Your answer
Follow this Question
Related Questions
Enemy AI Reloading Problem, they reload with queue 0 Answers
Help with Enemy AI 1 Answer
Deal damage on collision 2 Answers
2D sidescroller ranged character shooting problem. 0 Answers
Enemy Spaceship Movement AI Curve Movement Need help Please 0 Answers