0

Possible Duplicate:
C# virtual static method

Idea:

public class Item
{
      public Vector2 Position;
      virtual static Sprite mySprite;
      public void Draw() {Draw mySprite at Position}
}

public class Couch:Item
{
      override static Sprite mySprite=someCouchImage;
}
public class Table:Item
{
      override static Sprite mySprite=someTableImage;
}

Explanation There are many Tables and Couches in the game world, each with their unique Positions. Each Table has the same Sprite as the next Table. It seems silly that when I have 100 Tables, there are 100 Sprites.

Question Is there a way so that all Tables share the same Sprite without having 100 Sprites (while also being referencable in the Item class?)

Community
  • 1
  • 1
Dennis Benson
  • 11
  • 1
  • 6

3 Answers3

1

Obviously, C# doesn't support this.
Here's a simple way to achieve something pretty close:

public class Item
{
      public Vector2 Position;
      static Sprite mySprite;
      protected virtual Sprite getMySprite() { return mySprite; } // Virtualize getting the sprite
      public void Draw() {Draw getMySprite() at Position}
}

public class Couch:Item
{
      static Sprite mySprite=someCouchImage;
      override Sprite getMySprite() { return mySprite; } // Get the custom sprite
}
public class Table:Item
{
      static Sprite mySprite=someTableImage;
      override Sprite getMySprite() { return mySprite; } // Get the custom sprite
}
Scott Rippey
  • 15,614
  • 5
  • 70
  • 85
  • This works well! Heh, I thought I tried this already, but it works :) – Dennis Benson Nov 12 '11 at 04:26
  • One comment to anyone planning on using this: If you want to reference an Item.mySprite, it will return the Item's mySprite instead of Couch's mySprite. Casting (Couch)TargetItem.mySprite works iff you know it's a Couch – Dennis Benson Nov 12 '11 at 05:38
  • You're not really supposed to access static members from an instance ... You're supposed to use `Couch.mySprite`, or use `targetItem.getMySprite()` – Scott Rippey Nov 12 '11 at 20:15
0

I didn't try to compile this, but would something like this work for you?

public class Item
{
    public Vector2 Position;

    protected static Dictionary<Type, Sprite> sprites = new Dictionary<Type, Sprite>();
    public static void RegisterSprite(Type type, Sprite sprite)
    {
        sprites.Add(type, sprite);
    }

    public void Draw() {
            Draw sprites.Item(typeof(this)) at Position
    }
}

public class Couch:Item {}
public class Table:Item {}

And then register your sprites from some convenient location. This is admittedly bad for separation of concerns, since you're pushing the responsibility of managing the Sprites on somebody else than the Couch and Table classes who should be responsible for it, I guess. But it's the best I can come up with at this hour.. :)

Gaute Løken
  • 7,522
  • 3
  • 20
  • 38
  • This would be great implementation if the resources were fixed - this idea might be pretty helpful for a subtly different problem. Thanks! – Dennis Benson Nov 12 '11 at 04:33
0

Instead of creating a virtual static variable, just create a regular property that returns a sprite based on the type of the class (i.e. switch on "this.GetType().ToString()" in the get accessor)).

Brandon Moore
  • 8,590
  • 15
  • 65
  • 120