Save System Help

Started by
0 comments, last by KoldGames 10 years, 5 months ago

Hello! I am trying to create a save system and I am having problems.


[System.AttributeUsage(AttributeTargets.Property | AttributeTargets.Field)]
	public class NXSaveElement : Attribute
	{
		public string ElementName { get; set; }
		
		public NXSaveElement(string elementName)
		{
			ElementName = elementName;
		}
	}
	
	public abstract class INXSave
	{
		public abstract string Name { get; }
		
		public void Save(string dir)
		{
			string finalFile = Path.Combine(dir, Name);
			
			using (BinaryWriter writer = new BinaryWriter(File.Open(finalFile,FileMode.OpenOrCreate)))
			{
				foreach (PropertyInfo info in this.GetType().GetProperties())
				{
					foreach (object obj in info.GetCustomAttributes(true))
					{
						Attribute attribute = (Attribute)obj;
						
						if (attribute is NXSaveElement)
						{
							NXSaveElement element = (NXSaveElement)attribute;
							writer.Write(element.ElementName);
							//WRITE VALUE HERE
						}
					}
				}
			}
		}
	}
	
	public class NXPlayerSave : INXSave
	{
		public override string Name {
			get {
				return "player";
			}
		}
		
		[NXSaveElement("position")]
		public Vector3 Position { get; set; }
		
		[NXSaveElement("rotation")]
		public Vector3 Rotation { get; set; }
		
		[NXSaveElement("health")]
		public float Health { get; set ; }
		
		public NXPlayerSave(NXPlayer player)
		{
			Position = player.Position;
			Rotation = new Vector3(player.Camera.Yaw, player.Camera.Pitch, 0);
			Health = player.Health;
		}
	}

I would like to get the value of objects I have my NXSaveElement attribute attached to so I can write the value to a binary file but I don't know how to get the value. Is it possible? Thanks! :)

Advertisement

Never mind, I fixed it! :)

This topic is closed to new replies.

Advertisement