Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

The project I'm building for test purposes is working fine but I have dependencies flying around everywhere due to the XNA framework.

My issue is in the content.load textures/sounds/fonts. I have them as public variables (i.e. Texture1 = Content.load(of texture2d)("Texture1") )

I'm passing a 'Game1' pointer into the constructor of every new class being instantiated to gain access to these variables.

Am I missing an OOP trick that would prevent me having to pass a pointer to 'game1' to every New class?

share|improve this question
add comment

2 Answers

up vote 2 down vote accepted

In my opinion the simplest way is just to add a public static reference to your game object, and assign it in the constructor. Has some similarities with using a singleton, but without all the restrictions:

public class Game1 : Game {
    public static Game1 Instance;
    public Game1() { Instance = this; }
}

After which you can easily access it from anywhere, such as Game1.Instance.Texture1.

share|improve this answer
add comment

Do you know something about Singleton pattern? You can create for example GetGameObject function in some file, where you create your game object. This function will return game object (which is created only once).

share|improve this answer
    
Cheers Zach. I have thought of the singleton pattern, but much like a trigger in SQL it feels like a hack/workaround and thus i dismiss it without thinking.I might try it out. This is a good article :jorudolph.wordpress.com/2009/11/22/singleton-considerations ..... was thinking of a separate content class classed called say 'GameContent' using an interface. My objects are dependant on 'game1' but i want it to be loosely coupled. –  LoveofSnow Jul 10 '12 at 13:03
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.