Simple Deserialization with XML and XNA Content Pipeline

The XNA Content Pipeline can at first seem daunting but is extremely easy to use once you get the hang of it. I wanted to share a small class that I created to represent an enemy entity that has its information stored in an XML format. The content pipeline reads my XML animated sprite data and imports/processes it to a .xnb file at build time. At run time, this .xnb file is then deserialized into an object that I can use to draw to the screen.

My XML content looks like this:

The C# class associated with this data at run time looks like this:

The XNA Content Pipeline has a built in importer and processor for XML –> Object deserialization. As long as you have the “Type” attribute of the “Asset” element set to the correct namespace and class, the Content Pipeline will take care of the rest. Just load this content like so:

Reference: http://msdn.microsoft.com/en-us/library/ff604979Read more

Quirks with XNA ContentManager Load and Unload

I was recently experimenting with game state transitions and attempting to unload content when a screen was no longer needed. For example, I had a main menu screen which loaded assets to its own ContentManager on activation and unloaded the content when the screen transitioned off based on user input. This all worked fine until the main menu was asked to transition back on after the user was done playing the game.

What I found is that calling the ContentManager’s Unload method will call the Dispose method of all the resources for which it is responsible. You do not and should not manually call the Dispose method for content that has been loaded. However, a problem arises when you want to reuse something that has been unloaded without reinitializing the ContentManager. Even worse, the problem arises even if you call the Load method for a piece of content after it has been Unloaded.

For example:

I received an error on line 7 of the above paste because even though I reloaded the texture, the ContentManager will return the cached version of the already loaded texture (even though you called Unload!). Yes, the cached version is the one that got disposed!… Read more

Build XNA Game Installer: Visual Studio Setup Project

This guide assumes:

  • Visual Studio 2010 Professional
  • XNA Game Studio 4.0
  • Users will be Windows

This is the second article about building an XNA game installer when you are ready to distribute your game to the masses. In the first part, I described how to use the convenient ClickOnce option to build a relatively simple and non-customizable deployment package. If the ClickOnce option is too restrictive for you or simply does not meet your needs then you should refer to this article about using a Setup Project to create a more customizable installation package.

As with most things in life, the Setup Project is not a perfect solution to packaging up and distributing your game. What I find extremely annoying and shortsighted on Microsoft’s part is that the Setup Project will not automatically detect your game content like ClickOnce does. I want to be clear that, in this context, “content” is referring to your compiled content by the Content Pipeline (textures, sounds, other files) and not the “content” of your project as a whole. I will explain how to force the Setup Project to use your game content. Be warned: it is tedious and annoying, but it works..

  1. Open your game solution
  2. Right click solution -> Add -> New Project…
  3. Other Project Types -> Setup and Deployment -> Visual Studio Installer -> Setup Project
  4. Right click the setup project -> Properties
  5. Change the “Configuration” drop down to whatever configuration you want the setup to use (debug/release/etc)
  6. Click “prerequisites”
  7. Make sure these are checked:
    • Microsoft .NET Framework 4 Client Profile (x86 and x64)
    • Microsoft XNA Framework Redistributable 4.0
    • Windows Installer 3.1
  8. Right click the setup project -> View -> File System
  9. In the “Application Folder”, right click -> Add -> Project Output
  10. Change the “Project” drop down to the main game project
  11. Click “Primary” output in the list box
  12. Choose the configuration for which you want to include the output (debug/release/etc)
  13. Repeat steps 9-12 for any other projects that you need to include in the output (custom libraries)
    • NOTE: Do note include output from content pipeline extension projects because the content pipeline it is unavailable at run time

You just created a setup project, told it which prerequisites are needed for your game to work, and finally told it which build output you want to include in the installer. You can arrange the “File System” folders to how you see fit for your application folder hierarchy. For example, you should see options to include files in the User’s Desktop or the User’s Programs Menu in case you want to include shortcuts. Be careful not to abuse this though, because the user will not be happy if you dump 500 files on their desktop!

While that was relatively painless, all is not well in setup project land. For some reason there is no way to have the setup project automatically find your compiled content in .xnb format after the content pipeline has imported and processed your content files. You can include the “Project Output” of the content project, but that will not do anything useful, so do not bother!… Read more

Build XNA Game Installer: ClickOnce

This guide assumes:

  • Visual Studio 2010 Professional
  • XNA Game Studio 4.0
  • Users will be Windows

This article is the first of two on how to package your XNA game into an installer for Windows environments. The first is the easier but less customizable option of using Visual Studio’s built in ClickOnce installer. ClickOnce is essentially a hand-holding tool to package up prerequisites, dependencies, manifests, and content. Additionally, it allows you to specify locations from which the users will install the package (CD, Web, Shared Drive). More conveniently, ClickOnce allows you to specify where the application should check for updates in order to automatically update the content when new versions of the application are released. For example, you could specify a URL that the application will use to check for new versions. If a new version is detected, you can specify either the same URL or a different URL to use for downloading the new content.

While ClickOnce is easy to use, it is not perfect for every scenario and can fail quite spectacularly. I have personally run into situations where users could not install updates to the application because of cryptic errors that were only resolved by completely recreating a user account in Windows (I kid you not, this is a Microsoft-sanctioned approach). Many of the errors are difficult to diagnose and the documentation does not help much when it comes to the more advanced features of ClickOnce. It is definitely convenient, but do not use it if you want customization or extendability.

That said, here is how you use ClickOnce to package your XNA game into an installer.

  1. Open your solution in Visual Studio
  2. Right click your main project -> Properties…
  3. Click Publish tab
    • Publishing Folder: local or remote path that will be the destination of the publish (example: C:DevGamesGame1Installer)
    • Installation Folder URL: optional unless you want the users to install from a location different from the publish location
    • Install Mode: Pick offline availability (users should not have to be connected to the internet to play video games!)
    • Click Application Files…
      • Make sure the Publish Status of your game files is set to Include (Auto)
      • Make sure the Publish Status of the XNA libraries is set to Prerequisite (Auto)
    • Click Prerequisites…
      • Microsoft .NET Framework 4 Client Profile
      • Microsoft XNA Framework Redistributable 4.0
      • Windows Installer 3.1
    • Click Updates…
      • Check for updates if you want your application to check for updates through ClickOnce
      • A lot of the options are self explanatory on this screen
    • Click Options…
      • Description tab
        • Publisher name: your company name
        • Suite name: optional used to define a super application that manages this one
        • Product name: the name of your game
      • Deployment tab
        • If you want to deploy to a web page, specify the URL here
        • Check the other options here if need them
      • Manifests
        • Click “Create desktop shortcut” if you want a shortcut
        • Ignore the other options unless you really need them
  4. Specify the version of this publish (you can have it auto increment if you want)
  5. Click either the Publish wizard (which just confirms your already entered information) or Publish Now to begin publish

Your published files will be organized like this:

  • Application Files
    • Contains every version that has been published (in case user needs to rollback to a previous version)
    • Contains “Content” folder which includes files from your Content project in your XNA solution
    • .dll libraries that you created
    • Game icon
    • Game thumbnail
    • ClickOnce application file used to check for updates
    • Game executable
    • Executable manifest
  • ClickOnce application file used to check for updates
  • Setup.exe used to install the game

Note that the Application Files directory will contain a history of every version of your application.… Read more

Creating a Media Server using PS3 Media Server and a PS3

Hardware needed:

  • PC
  • PS3
  • TV
  • Switch
  • Receiver (optional)

Software needed:

Ideally your setup will look something like: PC (media server) –> Switch –> PS3 –> Receiver (optional) –> TV. The advantages of including a receiver between the PS3 and the TV include better speakers and the ability to decode some of the newer proprietary audio codecs such as DTS-HD and TrueHD (more on that in a different post.)

Follow these steps:

  1. Use the link above to obtain and install PS3 Media Server on the PC you’re using as your media server.
  2. Turn on your PS3.
  3. Start up PS3 Media Server and wait for it to scan the network for available renderers. When it’s done, you should see your PS3 in the “Detected media renderers” section. You can confirm connections or view any errors in the “Traces” tab.
  4. Under the “Navigation/Share Settings” tab, add any folders you want to share with your media renderers under the “Shared folders” section.
  5. On the PS3 menu, go to the appropriate section that you want to stream under (Pictures, Music, or Video), find the PS3 Media Server, and navigate to the content you want to stream.
  6. When you start streaming, PS3 Media Server will display information about what it is currently streaming, the buffer status, and the bitrate under the “Status” tab.

Quite honestly, this is all that needed to be done to get content streaming properly to my TV. In a separate post, I’ll talk about some of the more advanced options of PS3 Media Server, transcoding, video/audio codecs, subtitles, ripping Blu-Rays/DVDs, and lessons learned for the best streaming performance.… Read more