0
\$\begingroup\$

By default the SpriteBatch instance used is instantiated in the LoadContent method of Game1.

For example see here

However a SpriteBatch only requires a GraphicsDevice object to be instantiated. Said GraphicsDevice is actually already available when the Initialize method is created, so my question is why is the SpriteBatch created in LoadContent?

I think that both methods are run only once per game, so don't see why you can't/shouldn't instantiate the SpriteBatch object in the Initialize method?

\$\endgroup\$
1
  • \$\begingroup\$ Your question got me curious and I found this answer which you might find helpful. I would be very interested to see if it's still possible to trigger a device reset in MonoGame and more importantly if it actually causes issues having code in the wrong method. \$\endgroup\$ Aug 3, 2019 at 11:56

1 Answer 1

0
\$\begingroup\$

If your IDE has a decompiler, you may want to explore the base Game class. You will see:

protected virtual void LoadContent()
{
}

protected virtual void Initialize()
{
  this.applyChanges(this.graphicsDeviceManager);
  this.InitializeExistingComponents();
  this._graphicsDeviceService = (IGraphicsDeviceService) this.Services.GetService(typeof (IGraphicsDeviceService));
  if (this._graphicsDeviceService == null || this._graphicsDeviceService.GraphicsDevice == null)
    return;
  this.LoadContent();
}

So, LoadContent gets called conditionally, if graphics device has been setup correctly. If you want to preserve this logic, leave SpriteBatch initialization there.

But if you move it to Initialize, nothing horrible will happen.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .