Category: C Sharp


Part of my job over summer is to help to prepare software to be deployed into the teaching labs. Mostly this involves harassing staff to find out what software packages they need, but occasionally the problems get a little more interesting. This year that was the case with XNA.

xna_logo

XNA is a great framework for creating games, and we use it as a tool to motivate students to learn how to program whilst creating great games at extra-curricular events such as the three thing game. For us, a tools like XNA is an invaluable intrinsic motivator – inspiring students to want to learn to code, as opposed to being motivated because we said so, or because they will get better grades.

According to the official documentation XNA requires Visual Studio 2010. Now, clearly it’s possible to install both Visual Studio 2010 and Visual Studio 2012 on the same machine, but that would have a big impact on the size of the image. We’d rather not install both if we don’t have to, but if you try to install XNA on a machine that doesn’t include Visual Studio 2010 the installation will fail.

We’re also keen to provide students with as seamless an experience as possible when moving from working at home to working in university – although in the university labs we do insist that students wear pants at all times. It’s with this in mind that I’m writing this blog post, so that students can use XNA at home with Visual Studio 2012 to allow them an easy transition to and from the machines at the University.

After some digging around as well as a decent amount of experimentation we found a solution. Although I’m not 100% clear on how it works my interpretation. Normally when you install XNA there are a bunch of steps to go through. One of the steps copies some files to the Visual Studio directory, which it assumes is Visual Studio 2010. When that step fails the installer rolls back all the other changes like any good installer should. This process extracts each individual step, and when the time comes requires you to manually copy the files to the correct directory.

You’ll need to download this zip file which contains the entire XNA setup and the folders that you’ll need to copy yourself.

  1. Download the zip file and unzip it somewhere. You should see an executable called XNAGS40_setup.exe and a folder called XNA Game Studio 4.0
  2. Open a command line and navigate to the folder that contains XNAGS40_setup.exe – then run XNAGS40_setup.exe /x . You’ll be asked to enter a folder. It’s probably easiest if you create a new empty folder. This folder is temporary and can be deleted after you are done.
  3. Go to the temporary folder and run redists.msi
  4. Run the MSI at %ProgramFiles%\Microsoft XNA\XNA Game Studio\v4.0\Setup\XLiveRedist.msi
  5. Run the MSI at %ProgramFiles%\Microsoft XNA\XNA Game Studio\v4.0\Redist\XNA FX Redist\xnafx40_redist.msi
  6. Run the MSI at %ProgramFiles%\Microsoft XNA\XNA Game Studio\v4.0\Setup\xnaliveproxy.msi
  7. Run the MSI at %ProgramFiles%\Microsoft XNA\XNA Game Studio\v4.0\Setup\xnags_platform_tools.msi
  8. Run the MSI at %ProgramFiles%\Microsoft XNA\XNA Game Studio\v4.0\Setup\xnags_shared.msi
  9. Copy folder XNA Game Studio 4.0 provided in the zip file you downloaded at the start to C:\Program Files (x86)\Microsoft Visual Studio 11.0\Common7\IDE\Extensions\Microsoft
  10. Go to the temporary folder you extracted to in step 2 and run the MSI named arpentry.msi
  11. Open a cmd window and run “C:\Program Files (x86)\Microsoft Visual Studio 11.0\Common7\IDE\devenv.exe” /setup
  12. Delete the temporary folder you created, as well as the zip file and the folder you extracted that to. You don’t need those any more.
  13. Create some awesome games using XNA! 😀

That should have done the trick. Let me know if it works for you :D, or even if it doesn’t 😥

A while ago I read an excellent book called The Pragmatic Programmer. One of the main themes throughout the book was the DRY principle. DRY stands for Don’t Repeat Yourself. I’ll blog about that another time, as talking about it now would be a violation of the DRY principle later. Anway, the authors of the pragmatic programmer suggest that in order to avoid repetition in different forms where possible code should document itself.

Visual Studio offers a tool to help you do this. You can decorate your code with special comments by typing in three forward slashes. One of those tools is the xml comments feature that helps you comment functions in a useful way.

 /// <summary>
 /// A summary of what this method does.
 /// </summary>
 /// <param name="pFantasticString">The significance of the pName parameter that is passed in.</param>
 /// <returns>Information about what this method returns</returns>
 static bool AwesomeMethod(string pFantasticString)
 {
    return true;
 }

This is really handy for two reasons. The first is because it gives you a helpful structure, and a reminder about what sort of information you might want to put in your comments (However sometimes good naming of variables make the comments a little redundant). The really nice thing about documenting your code in this way is that visual studio can use this to populate intellisense, so people using this method don’t even have to find the definition to read these comments. They just pop up as and when they might be required.

Two examples of intellisense using my comment to deliver information when it might be needed

As I said, good method and function names can sometimes render such comments redundant, but sometimes they are invaluable.

Intellisense being really useful

Take this example using the .net Random class. This comment reassures me that if I ask for a random number and pass in 0 and 5, then there is a chance I will get 0, rather than the lowest value possible being 1.

Additionally, by checking a box in a project’s build menu you can get visual studio to create an xml file of your project. This could be used to create documentation for the internet automatically, using tools such as Sandcastle.

Have you ever found yourself in the situation where you know what you want your program to do, but you just don’t know how to express it in code? If so, then read on. If not, read on anyway, as this still may be of use to you!

Code snippets are an excellent feature of visual studio, especially if you go to the effort of learning the shortcuts.

They most important shortcut to remember is Ctrl-K, Ctrl-X (multiple shortcuts used like this is known as a chord). This should bring a code snippets UI. Next I press V for Visual C# and then you can select or type the keyword of a load of constructs and code snippets will deal with the syntax for you. Any variables or names that you might want to define can then be tabbed through.

Hello, Code Snippets!

Here’s an example of a function created using snippets:

public void SortList(List<int> pValues)
{
  bool swaps = false;
  do
  {
    for (int i = 0; i < pValues.Count - 1; i++)
    {
      if (pValues[i] > pValues[i+1])
      {
        int temp = pValues[i];
        pValues[i] = pValues[i + 1];
        pValues[i+1] = pValues[i];
        swaps = true;
      }
    }
  } while (swaps);
}

And here are the parts of that code that were written using code snippets:


  do
  {
    for (int i = 0; i < pValues.Count - 1; i++)
    {
      if (pValues[i] > pValues[i+1])
      {

      }
    }
  } while (swaps);

Code snippets are particularly useful for developing in languages that you are not familiar with. Often you know what you want to do, but you don’t know the correct syntax. In these cases code snippets are invaluable. Here’s the same code, but this time written in Visual Basic, which I hardly ever use:

  Dim swaps As Boolean = False
  Do
    For index = 1 To pValues.Count - 1
      If pValues(index) > pValues(index + 1) Then
        Dim temp = pValues(index)
        pValues(index) = pValues(index + 1)
        pValues(index + 1) = pValues(index)
        swaps = True
      End If
    Next
  Loop While swaps

The logic is the same, but the syntax is different. If you’re ever unsure of the syntax you should be using to get something to work remember Ctrl X, Ctrl K and code snippets will help you out!

You can even define your own snippets, but I’ll save that for some other day.