Jump to content
  • Advertisement
Sign in to follow this  
ukdeveloper

Design considerations for static keyword

This topic is 3757 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

I'm redesigning one of the classes in my game and I wanted a spot of advice. Consider the following:
namespace Asteroids
{
    class Bullet_New
    {
        private static Texture2D bulletTexture;

        public Bullet_New(ContentManager cm)
        {
            bulletTexture = cm.Load<Texture2D>("bullet"); // Hmm...
        }

    }
}
As you know, when I use static it means all objects of this type share the same Texture2D, which I've called bulletTexture. I'm doing this so that I don't store the texture inside each class instance individually which could cause too much memory to be used up at runtime. The sprite itself isn't terribly big in terms of memory anyway but I'm just trying to economise and trying to improve my practices. However, inside the constructor I'm calling the Load on a ContentManager I'm passing in. I'm a bit confused, if that gets called automatically on the creation of each individual Bullet_New object does that make the static bulletTexture redundant? Why load what's already being loaded at another time if you see what I mean? Remembering of course the idea is that the Texture2D is supposed to be already initialised and shared amongst all objects of this type. Thanks in advance, ukd.

Share this post


Link to post
Share on other sites
Advertisement
There's no need to make sure you only load something from the content pipeline once...if you load the same asset multiple times you'll just get multiple references to the same instance. Isn't XNA wonderful? [smile]

ContentManager.Load

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!