Quantcast
Channel: Questions in topic: "saves"
Viewing all articles
Browse latest Browse all 14

Save an array to file

$
0
0
Hi i need help to save my game. I've searched so much but i haven't found anything or it could be that im searching for the wrong things Oh well, the problem is i need to save my inventory[] array which store itemIds like inventory[1] is itemid 1 and that means i got it in my inventory. here is the script i'm using, which i also found somewhere around here. I'm quite new to programming and there is alot i don't understand but if you could tell me what i'm doing wrong or how to fix it i would be very glad!. Inventory.things[] is an array copied array of inventory[], i think atleast.. well the last part of the inventory script will be in the end of the post. using UnityEngine; using System.Collections; using System.Text; using System.IO; using System.Runtime.Serialization.Formatters.Binary; using System; using System.Runtime.Serialization; using System.Reflection; [Serializable] public class SaveData : ISerializable { public bool foundGem1 = false; public float Hp = 0; public float PlayerMoney = 0; public static string theTime = System.DateTime.Now.ToString("hh:mm:ss"); public static string theDate = System.DateTime.Now.ToString("MM/dd/yyyy"); public int[] thinglist = new int[100]; int test ; // The default constructor. Included for when we call it during Save() and Load() public SaveData () {} // This constructor is called automatically by the parent class, ISerializable // We get to custom-implement the serialization process here public void timeSave() { theTime = System.DateTime.Now.ToString("hh:mm:ss"); theDate = System.DateTime.Now.ToString("MM/dd/yyyy"); } public void Incluce () { Hp = PlayerHp.curHealth; PlayerMoney = Inventory.money; } public SaveData (SerializationInfo info, StreamingContext ctxt) { // Get the values from info and assign them to the appropriate properties. Make sure to cast each variable. // Do this for each var defined in the Values section above foundGem1 = (bool)info.GetValue("foundGem1", typeof(bool)); PlayerHp.curHealth = (float)info.GetValue("Hp", typeof(float)); theTime = (string)info.GetValue ("Time", typeof(string)); theDate = (string)info.GetValue("Date",typeof(string)); Inventory.money = (int)info.GetValue("PlayerMoney", typeof(int)); Inventory.amount = (int)info.GetValue("amount", typeof(int)); for(int i = -1;i < Inventory.things.Length;i++) Inventory.things[i] = (Inventory.things[])(info.GetValue ("things" , typeof(Inventory.things[]))); Debug.Log(Inventory.things); switch (Inventory.amount) { default: break; } } // Required by the ISerializable class to be properly serialized. This is called automatically public void GetObjectData (SerializationInfo info, StreamingContext ctxt) { // Repeat this for each var defined in the Values section info.AddValue("foundGem1", (foundGem1)); info.AddValue("Hp", PlayerHp.curHealth); info.AddValue("PlayerMoney", Inventory.money); info.AddValue ("Time", theTime); info.AddValue ("Date", theDate); info.AddValue("amount",Inventory.amount); info.AddValue ("things", Inventory.things); } } // === This is the class that will be accessed from scripts === public class SaveLoad { public static string currentFilePath ="SaveData.sav"; //SaveData.theDate + " "+ SaveData.theTime + ".sav"; //"SaveData.sav" ; // Edit this for different save files private string GetUniqueName(string name, string folderPath) { folderPath = "Game"; name = "SaveData"; string validatedName = name; int tries = 1; while (File.Exists(folderPath + validatedName)) { validatedName = string.Format("{0} [{1}]", name, tries++); } return validatedName; // currentFilePath = validatedName; } // Call this to write data public static void Save () // Overloaded { Save (currentFilePath); } public static void Save (string filePath) { SaveData data = new SaveData (); Stream stream = File.Open(filePath, FileMode.Create); BinaryFormatter bformatter = new BinaryFormatter(); bformatter.Binder = new VersionDeserializationBinder(); bformatter.Serialize(stream, data); stream.Close(); } // Call this to load from a file into "data" public static void Load () { Load(currentFilePath); } // Overloaded public static void Load (string filePath) { SaveData data = new SaveData (); Stream stream = File.Open(filePath, FileMode.Open); BinaryFormatter bformatter = new BinaryFormatter(); bformatter.Binder = new VersionDeserializationBinder(); data = (SaveData)bformatter.Deserialize(stream); stream.Close(); // Now use "data" to access your Values } } // === This is required to guarantee a fixed serialization assembly name, which Unity likes to randomize on each compile // Do not change this public sealed class VersionDeserializationBinder : SerializationBinder { public override Type BindToType( string assemblyName, string typeName ) { if ( !string.IsNullOrEmpty( assemblyName ) && !string.IsNullOrEmpty( typeName ) ) { Type typeToDeserialize = null; assemblyName = Assembly.GetExecutingAssembly().FullName; // The following line of code returns the type. typeToDeserialize = Type.GetType( String.Format( "{0}, {1}", typeName, assemblyName ) ); return typeToDeserialize; } return null; } } I know it isnt clean with all the comments and that but i have tried alot of things. Prefs is an earlier save system of the inventory but i want to it this way anyway. Script 2: public static int amount; int items = amount; public static int[] things = new int[100]; public void blaa() { Debug.Log("FOodkfds"); } public void SaveInventory() { /* foreach(Item item in inventory){ if(item.itemID == i) return true; itemname = item.itemName; */ /*for (int i = 0; i >= -1; i--) things [i] = -1; */ for (int i = 0; i < inventory.Count; i++) { // mycket bra ide!!!!!!!!!!!!!! if(InventoryContains(i)) things[i] = i; /* foreach(Item item in inventory){ if(item.itemID == i) itemname = item.itemName int test = things[i]; if (test == inventory[i].itemID) { things[i] = test; } */ //amount = inventory[i].itemID; print (amount + " amount"); /* items = inventory[i].itemID; print (inventory[i]"); */ } //PlayerPrefs.SetInt ("inventory " + i,inventory[i].itemID); PlayerPrefs.SetInt("money",money); } public void LoadInventory() { // for (int i = 0; i < inventory.Count; i++) { // inventory [i] = -1 + i >= 0 ? database.items [amount] : new Item (); int[] inventory = (int[]) things.Clone();// ? database.items[i] : new Item(); // things.CopyTo(inventory, 0); //} // inventory[i] = PlayerPrefs.GetInt("inventory " + i, -1) >= 0 ? database.items[PlayerPrefs.GetInt("inventory " + i)] : new Item(); print (PlayerHp.curHealth); money = PlayerPrefs.GetInt("money" + money); } } All answers are appreciated! :D

Viewing all articles
Browse latest Browse all 14

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>