Sunday, November 15, 2009
Non-Trivial Custom ContentProcessor - Part 1
Besides the tutorial and samples I mentioned in the introduction MSDN also has a very nice tutorial on how to create a complete content processor.
A complete content pipeline extension project consists of the following parts:
- A content importer
- A content processor(s)
- A content data class(es)
- A content writer
- A content reader
Each piece is a distinct step in the process of importing and loading content in your project.
The content importer is a very simple object. All it has to do is open the basic content (image, model, game data etc.) and parse it enough that it's usable by the content processor. Since I originally chose to use XML my importer only needs to open the file as an XML document. If you're using XML you might want to test the document against a DTD to save in sanity checking code in the processor.
Here's the importer:
using System;
using System.Xml;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content.Pipeline;
using Microsoft.Xna.Framework.Content.Pipeline.Graphics;
// Entity group type specs/annotations are in XML format
using TImport = System.Xml.XmlDocument;
namespace EntityGroupProcessor
{
///
/// Basic entity group type importer. Entity group types are always in XML. All this does
/// is load the file.
///
[ContentImporter(".ent", DisplayName = "Entity Group Importer", DefaultProcessor = "EntityGroupTypeProcessor")]
public class EntityGroupTypeImporter : ContentImporter
{
public override TImport Import(string filename, ContentImporterContext context)
{
XmlDocument document = new XmlDocument();
document.Load(filename);
return document;
}
}
}
Notice the line
[ContentImporter(".ent", DisplayName = "Entity Group Importer", DefaultProcessor = "EntityGroupTypeProcessor")]The first part ".ent" specifies the extension of the file that this importer processes. This allows Visual Studio to automatically use this importer when content with the extension of ".ent" is added to the project. The second part specifies the name of the importer as it appears in Visual Studio. The third, and most important part, names the class used to process the output (in this case an XmlDocument instance) in the next step.
The bulk of the code, at least in my case, ends up in the content processor and the content data classes. Stay tuned for Part 2 where I get into the basics of the ContentProcessor and content data classes.
Neat effect
I'm likely to not use 3D textures like in these blogs so that I can save on resources. Instead I hope to derive the thickness of a particular section of a beam from the alpha value of that section of beam.
Stay tuned for followups, and Part 1 of my custom content processor series.
Saturday, November 14, 2009
Non-Trivial Custom ContentProcessor - Intro
This past week has been interesting so far since I found that my initial solution for batching objects for drawing was impractical when it came to the critical stage of actually doing the batching. So I had to tear it out and start over. The new solution is quite clean and has an added advantage of reducing the amount of data stored for render objects. Only one set of render data is stored no matter how many objects that use it exist. But this change started a cascade of extra changes which has led me to begin developing a custom content processor for XNA.
I'm working on generalizing the code needed to spawn in-game objects. My original method of loading in-game object data was to load a model and create one in-game object for each mesh that makes the model. The properties of the in-game objects were encoded in the names of the meshes. Initially this worked surprisingly well. The biggest advantage is that I didn't have to spend time on tool development and could focus on getting the game working.
There are, however, four problems.
- No two objects defined in a model can have the same properties (due to mesh names having to be unique)
- There's no easy way of creating a deep tree of attached objects (e.g. a capital ship with turrets)
- Using names severely limits the number of properties that can be defines
- Not all objects need to be models (e.g. beams and bullets)
My solution is to split off loading of graphics and defining in-game objects. Graphics are loaded and tossed into a pool for later use. Each graphical item has a unique name which can be used to reference it. Once the graphics are loaded the game then has to load information on how to create in-game objects using the graphics.
This is where the ContentProcessor comes in. My current plan is to create a custom XML document which defines a tree of in-game objects and their properties. Each file lists a set of in-game objects to be created. Each object is a single block in the XML file defining the following.
- What appearance the object has (references a loaded bit of graphics)
- What the object's type is
- What the object's reference name is
- What the object's parent's reference name is
- A set of properties specific to the object's type
In order to keep the code for the game clean this XML data is to be loaded using a custom ContentProcessor. There are at least some resources out there on the internet including this (tutorial 21, PDF Alert!) and this which provide some nice sample code. I like the first since it distills the process down to its simplest form. Useful for learning, but it doesn't cover all the nuances I need for what I'm doing.
Stay tuned as I post more details on the content processor.
Monday, November 9, 2009
What to do... what to do... what not to do
But I digress... one part of Scrum that we do use is the product backlog. It's essentially a glorified to-do list. A huge set of items to be created for the project with estimates for how long they'll take. Work from the list is batched together in units called sprints. The expectation that over the length of the sprint all of the selected tasks are to be done. In the real world I believe sprints are one or two weeks long, in school they're about a month. Anyway, if the to-do list for the sprint isn't empty when the time is up then there's a problem. Fortunately, due to the way Scrum works this problem is noticed as the sprint ends which is usually well before the project is to be delivered. This is the good news.
The bad news is that, in my case, I have around 40 hours of work left to accomplish in one week. We're expected to put in about 12 hours a week individually. As I hinted before, that's a problem. At this point in the project I can defer some of the work to later sprints if need be. But first, prioritization.
The critical items (not started):
- X-Box 360 support
- Beam weapon rendering
- Beam weapon test
- Capital ship gun turrets (guns which can independently shoot targets)
- Targeting cursor/crosshairs
The incomplete items:
- The targeting computer
- Weapon status display is working but ugly
- Smoke/flames/tracers (barely started)
- Sorting/grouping of graphics by how they're to be drawn
- The network code (still untested)
This leaves:
- The network code
- X-Box 360 support
This cuts the amount of work left from ~40 hours to 10.5 hours or so. Much better.
Sunday, November 8, 2009
I can haz multiplayer test???
This has turned out to be painful in one place which I didn't expect, the game's rules system. When I first created the game I've been working with a basic startup sequence which creates a bunch of fighters and assigns a local controller (i.e. your X-Box 360 gamepad) to one of them leaving the rest uncontrolled. I originally wanted to create a fairly simple stub and hook joining players to other objects arbitrarily. This didn't work.
The problem is that when a player joins they initially know nothing of the game's state. This includes which fighter they're supposed to control. If I just assigned one arbitrarily on both sides the results would be inconsistent. Each player would think that the other is attached to a different object. Worse, two players could be trying to control the same object. This meant I had to implement code which runs on the game's host only to assign a player to a specific in-game object. In addition it had to support the player being killed and assigned a new object of (possibly) a different type than before.
What occurs now is this:
- The host starts up a game session and starts playing.
- Some other player joins the game session
- The host notices the joining player and asks the rules system that a new player is in the game. This is where team balancing etc. would occur.
- In the mean time the joining player is watching the game without participating.
- The host notices that there's a player without a ship (i.e. the joining player.) The host finds an appropriate place to create the player and adds them to the game. In addition the host lets all players know that one of the players has just been assigned a new ship. This is where re-spawn rules would go.
- The joining player notices that they've been assigned a ship. Since it doesn't exist on their machine yet they create it and attach their local controller to it. The player is now in the game.
- If there were other players they'd also notice a player has been assigned a ship. Since it also doesn't exist for them they'd create it as well.
I've omitted some housekeeping but this is, at least for now, how players joining and being assigned ships is handled. What's left is testing. Since I only have one PC on hand I'll need to do that at school.
Monday, November 2, 2009
Oink
Instead, last week, I was down with a nice little case of the swine flu. Not fun... but not the worst disease I've had by any strech however. The only thing that really sucked was the night I didn't get to sleep (and fitfully at that) until about 2:00AM because of an irritating, persistant cough. That and the fact I had not one, but two exams right in the middle of the week I was sick. Oh yeah, that fever was fun too...
To anyone reading... try to not get the swine flu, it's not the worst flu, but it still sucks.
I'll be posting about my adventures in getting communication between players working later this week.