XNA Access variables from base class in game component

Started by
4 comments, last by Flimflam 12 years, 5 months ago
Hey guys.
Can someone please inform me how I should be accessing variables from the base game class in a game component?
Or should I be passing it via the game component's constructor?
Advertisement
Aloha,

You plan to access some member variables of the Game (Microsoft.Xna.Framework.Game) from within a GameComponent, right?
There is a property called "Game" at each GameComponent, see here. That should do the trick. :)

Aloha,

You plan to access some member variables of the Game (Microsoft.Xna.Framework.Game) from within a GameComponent, right?
There is a property called "Game" at each GameComponent, see here. That should do the trick. :)



Yes, I'd like to access public/private variables of my Game class. I can't seem to access it via Game.speed or game.speed ?
Ah, I see. So, you want to access a member in the class you derived from Microsoft.Xna.Framework.Game?
The Game property of the GameComponent only gives you an object of Type Microsoft.Xna.Framework.Game. You have to cast it to your type, e.g.
Game1 myGame = Game as Game1;
This is possible, if Game1 inherits from Microsoft.Xna.Framework.Game. If there is no inheritance, the 'as' returns null.
Well, and then you can access public members and properties, e.g.
myGame.speed;
Is that what you like to do?
Thanks I'll give that a go. :rolleyes:
Also keep in mind the scope. If you're in a GameComponent, you won't be able to access private member variables from your Game class because the GameComponent doesn't have any rights to them.

This topic is closed to new replies.

Advertisement