Visual Studio Tip – Create Templates

on

When you have to code the same thing over and over again, it’s great to make yourself a template in Visual Studio to speed up your workflow, let’s take a quick look at how you do this in Visual Studio 2012, but earlier versions should apply as well.

You can take any codefile you have and turn it into a template, and after that you will be able to create this codefile from the Add New Item dialog. The great thing about templates is that you can use special variables that will be replaced upon creating of the template. For example, when you create a new class, a template is used where the class name will be replaced by the name you enter in the Add New Item dialog box.

These template parameters start and end with a $ sign, like $itemname, $projectname$, $year$, $clrversion$ and so on, you can find an overview of them here

With that said and done, let’s code! One common thing I have to do in game projects is creating new scenes that fit in our scene manager framework (containing basic includes, basic overriding method calls). I’ve replaced all the class names by $itemname$ which in turn will be replaced by the right class name later on.

codeOverviewThat file is finished, so time to make it into a template. Go to File -> Export template and you’ll get a Mage, err, Wizard for template creation. You can choose between a Project Template or an Item Template. A Project Template exports your whole current project as a template, and you’ll be able to create a new project based on that. This is the way the Windows Store templates have been created, and can give you a workable base to get started. We only need to turn one codefile into a template, so we’ll pick Item Template.

Next up, we can select which codefile to export, we can only export one file, which is a bit sad, but for C# that’s not really a problem.  For C++ (as the code example above) you’ll have to create a seperate template for both the header and source file, but for me that’s still a considerable speed gain instead of typing or copying/changing existing files every time I’d need a new GameScene.

Untitled

Next up, we fill in some additional details press Finish, and we’re done!

selectTemplateWhenever we want to use this template, we can simply add a new item to our project (right click our project -> Add -> New Item), and you’ll see this template appear in there.  After it’s been created the file will be there like above, but with the $itemname$ parameter replaced by the item’s name you entered in the dialog box.

addNewItem

Templates are a really handy way of speeding up your workflow, and you really should use them if you find yourself doing the same thing over and over.

 

Advertisements

One Comment Add yours

  1. Note: If you do it the manual way, you can create multi-file templates.
    I have for instance template set up for a full class and different patterns (Singleton, etc)

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s