none
deriving from a static class RRS feed

  • Question

  • I am writing an rpg engine and I don't want the game to full of spell instances hogging unnecessary memory.

    If player A, B, C or monster X, Y, Z use the Fireball spell, nothing changes except the parameters (like user and target), but the Fireball itself, properties and methods, do not change at all.

    Normally I would just make a static class, but the problem is that there are more spells and I want to derive child classes like Fireball from a base Spell class.

    Is there a workaround to make something akin to deriving a static class from another static class?

    Because I am pretty sure the spell class will change over time and I don't want to copypaste the code of a spell all the time.

    Sunday, November 13, 2011 2:00 AM

Answers

  • Why creating a static class, at least using OOP will probably give you a better use of the memory as all OOP program languages are meant to use with objects and not with fixed memory modules (another name for static classes).

    It makes you then also easier to use polymorphism

     http://en.wikipedia.org/wiki/Polymorphism_(computer_science)


    Success
    Cor
    Sunday, November 13, 2011 10:04 AM

  • Have you considered a singleton class?  It is described as well as shown here: http://msdn.microsoft.com/en-us/library/ff650316.aspx.

    --
    Mike
    Sunday, November 13, 2011 3:07 AM
  • Hi,

     

    You can use the Strategy Design pattern in this case. You have a base class called Spells, then you have concrete implementations of different types of Spells like (Fireball etc.) which have their own behavior. You can find more information here: http://msdn.microsoft.com/en-us/magazine/cc188707.aspx#S6

     

    Let me know if you need any code example.


    Jinish
    Sunday, November 13, 2011 3:09 AM
  • I think strategy pattern will be good but you can think aslo about Dependency injection as will .

    You said that you have Base spell class and Fire Spell Class that derive from Base spell class. by using  Dependency injection with little modification on your can do the something as following

     

    //Base Spell
    interface ISpell {
      void Attack();
    }
    // Child Spell
    class EarthSpell: ISpell {
      public void Attack() {
        Console.WriteLine("Earth Spell");
      }
    }
    
    //Child Spell
    class FireBall: ISpell {
      public void Attack() {
        Console.WriteLine("Fire Ball Spell");
      }
    }
    
    class Wizard{
      private ISpell _Spell 
      public Wizard(IWeapon Spell) {
        _Spell = Spell 
      }
      public void Fire(string target) {
        _Spell.Attack(target);
      }
    }
    
    class Program {
      public static void Main() {
     /// Different player can use different spells
        Wizard wizard1 = new Wizard(new EarthSpell());
        Wizard wizard2= new Wizard(new FireBall());
        warrior1.Fire();
        warrior2.Fire();
      }
    }
    

     


    As you can see we changed from base class to interface and each child inherit this interface with it's own implementation and create new players and inject them with different type of spells.

    of course this just a sample, to know more about dependency injection in .net take a look to the following links 

    Ninject

    Inversion of Control and Dependency Injection: Working with Windsor Container

    Dependency Injection with Autofac

    I hope that help

    Regards.

     

     


    Ahmed Naji SharePoint Geek
    MCP|MCTS
    My Blog | DotnetFinder

    Sunday, November 13, 2011 10:43 AM
  • Hi Ishiirou,

    It is impossible to drive from a static class, because a static class is sealed. But I think you can create a normal class with static member only, For example:

    class Spell

    {

        public static string Name;

        public static int baseDamage;

     

        public static int Attack(int p)

            {

                return baseDamage + p;

            }

    }

    class FireBall : Spell

    {

        public static int Attack(int p)

        {

            return baseDamage + 2*p;

        }

    }

     

    Best Regards,


    Bob Wu [MSFT]
    MSDN Community Support | Feedback to us
    Monday, November 14, 2011 12:22 PM
    Moderator

All replies


  • Have you considered a singleton class?  It is described as well as shown here: http://msdn.microsoft.com/en-us/library/ff650316.aspx.

    --
    Mike
    Sunday, November 13, 2011 3:07 AM
  • Hi,

     

    You can use the Strategy Design pattern in this case. You have a base class called Spells, then you have concrete implementations of different types of Spells like (Fireball etc.) which have their own behavior. You can find more information here: http://msdn.microsoft.com/en-us/magazine/cc188707.aspx#S6

     

    Let me know if you need any code example.


    Jinish
    Sunday, November 13, 2011 3:09 AM
  • Why creating a static class, at least using OOP will probably give you a better use of the memory as all OOP program languages are meant to use with objects and not with fixed memory modules (another name for static classes).

    It makes you then also easier to use polymorphism

     http://en.wikipedia.org/wiki/Polymorphism_(computer_science)


    Success
    Cor
    Sunday, November 13, 2011 10:04 AM
  • I think strategy pattern will be good but you can think aslo about Dependency injection as will .

    You said that you have Base spell class and Fire Spell Class that derive from Base spell class. by using  Dependency injection with little modification on your can do the something as following

     

    //Base Spell
    interface ISpell {
      void Attack();
    }
    // Child Spell
    class EarthSpell: ISpell {
      public void Attack() {
        Console.WriteLine("Earth Spell");
      }
    }
    
    //Child Spell
    class FireBall: ISpell {
      public void Attack() {
        Console.WriteLine("Fire Ball Spell");
      }
    }
    
    class Wizard{
      private ISpell _Spell 
      public Wizard(IWeapon Spell) {
        _Spell = Spell 
      }
      public void Fire(string target) {
        _Spell.Attack(target);
      }
    }
    
    class Program {
      public static void Main() {
     /// Different player can use different spells
        Wizard wizard1 = new Wizard(new EarthSpell());
        Wizard wizard2= new Wizard(new FireBall());
        warrior1.Fire();
        warrior2.Fire();
      }
    }
    

     


    As you can see we changed from base class to interface and each child inherit this interface with it's own implementation and create new players and inject them with different type of spells.

    of course this just a sample, to know more about dependency injection in .net take a look to the following links 

    Ninject

    Inversion of Control and Dependency Injection: Working with Windsor Container

    Dependency Injection with Autofac

    I hope that help

    Regards.

     

     


    Ahmed Naji SharePoint Geek
    MCP|MCTS
    My Blog | DotnetFinder

    Sunday, November 13, 2011 10:43 AM
  • Well, thanks for the feedback but It's not what I'm looking for. The main reason for the inheritance is that the logic for using a spell (damage calculations, etc) should be in the base class and the derived classes only change things like name, base damage, etc. Coincidentally however, I found some kind of solution to another problem I had with the links you provided. A way to tell how big my objects are with GC.GetTotalMemory. Thanks.
    Sunday, November 13, 2011 2:25 PM
  • Hi Ishiirou,

    It is impossible to drive from a static class, because a static class is sealed. But I think you can create a normal class with static member only, For example:

    class Spell

    {

        public static string Name;

        public static int baseDamage;

     

        public static int Attack(int p)

            {

                return baseDamage + p;

            }

    }

    class FireBall : Spell

    {

        public static int Attack(int p)

        {

            return baseDamage + 2*p;

        }

    }

     

    Best Regards,


    Bob Wu [MSFT]
    MSDN Community Support | Feedback to us
    Monday, November 14, 2011 12:22 PM
    Moderator