Jump to content
  • Advertisement
Sign in to follow this  
mohaakilla51

classname

This topic is 4114 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

OK so I have a class called character, and I want it to Serialize when the game is quit. is that what this does? If I were to create:
    [Serializable]
    class Character
    {
        public string type;
        public int health;
        public string name;
        public int strength;
        public int exp;
        public int level;
        public short[] location = new short[2] { 0, 0 };
        public string armor;
        public string weapon;
        public int weapon_lvl;
        public void attack(Character enemy)
        {
            int power = strength + level;
            int enemy_power = enemy.strength + enemy.level;
            int totalpower = power - enemy_power;
            if (totalpower < 1)
                totalpower = 1;
            enemy.health = enemy.health - totalpower;
            Console.WriteLine("{0} attacks {1} with an offensive power of {2}.", name, enemy.name, power);
            Console.WriteLine("{0} defends with a defensive power of {1}", enemy.name, enemy_power);
            Console.WriteLine("{0} does {1} damage to {2}", name, totalpower, enemy.name);
            Console.WriteLine("{0} has {1} health left.", enemy.name, enemy.health);
            if (enemy.health <= 0)
            {
                Console.WriteLine("{0} killed {1}", name, enemy.name);

            }

        }
        ~Character()
        {
            FileStream flStream = new FileStream(Hero.name.sav, FileMode.OpenOrCreate, FileAccess.Write);
            try
            {
                BinaryFormatter binFormatter = new BinaryFormatter();
                binFormatter.Serialize(flStream, Hero);
            }
            finally
            {
                flStream.Close();
            }
        }
    }

Will this save the stats of my hero as the name of the hero?

Share this post


Link to post
Share on other sites
Advertisement
I'm not very familiar with the C# serializing stuff, but this might help you. Try the example and then substitute the example class for your own.

Share this post


Link to post
Share on other sites
Yes, it will serialize the entire class. If you want to omit particular things then there are other attributes you can use to mark out specific fields.

Share this post


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

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!