Stefan Toller

Hi!

I have been working with XNA for about 2 weeks now and I am new to the C# language.
In my game I have a TextureManager and I wanted to give every class access to it.
So I thought Game Services are a great idea, but it doesn't work right.

In the Texturemanager-Class constructor I add it into the Services:
Services.AddService(typeof(Texturemanager), this);

Then I made an instance from the Texturemanager in my MainClass and after it I wanted to get the object back.

ITexturemanager texturemanager = (ITexturemanager)Services.GetService(typeof(ITexturemanager));

But the texturemanager value is always null! Why doesn't it work right

thx

cya,
Stefan






Re: XNA Game Studio Express Problems with AddService()

Leaf.

You're adding your object with typeof(Texturemanager) but trying to retrieve the service with typeof(ITexturemanager), they are not the same type. Use ITexturemanager when you add the service.

Cheers,
Leaf.






Re: XNA Game Studio Express Problems with AddService()

Stefan Toller

Sorry, when I posted the thread I put away the namespaces and I removed one char to much by ITexturemanager. In my code I have always the same type.

Maybe the problem isn't in the two lines I posted before, so I show you more of my code.

// This is the interface
public interface ITexturemanager
{
int Add(String filename);
....
....
void DeleteAllTextures();
}


// This is my Texturemanager class
public class Texturemanager : Microsoft.Xna.Framework.Game,ITexturemanager
{
#region Private Attributes
.....
...
public Texturemanager(String Mediapath)
{
mediapath = Mediapath;
Services.AddService(typeof(Redwoods.ITexturemanager), this);

}
...
}




Re: XNA Game Studio Express Problems with AddService()

sheepsteak

Not sure whether it's just a typo but shouldn't your TextureManager class derive from GameComponent and not Game




Re: XNA Game Studio Express Problems with AddService()

Shawn Hargreaves - MSFT

sheepsteak wrote:
Not sure whether it's just a typo but shouldn't your TextureManager class derive from GameComponent and not Game


That looks like the problem.

Once you fix that, you will notice that you don't have a Services property in your TextureManager any more. That is being inherited from the Game base class, which means you are adding the service to the Services collection of the TextureManager instance, when really you want to add this to the Services of the main game object.

You will either have to pass in Services from the game to the TextureManager constructor, or what I think would be cleaner, do this outside the TextureManager entirely, and have your Game instance register the service:

Services.Add(new TextureManager());






Re: XNA Game Studio Express Problems with AddService()

Greg Christensen

I like to put the call to AddService in my objects constructor as Stefan did. I initially adopted this approach to be consistent with the Framework design: GraphicsDeviceManager accepts a Game parameter (game) and calls game.Services.AddService in the constructor. Also, if you derive from GameComponent you take a Game parameter anyways, in which case I similarly like to register it as a component in the constructor.

public class TextureManager : GameComponent, ITextureManager
{

public TextureManager(Game game) : base(game)
{

base.Game.Services.AddService(typeof(ITextureManager), this);
base.Game.Components.Add(this);

}

}





Re: XNA Game Studio Express Problems with AddService()

Stefan Toller

Thank you all for the great answers!

Sheepsteak was right, I had to derive from GameComponent and not from Game. Now it works fine. :)

And the idea from Creg to add it to the components looks also good, I think I will do it the same way! thx