1
\$\begingroup\$

I have a spritebatch in my primary game component in a game project:

public class GameRoot : Game
{
    private SpriteBatch mySriteBatch;
    public SpriteBatch MySpriteBatch 
    {
        get { return mySpriteBatch; }
        set { mySpriteBatch = value; }
    }

I wish to access MySpriteBatch in a library class:

    public override void Draw(GameTime gameTime)
    {
        sprite.Draw(GameRoot.SpriteBatch, gameTime);

This does not work since GameRoot is derived from Game, which is passed to the class, and is not known in the library project.

How do I resolve this?

\$\endgroup\$
2
  • \$\begingroup\$ What do you mean by library? Is it another project in your solution that your main game project depends on? \$\endgroup\$ Commented Feb 6, 2016 at 7:55
  • \$\begingroup\$ Yes, I have many classes in a library project dependent on my main game project. \$\endgroup\$ Commented Feb 6, 2016 at 15:14

1 Answer 1

1
\$\begingroup\$

It's kind of hard to understand what you're trying to do here, but for one I would recommend passing instances as parameters anywhere you can, it will lead to clearer code in the end:

public override void Draw(GameTime gameTime, SpriteBatch spriteBatch)
{
    ...
}

Depending on what you mean by library, you could have a few choices. I am currently using VS 2015 and there is a really cool feature I have taken advantage of for monogame, that is the shared project type. You can learn more about it on c# corner.

Shared Project in VS 2015

You can create a shared project like this one and use it as a library for any platform. As a result, I've been able to create a single project that acts as my core game code, and created individual projects for each platform I am targeting, by leveraging the use of platform-specific markers, such as

#IF ANDROID
    DoSomethingSpecific();
    ...
#ENDIF

etc.

If you're worried about performance issues from having multiple SpriteBatch objects in separate components, that should not be an issue (from my experience), I have used dozens of components using separate spritebatch objects, and never had any problems.

If you still want to use the SpriteBatch object contained in your Game root, you may be able to access it like this:

this.SpriteBatch = (SpriteBatch)game.Services.GetService(typeof(GraphicsResource));

But I have not tried it.

Good luck!

\$\endgroup\$
2
  • \$\begingroup\$ Thanks for the response. Actually I do have a class library or shared project containing all my sprites and controls. The library project is referenced by my main project creating a dependency. When I pass a parameter to a class in the library I am unable to downcast to a derived class in the main project because it is not known in the library project. If I define CustomSpriteBatch in the main project and pass it to a class in the library project then the custom methods are unavailable to the library project because CustomSpriteBatch is not knowm. Is there a way to resolve this? \$\endgroup\$ Commented Feb 18, 2016 at 18:44
  • \$\begingroup\$ You might be able to use a partial class if you are in the same namespace, see msdn.microsoft.com/en-us/library/wa80x488.aspx aside from that I'm mostly confused about how you are trying to architect your solution. You should be able to include the microsoft.xna references in your library, and be able to pass in a spritebatch reference from anywhere in your code \$\endgroup\$ Commented Feb 20, 2016 at 19:26

You must log in to answer this question.

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