-1

I'm writing a game in MonoGame and have an entity class which is used by pretty much every game object. While the entity class doesn't have its own defined texture, having the texture member declared inside that class would greatly simplify the code by eliminating the need to override certain virtual functions with the exact same code in every child class.

For instance, I currently have to put this in my Entity class:

public virtual void Draw() { }

and then add this in every single derived class:

public static Texture2D texture;
...
public override void Draw()
        {
            if (active)
                Game1._spriteBatch.Draw(texture, position, Color.White);
        }

I tried various things like new static, but they didn't work. Can someone help, or is it simply not possible in C#? I know I can write a virtual accessor function and override that, but I'd rather not have to write that in every single child class.

  • 1
    Any reason *not* to have the texture in the base class, provided to the base class constructor by the derived class constructor? (It's not clear what you were trying to do with `new static`, but static methods don't participate in inheritance, basically.) – Jon Skeet Sep 05 '20 at 17:19
  • I don't want to set the texture in the base class because every object type (player, enemy, etc.) uses a different one, and I want to make them static for each class to save memory. – RedVibes99 Sep 05 '20 at 17:43
  • It would only be one extra *variable* in each base class object - not a different *object* for each of them. Do you have any evidence that one extra variable would cause a significant issue for you? If you really don't want any state, then the difference between instances *would* have to be via inheritance, so you *would* have to override a method or property in every class. You could do that with a `Texture` property which would be simpler than your `Draw` method, but you'd still need some code in every class. – Jon Skeet Sep 06 '20 at 06:21

2 Answers2

0

This is covered pretty well in this question, so I'd check that out. The short story is that you can use new static, just don't mark the base class' method as virtual. You'll also want to keep in mind the slightly different behavior of new and standard method overrides.

That being said, if you're doing this for memory reasons, are you currently seeing issues? If you're not, I'd pick the design that is cleaner and more maintainable, and then worry about optimizations if issues crop up.

patrickb
  • 422
  • 2
  • 9
0

Making virtual base class members static in derived classes

Not possible, period. Member names are unique and you can not redefine a member name to be static as per C# specifications. You can make ANOTHER member that is static and use a final override of the original member to access this one, but that will leave this one in place.

Redefinitions like this are simply not supported by C#.

I can write a virtual accessor function and override that, but I'd rather not have to write that in every single child class.

THen do a new static one and a final override that basically uses the static one. Done.

TomTom
  • 61,059
  • 10
  • 88
  • 148