Jump to content
  • Advertisement
Sign in to follow this  
watermelonChris

XNA Access variables from base class in game component

This topic is 2476 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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?

Share this post


Link to post
Share on other sites
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. :)

Share this post


Link to post
Share on other sites

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 ?

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!