Code Snippets in XNA

Do you find yourself typing the same thing over and over again? Maybe it’s the code to draw an effect (begin, end and the pass loop) or maybe you keep having to resolving a render target to a texture. Wouldn’t it be nice if you didn’t have to type all that code every time?

Enter code snippets! Visual Studio has a nice feature that will insert small bits of code for you, even allowing you to specify different fields within that code. For example, the “prop” snippet will insert the following code for you:

private int myVar;

public int MyProperty
{
get { return myVar;}
set { myVar = value;}
}

It’ll let you specify the type of the variable “int”, the name of the variable “myVar” and the name of the property “MyProperty” just by tabbing between the fields too. Go ahead, try it – open Visual Studio and type “prop”, then press enter in the intellisense popup and then enter the values you want.

Using the prop code snippet.

I could easily change it to a Vector3 type variable called position with a property name of Position.

Editing the prop code snippet.

“That’s great,” you may say, “but it doesn’t help me set up my drawing code any faster.” Fortunately, that’s not entirely true. While there isn’t a “SetUpMyDrawingCode” code snippet, you can make one! Code snippets are just XML files that define the code to insert, the variables to allow the user to edit, and a few extra things about what namespaces might need to be included and who created the snippet. You just drop these in your “Visual Studio 2005\Code Snippets\Visual C#\My Code Snippets” (probably in your My Documents directory) and then reload Visual Studio (or add them manually via Tools -> Code Snippets Manager).

There’s plenty of information on the internet on how to create your own code snippets. There’s a nice article written by Anand Kumar at dotnetjunkies that can walk you through creating a simple code snippet. You can find it here. I’d also suggest reading the Code Snippets Documentation on MSDN. It provides a lot more information about the little nuances that you might want to explore with more complex code snippets.

I’ve written some simple XNA code snippets and put them up for download here. You can install them by unzipping them to your “Visual Studio 2005\Code Snippets\Visual C#\My Code Snippets” and then loading Visual Studio (and if they don’t load, add the folder manually using Tools -> Code Snippets Manager in Visual Studio). This includes the following snippets:

  • DrawEffect - Sets up the effect drawing code, including the “forall passes” loop.
  • PlatformVariable - Creates a platform-dependent variable, using the method discussed in my previous post on sharing code between platforms.
  • ResolveRenderTarget - Resolves the specified render target, stores its contents in a texture, and then sets an effect parameter to the value of that texture.
  • ThreadStarter – Sets up and starts a thread containing user-defined code.

I made these pretty quick, so they don’t contain the best code in the world and they’ll probably need some references resolved for proper use, but I hope they make a good starting point for you to make your own code snippets.

One word of advice: make sure you have an XML syntax validator. Incorrectly formatted snippets won’t load at all, and there’ll be no error message telling you what’s wrong. I personally use Notepad++ with the XML Tools plugin for all of my XML editing needs. It’ll even format my XML files for me so they look nice. Visual Studio has a pretty nice XML editor too though, so use whatever you’re comfortable with.

Post some of your own code snippets here to let me know what you come up with.

Leave a Reply