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

How do I expose access to my GameInstance since it is protected in the default GameComponent?

Last post 9/9/2007 2:28 PM by EKLynx. 4 replies.
  • 9/9/2007 1:29 AM

    How do I expose access to my GameInstance since it is protected in the default GameComponent?

    I have the following code, however it gives me an error:

    new public Game Game
    {
    get { return base.Game; }
    }

    and I get the following error:

    Error 1 Cannot implicitly convert type 'Microsoft.Xna.Framework.Game' to 'MyGame.Game'. An explicit conversion exists (are you missing a cast?) K:\MyGame\MyGame\ScreenManager\ScreenManager.cs 47 26 MyGame

    Dead_Illusionist
  • 9/9/2007 2:11 AM In reply to

    Re: How do I expose access to my GameInstance since it is protected in the default GameComponent?

    I usually make a public static member variable that I initialize at the beginning which
    gives everything in my whole program access to my game.

    public static Game ThisGame;

    somewhere else in initialize do this:

    ThisGame = this;

    Also, when you get that error it usually means you're missing a cast.  You might have to put (Game) in front of base.Game there.

    Then anywhere in your code you can do Game.ThisGame to get the game reference.
    Puttin' my foot up on that rail since 2008.
  • 9/9/2007 2:45 AM In reply to

    Re: How do I expose access to my GameInstance since it is protected in the default GameComponent?

    Answer
    Reply Quote

    Why not try and return "this" ?

    Sorry, I misunderstood your question.

    Error 1 Cannot implicitly convert type 'Microsoft.Xna.Framework.Game' to 'MyGame.Game'. An explicit conversion exists (are you missing a cast?

    Your problem is that your main class is called Game. The default name, when created by the IDE is Game1, you probably changed that to Game, so now there is a conflict between classes names.

    I've tried using the following code in a component in a new project, where the main class is named Game1, and it works

     

    new public Game Game
    {
    get { return base.Game; }
    }

     




     

  • 9/9/2007 9:55 AM In reply to

    Re: How do I expose access to my GameInstance since it is protected in the default GameComponent?

    OH OK! Thank you, cause I have an example game engine that I'm going by and it works fine, but it also has a different name... That makes sense, thank you...
    Dead_Illusionist
  • 9/9/2007 2:28 PM In reply to

    Re: How do I expose access to my GameInstance since it is protected in the default GameComponent?

    This might be also something to keep in mind, not sure if it applies to you.

    // Creates class MyGame that inherits from the base class Game
    class MyGame : Game
    {
    // returnsthis instance of your extended MyGame class
    public MyGame GameInstance
    {
    get { return this; }
    }

    // returns the instance of the base class without your
    // custom things you added to MyGame
    public Game BaseInstance
    {
    get { return base; }
    }
    }
Page 1 of 1 (5 items) Previous Next