Okay, by default, the Game
class for an XNA PC game has a public bool attribute called IsMouseVisible
, which when set enables/disables the mouse being visible. I have another class called Configs
which monitors specific keys being pressed. Now what I want to do is set it so that when I press a specific key, it will change the value of IsMouseVisible
. This is where I get stuck because I don't know how to change the value from another class.
I know that I could just create a bool value in the Configs
class and make it so that when the key is pressed, the value will change, then make it so that when the main Game
class updates, it sets the value for IsMouseVisible
to the Configs class' value, but the thing is, I might want to create multiple different values (not just IsMouseVisible
) that can be set in the main Game
class from another class.
And there's my question, how could I set something in the main Game
class from another class? (Without having to create multiple updates for each and every value I want to be accessible.)
Create the object 'Configs' in the main class's constructor. While doing this, give the Config objects a reference to the main class. Now Configs can access the main class's value.
You will need to pass a reference of the game class to the Configs class. In the Configs' class constructor, set a private member variable to the instance of the game class. From there you can manipulate the game. See below:
Game Class
public class Game1 : Game
{
Configs configs;
GraphicsDeviceManager _graphics;
SpriteBatch _spriteBatch;
public Game1()
{
_graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
}
protected override void Initialize()
{
// Pass THIS game instance to the Configs class constructor.
configs = new Configs(this);
base.Initialize();
}
protected override void LoadContent()
{
_spriteBatch = new SpriteBatch(GraphicsDevice);
}
protected override void UnloadContent()
{
}
protected override void Update(GameTime gameTime)
{
base.Update(gameTime);
}
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.CornflowerBlue);
base.Draw(gameTime);
}
}
Configs Class
public class Configs
{
// Reference to the main game class.
private Game _game;
public Configs(Game game)
{
// Store the game reference to a local variable.
_game = game;
}
public void SetMouseVisible()
{
// This is a reference to you main game class that was
// passed to the Configs class constructor.
_game.IsMouseVisible = true;
}
}
This is basically what others have been stating. However, it seems you are having a rough time understanding the concepts without seeing some code. Therefore, I provided it.
Hope that helps.
Three things, and they all relate to your comments on the answer from @Patashu:
First, if you are passing a Game
object into your constructor, you don't need to set it as a ref type argument, because it is automatically passed by reference (by virtue of being an object).
Second, the reason your GameComponent
subclass needs a reference to the Game
object in its default constructor...is that it already has a reference to Game
built into it. In your Configs
class, call this.Game
That is the main Game
instance.
Third, If your constructor already recieves one argument of an object, it does not need another argument of the same instance of the same object.
Read the documentation. It's useful.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.