Best way for Cloud Variables

What is the best way to acces varriables from Game1 class to another class…

Couple ways I do it so far:

  1. I pass references in the constructor so a class can access the other objects any time it needs to (seems to be fairly common)
  2. If you have things that are use in numerous places and will end up with a huge list of things to pass you can cut back some of those with a static class which carries those references. ie:
    static public class g {
    static public ContentManager Content;
    static public GraphicsDevice gpu;
    static public SpriteBatch spriteBatch;
    static public QuadBatch quadBatch;
    static public int screenW, screenH;
    … ETC
    This is often not recommended but it does allow one to easily mindlessly get away with using whatever you need from wherever you need to for certain things that are used almost everywhere. ie:
    if (g.player.pos.X<200) // inside something that normally has nothing to do with player (need only once)

However I think usually in a team environment, ppl prefer to avoid static too much and allow access to be granted through a reference passed into the constructor (as far as I know).

1 Like

I use a global static class that all classes can reference.

I use the service model that’s part of the MonoGame framework.

I use a combination of references, static and service model. Service model is really helpful for accessing members in other projects. Reference is very common. Static is quick and dirty.