XNA Creators Club Online
Page 1 of 1 (6 items)
Sort Posts: Previous Next

Inheriting from DrawableGameComponent

Last post 09/11/2009 0:55 by Kapps. 5 replies.
  • 07/11/2009 18:46

    Inheriting from DrawableGameComponent

    So,

    Another problem.

    I tried writing this class but I can't since a static class cannot inherit from DrawableGameComponenet.

    What should I do?
    Try not. Do, or do not. There is no try.
  • 07/11/2009 19:18 In reply to

    Re: Inheriting from DrawableGameComponent

    Why are you trying to make it static?
    Jim Perry - Microsoft XNA MVP
    If people spent a minute searching the forums and reading the FAQs before posting I'd be out of a job.
      Got some XNA Game Studio/XNA Framework development info to share with the community? Put it on the XNA Wiki.
        Please mark posts as Answers or Good Feedback when appropriate.
  • 07/11/2009 22:01 In reply to

    Re: Inheriting from DrawableGameComponent

    If there is only going to be 1 instance, implement it as a singleton. It is a lot more flexible, as you're finding out now.

    Chris
    Now in playtest: Chris Unarmed - Blog | Trailer | Facebook
  • 08/11/2009 6:37 In reply to

    Re: Inheriting from DrawableGameComponent

    Static classes can never be derived from any other class, for a few reasons.
    The alternative I use in this case is to make a Singleton, and just make all of the other methods/properties static. Because your instance must be initialized with a parameter for Game, if you don't have access to the Game, then you can just create an Initialize method with Game passed to it that generates the instance instead.


  • 09/11/2009 0:26 In reply to

    Re: Inheriting from DrawableGameComponent

    I couldn't get it to work.

    My problem is passing the "Game" reference to it, how can I pass a Game reference to it when it is generated on run time by CLR?
    I spend all day reading tutorials but couldn't find out how to integrate a Singleton pattern into XNA.
    Try not. Do, or do not. There is no try.
  • 09/11/2009 0:55 In reply to

    Re: Inheriting from DrawableGameComponent

    Here's a simple example. I haven't tested it, but it should work.

        public class SingletonClass : DrawableGameComponent { 
     
            private SingletonClass(Game game) 
                : base(game) { 
     
            } 
     
            private static SingletonClass _Instance; 
            public static SingletonClass Instance { 
                get { return _Instance; } 
                private set { _Instance = value; } 
            } 
     
            public static void Initialize(Game game) { 
                _Instance = new SingletonClass(game); 
            } 
        } 

    The idea is, the class itself isn't static, but all methods inside the class excluding the ones from DrawableGameComponent are static. If you really need them to be static also though, you can make a static method that calls Instance.WhateverNonStaticMethod(). Initialize would have to be called in order to pass along a reference to Game (this is your main class, the one that derives from Game, the one created when you create a project, Game1 by default), so that it can get passed to the constructor for Instance. Alternatively, you could change that private set to be public and then just have a different class initialize it.

    Also, since Instance is accessible, you don't actually have to make any methods static, you could just access them through SingletonClass.Instance.DoWhatever(), if DoWhatever isn't static. If it is static, just SingletonClass.DoWhatever(). There are advantages to both, though I think making them non-static and accessing them through Instance is the generally used way since it allows you to have more than one Instance if need be.
Page 1 of 1 (6 items) Previous Next