Jump to content
  • Advertisement
Sign in to follow this  
Moe

[XNA] When to use game services, when to use GameComponents...

This topic is 4113 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hey y'all, I am just wondering if there is any sort of definitive rule on when to use GameComponents and when to use game services. Take for example, something I have been toying around with a little bit. Right now I have a DrawableGameComponent called a console. It doesn't do much right now except print a few lines of text at the top of the screen. I am thinking that it might be better to break it into a service and a DrawableGameComponent. That way other parts of the progam can have access to it (by requesting that service). The Services would in turn use the DrawableGameComponent to draw the actual text. Is this a better way of doing things, or am I confused about the architexture of XNA programs?

Share this post


Link to post
Share on other sites
Advertisement
Sounds logical. You actually don't even have to make two classes to do that. Services don't have to be interfaces. So this works just fine: (Edit: don't shoot me if the code isn't 100% right, I'm just typing this in from memory)


MyConsoleClass console = new MyConsoleClass(this);
Components.Add(console);
Services.AddService(typeof(MyConsoleClass), console);

And then any other component can get it:

MyConsoleClass console = (MyConsoleClass)Game.Services.GetService(typeof(MyConsoleClass));

So you don't even have to break it up to expose it as a service.

Basically when thinking about the two you use a game component if you need to draw something or do something every frame (update). You use a service if you need access to something from within all the game components. You would use both if you have something you need to draw/update and you need access to it from other components.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!