Sign in to follow this  

Save System Help

This topic is 1535 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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! :)

Share this post


Link to post
Share on other sites
Sign in to follow this