Again, as I did last year after the Gamefest, I'll be visiting my sister and brother-in-law in Vancouver for a few days after the Summit. So, don't expect technical articles until I return :)
Best,
~Pete
-> Link to Spanish version.
Again, as I did last year after the Gamefest, I'll be visiting my sister and brother-in-law in Vancouver for a few days after the Summit. So, don't expect technical articles until I return :)
Best,
~Pete
-> Link to Spanish version.
If you're new to the world of Community Games on the XBox 360, you'll probably don't know how a/o where to find them.
Kathleen Sanders -XNA Community Manager- a.k.a. "Cookiecups" has published a useful video explaining exactly that.
After watching this video you'll have no excuses for playing'n'buying Community Games.
Enjoy,
~Pete
-> Link to Spanish version.
I have finished with the task of translating a selection of articles from the archive to Spanish.
Thus, from now on, everything goes back to normal here, so you may guess that new articles are coming shortly.
Stay tuned!
~Pete
-> Link to Spanish version.
It's really motivating to know that all my technical contributions to, and support for, the XNA Community is recognized, welcome and appreciated.
Thanks to those of you who nominated me for the award, to those of you who read this blog, to those of you who ask me technical questions via email, to those of you who invite me to participate in XNA-related events, and last but not least to Microsoft for granting me this great honour. [... background music starts to play indicating that my speech must end ...]
This year I'll continue to post articles, submit recommendations for XNA through MS Connect (nope, you're not getting rid of me!), participate in technical/community forums, and help other members of the Community to spread the word that XNA rules!
Cheers!
~Pete
For those of you who usually read this blog thanks a lot for you time, comments and private messages.
As you may know, the results from Ziggyware's lastest article compo have been recently published so congrats to the winners (many thanks to all of you who submitted an article to the competition).
Before I leave, I must tell you: don't foget to check out the games been published on the community games channel on the XBox Live! There are great games being released as we speak which are quite fun to play ... my favorite so far is ... well, I'll tell you after the break.
Happy holidays and cheers!
~Pete
Bill from Blue Rose Games has finally released his game "Dr. Popper" to the Community Games channel over the Xbox Live!
[For those of you who don't know Bill, he usually posts handy articles regarding game programming using both, Silverlight and XNA technologies.]
This is a game I've really enjoyed playing on the PC on the past, and now it's great to see it published for the XBox 360.
The proposal is simple in design and still incredibly fun to play. So give it a try, relax, and enjoy the smooth puzzle-like gameplay.
I'l leave you with a short video of the game:
One of the best parts is that it only costs 200 points ... a nice Christmas present, indeed.
Cheers!What is more, you should also know that thanks to the integration of XNA GS with Visual Studio, in order to add a new component you just:
This is great!
Now, and again: if you've been using the XNA Framework you should know that there is a specification of the game component class for those that will be drawed to the screen: "DrawableGameComponents".
Unfortunately, if you want to create one of those components there's no shortcut, meaning either you create it by scratch or use a game component file as your source file and then modify it accordingly. In short, there is no DGC template integrated into VS, yet.
To change this, however, you could just create your own DrawableGameComponent template by going in VS to "File -> Export Tamplate", and then choosing the item you want export as a template.
After following the whole exporting process you can get and also integrate your new template to the list of items that you can pick when using VS "Add New Item" feature.
The problem with this solution is that your new template will be listed by default under the "My Templates" section of the "Add" dialog control, completely unrelated to the "XNA Game Studio 3.0" items section. Maybe you're fine with this solution generated "by default", but if you don't, then you have to tweak a little your recently generated template, change its location and one more thing that I will explain in what follows.
First, I have uploaded a class template for the drawable-game component here:
http://www.megaupload.com/es/?d=74ITZDJX
This template contains all the proper files that will helps us to better integrate it into VS.
Thus, follow whis steps:
Usually, the "ItemTemplates" folder is located in "%vs90comntools%\Common7\IDE", so say if you're using VS08 Standard, Pro or Team edition, you'd probaly find something like "C:\Program Files\Microsoft Visual Studio 9.0\Common7\IDE\ItemTemplates".
We are almost done, now. There is just one more little procedure we must follow before reaching our goal:
In case you're using the express version you have to browse for the folder in the start menu for that edition and, instead of typing "devenv" as stated in (3.) above you must type "VCSExpress".
If everyhing goes ok, everytime you want to add a new item template you should see something like:
Or better yet:
By the way, you can use your own icon design if you don't like the one I provided in within the zip. To replace it, just delete the "old" one and include your own in the zip file, respecting the old name (that is, the name requested by the template XML file).Well, that's pretty much it. I hope you find this useful. And yes, you can repeat this same procedure for every item template you want to include under the "XNA Game Studio 3.0" items section.
Cheers!
~Pete
To all XNA'ers out there: only 5 days remain to submit your article(s) to Ziggyware's Fall '08 contest.
If you're still in the process of making up your mind whether to enter the compo or not, this may help: you could win an XBox 360 ELITE! And that's only the beginning.
Want to know more? Read about the prizes on this post.
Just, hurry up! The clock is ticking ...
Happy writing!
~Pete
Uruguay Gamer has published the links to the videos recorded during the closing ceremony of the third annual domestic constest of videogame development held in Uruguay.
On the site you will also find the comments of Chaim Gingold for each videogame.
Note: The presenter in both videos is Gonzalo Frasca, CEO of Powerful Robot Games (one of the most-known uruguayan videogame companies) and main organizer of the compo.
Here's video 1 (the one receiving the first prize is me):
And here's video 2 (here you can see Frank Baxter -ambassador of US in Uruguay- congratulating the grand winner and at the end of the video you will find Chain Gimgold himself):
Enjoy!
Pete
My PC game "The Riegel Battle" (created with XNA GS) has been today awarded first prize in the category "Best 3D Videogame Promise" during the third annual domestic videogame contest in Uruguay.
Even though it wasn't awarded first prize for the whole compo, as a local supporter of XNA, it was a great honor for me to be among the winners.
Along with the organizers, the contest was sponsored by the US Embassy in Uruguay, so ambassador F. Baxter was present today in the closing ceremony to congratulate the winners.
Chaim Gingold (one of the designers of the game Spore) was one of the judges, and it was also present today in the above-mentioned ceremony, not only as a judge but to speak about the design concepts behind Spore's character editor (a great talk, btw).
Here's a video of my entry:
As soon as I get the links to the official pictures of today's ceremony I'll publish them here.
Cheers!
Pete
With this third article, I'm closing my introductory series to the world of Extension Methods and XNA.
At first, this article was meant to focus on how to extend the Random class to get new colors, but Ziggy, in his article entitled "Particles Using LINQ" -worth reading, btw- has clearly shown how to achieve this so I will take that great article as a starting point to go a bit deeper in the discussion on the matter.
If you see Ziggy's method named "RandColor" -which from now on I will refer to it as "NextColor" to be in line with the Random class, he's showing one of the new features of the XNA Framework 3: when using floats there's no need to first create a Vector3 in order to then create a Color, anymore.
In previous versions of XNA, to use floats in order to create a color, you would have declared:
public static Color NextColor(this Random rand)
{return new Color(new Vector3(rand.NextFloat(), rand.NextFloat(), rand.NextFloat()));
}
But from the XNA Framework 3 and on, this also works:
public static Color NextColor(this Random rand)
{return new Color(rand.NextFloat(), rand.NextFloat(), rand.NextFloat());
}
Now, if you see the Particle2D class in the above-mentioned article, you will notice the field named "Color", which is declared as an variable in uppercase; a new extension method could be created for it, as follows:
public static void NextColor(this Random rand, out Color color)
{ color = new Color(rand.NextFloat(), rand.NextFloat(), rand.NextFloat());}
Thus, we can use it within the Update method in the referred example to replace this line:
p.Color = rand.NextColor();
By this new line:
rand.NextColor(out p.Color);Since the type Color is a struct, the advantage of using the latter over the former could be generally deemed as marginal, but eventually you may get to the situation when even the slightest improvement in your code is desired, specially when you're targeting the compact framework.
New question: what if you followed the design guidelines of C#, declaring the color field in lowercase as a private field and instead, a public getter and setter was included in that class, in the form of a public property named "Color".
In that case, the new overloaded method cannot be used since Properties and Indexers cannot be passed as out/ref parameters. Why? Well, just remember that when you're using properties and indexers you're not directly dealing with variables but methods. In that particular case, the first implementation -that is, the one included in Ziggy's article- is the best solution.
Let's face it! You could be tempted to pass the existing instance of Particle2D as "ref" to sort out the above-mentioned constraint, as follows:
public static void NextColor(this Random rand, ref Particle2D particle)
{ particle.Color = new Color(rand.NextFloat(), rand.NextFloat(), rand.NextFloat());}
It should compile ok BUT ONLY if you're NOT PASSING to that method the temporary variable created by a foreach loop -as opposed to the base example. This operation is just NOT ALLOWED for the iteration variables of foreach loops. My advice here is simple: avoid this tempting implementation and as I said before just use the one presented by Ziggy. Generally, the simplest solution is the correct one.
One side note: in case you're new to C#, it's important to notice that in this case we are using "ref" instead of "out", since we need an existing (that is, a non-null) instance of the class in order to assign the new color. Otherwise, by using "out" we should have to create an instance of type Particle2D from within the extension method BEFORE assigning the new color to it.
Now, let's move onto an interesting situation. What if:
If that is the case, then you should take a look to this implementation:
public interface IColor
{ Color Color { get; set; }}
public static class HelperMethods
{...
public static void NextColor<T>(this Random rand, ref T instance)
where T : class, IColor
{ instance.Color = new Color(rand.NextFloat(), rand.NextFloat(), rand.NextFloat());}
}
As you can see, we are hereby combining the power of Generics with Extension Methods.
But, why the "IColor" constraint? What we are saying here is simple: only types that implement the Color property can call this extension method. And those non-null instances passed to the method as a parameter (remember we are using the "ref" word here) will be handled as IColor types.
Ok, but why the additional "class" constraint? Tricky one. As a corollary of the previous constraint, if we do not specify that only classes are accepted as parameters, then structs can be also passed when calling the method, and for that they will have to implement the IColor interface ... Meaning? ... Tic tac tic tac ... Yeap, boxing. Ugh! That nasty word. Handling structs through interfaces causes boxing. Chilly, isn't it? Now you know it, in case you didn't.
To sum up, as you can see Extension Methods are quite handy, but you have to use them with care if you want to avoid getting into a design trap. As usual, not all the situations can be solved the same way, even though at first it sounds logic to you. You may find your-self saying "Whaaat? Buy whyyy? ...". So, I hope these examples show you what to use and when and what to avoid and why.
Some final thoughts about Extension Methods:
In short, if you use Extension Methods with care and wisdom, they can be a true friend at the time of coding your XNA-based game or application: they simply help you extend any types with new methods AS IF these where originally part of those types, what in turn can be handy, for instance:
So go ahead, try Extension Methods and share what you find with the XNA Community. It would be great and fun to know what you are using them for!
Well, I hope you have found this series useful. Constructive comments and suggestions are always welcome.
Cheers!
~Pete
Continuing with the topic of how to use the handy functionality provided by "Extension Methods" with our XNA-based creations, for this article we are going to need the "FuelCell" tutorial (remember that you shall find this tuto -along with the "TopDownShooter" one- in the help file under the "What's New In This Release" section).
Ok. In the chapter called "Finishing Touches" of the FuelCell' s tutorial, you'll find the following method at the very end of the "FuelCellGame.cs" file:
If so, you guessed it right! We can use Extension Methods to declare an operation called, say "GetFullArea", and give it the proper implementation.
Not only can we pass again a parameter by reference (using the "out" reserved word) -like we did in Part 1 of this series- but also in this particular case, since we're dealing with a struct, we can afford to implement an overloaded method that locally declares, creates and returns an instance of a Rectangle (I know what you're thinking, but unfortunately, there are no "Extension Properties" in C#, so we need to get the rectangle from a method call).
Using Extension Methods, our solution should look like the following:
Now, the GetTitleSafeArea(...) method could be re-implemented like this:
Or also like this:
Remember two important things:
This technique can be used to get, say -among other things, a Vector2 containing the width and height of the Viewport (as well as for the display and the client area). I leave it as an exercise for you to try (you'll see it's actually quite easy to implement).
I hope you find this article useful.
'till next time,
~Pete
With the release of XNA GS 3, two new tutorials were included into the "What's New In This Release" section in the help file: "TopDownShooter" and "FuelCell" games. For what follows, we'll be referring to the first tutorial game: TopDownShooter.
Sometimes we need to get an instance of a sound effect without playing it. The good news is that it can be done. The bad news is that there's no built-in method provided yet to get that instance in one step.
So, what's next then? Open the AudioManager.cs file provided in the TopDownShooter example. You should find the following code on the LoadContent() method:
As you can see, the above-mentioned code gives an idea of what you should use to get the instance of a sound effect "without playing it".
But, what if we want to get that functionality in one method (as if it were provided built-in) without extending the SoundEffect class? Well, in that case since we are targeting the .NET Framework 3.5 we can use the magic of "Extension Methods".
I'm not going to explain what Extension Methods are (to get a first read on the subject, please refer to this page), but how we can take advantage of them for this particular case.
Our goal is to extend the SoundEffect class in a way that:
Thus, our solution should be the following:
Therefore, our final implementation should be something like this:
As you can see, I'm naming the static method "GetCue" but you can call it, say, "GetInstance" if you prefer. Also, since we cannot yet use optional parameters -we'll have to wait for C# 4.0 for that- you will have to create as many overloaded methods as you need.
Now, in the LoadContent() method of the AudioManager.cs file, all we should use instead is:
Don't forget to add the proper "Using" statement on the AudioManager.cs, linking to the namespace where the extension method was declared and implemented (in my example "MyNamespace.Audio"), or your code won't compile.
Also, you will need to manually add a reference in your project to the "System.Core" assembly -in case it is not already referenced.

Well, this is it. Now you know how to extend the SoundEffect class to get a new instance of a sound effect in just one method call, virtually speaking ;)
One final note: by using "Extension Methods" we are not breaking any design principles, since we have no direct access to private members of the "extended" type (being the latter, in this example, the SoundEffect class).
Enjoy!
~Pete
The winners of this year's Dream.Build.Play competition have been announced today.
In order:
Congrats to the winning teams!!!
If you have been using XNA GS for some time now, then you will know by heart the benefits of the content pipeline. If not, just to mention a few, here is a brief list for you to check before reading on this post:
Ok, let us go on, now.
I. THE BASICS
On the process of creating your game, having built all your assets at compile time with GS you will have to use a Content Manager to load them at runtime. Fortunately, as you all may know by now, XNA already provides one for you. What is more, the game project template defines a reference and creates and instance of it on the game class.
This CM will take care of both, the loading and unloading process of pre-built assets. This means that behind the scenes, not only will it handle the instantiation of the assets being loaded but also, when properly requested, it will dispose for you the ones that can be disposed.
For loading, you will have to use a sentence like "this.Content.Load<...>(...);" or "this.Game.Content.Load<...>(...);" and the usual place to put them is inside the overriden LoadContent method of your game class and or drawable game components.
And for unloading, all you have to do is call your CM's Unload() method. Again, being a usual place to include this sentence inside the overriden UnloadContent method of your game class and or drawable game component.
II. BEHIND THE SCENES
Now, If you have been reading carefully you will notice that you can load assets individually but there is no way to unload them individually. At first you may think that this is an unpleasant restriction, but as you may conclude after reading this post -or at least, as I expect you will, this is not. In fact, it helps you manage your assets on a very tidy manner.
Generally, for the sake of performance, efficient memory allocation and best user experience, you design your game so that all the needed assets are loaded at "one time", say, at the begining of each level. So, even if you load the assets individually, that behavior from the perspective of "one-shared time bucket" -to call it some way- shows that you programmed the logic of your game to treat all these loaded assets as a cluster.
The above-mentioned rationale can be then extended to the process of unloading assets. There is no need to assume a priori that each asset will be unloaded individually at different times during gameplay. And therefore, they will be treated as a cluster, in this case during the respective level, and unloaded altogether, again, in "one-shared time bucket", when requested.
You may wonder: "Ok, but what if I want to dispose certain assets during runtime and, at the same time, to keep others in memory?". The answer to that is quite simple actually: just create more than one content manager.
We will get back to this topic in a moment, but first let us consider another important aspect of the Content Manager.
III. THINGS NOT TO DO
Avoid to manually dispose your assets!!! The CM does not like that you handle the destruction of assets by using sentences like "this.myAsset.Dispose();". In fact, the CM does NOT monitor if you do, and thus, it could and will get confused when that happens.
Let us see an example. Say you have a Screen1 and Screen2 classes and:
If Screen1 manually disposes the texture (either using the Dispose Pattern or by calling Dispose within the UnloadContent method) without calling "this.Game.Content.Unload();" first, when Screen2 tries to draw the texture on screen you will get an exception stating that the texture has being disposed, even if Screen2 loaded that texture. As I told you before, the CM gets confused with situations like this.
Therefore, avoid these implementations:
... and ...
Being the proper way to unload assets:
Please notice that this is also allowed by the CM:
Comply with the above, and you will do good.
IV. THINGS TO DO
Getting back to the question of how to unload assets at different times at runtime, there is one practice that will help us understand why it is sound and thus, advisable, to have more than one CM in our XNA-based games.
When you create a program you declare global variables and local variables. This common practice incorporated to our daily programming tasks is the key that will then leads us to think of global and local assets.
You may consider an asset "global" if its lifetime lasts across all the game, or at least, most of it. In this sense, assets should be deem as "local" instead if it is expected and intended to unload them once a stage in the game has finished but the game has not and will not, at least for a while more.
Thus, when creating your game, use:
1) A Content Manager for "global" assets: the one instantiated by default within the Game class (which you can access using the Content property), and
2) One (or more) Content Manager(s) for "local" assets: say one content manager per level, per screen, or per whatever category that fits your game's design.
Again, group your assets in clusters based on lifetime, and you will know which one is global and which ones are locals. If all your screens will use one spritefont in common, then there is no reason to load and unload it for every screen; just load it once and hold it in the collection of global assets. If a ship model will be used in only one level, handle it locally to that level.
As you can see, using more than one Content Manager is a sound practice: easier to apply, easier to debug, easier to mantain and easier to extend.
Sometimes there is no need to have more than one CM for all the game, depending on the game of course, but still it is a good practice to know and get accustomed to, since in the end it helps us manage our assets at runtime in a more tidy and convenient manner.
So, my recommendation is: get used to it. The sooner, the better.
V. CONCLUSION
The Content Pipeline is a great tool to import assets to our XNA-based games which is complemented with the proper manager to handle that content at runtime: the ContentManager class.
By using instances of this class wise and properly, we could get (more) efficience in the fields of implementation, debugging, mantainability and extensibility.
In order to achieve this goal certain practices must be applied:
I hope you find this post useful. Comments and suggestions are welcome.
Cheers!