Introducing U2DEX: Unity 2D Editor Extensions

Been a while, eh?  With Xbox Live Indie Games slowly being pulled off life-support, it was time I moved on to something else — turns out that something else is the Unity game engine.

Which brings me to the topic of the day: U2DEX — my first foray into making plugins for Unity.

From it’s asset store page:

U2DEX streamlines areas of the Unity Editor for 2D development, further turbocharging your 2D workflow.

By stripping away 3D values, or presenting them in a way that makes sense for 2D space, U2DEX rounds out and polishes some of the Unity Editor’s sharp, 3D corners, while still maintaining the Unity look-and-feel you know and love.

Essentially, U2DEX makes creating in Unity even more awesome for 2D games.  Here’s a run down of some features:

  • Grid tool – Aligning objects has never been easier!
  • Snap tool – Snap objects to numerical units, or to a Grid component’s Grid if you have one enabled!
  • Applicable class editor – Apply the 2D Transform Inspector to virtually any class with a name!
  • 2D Toolkit support!
  • Orthello support!
  • Enhanced Size and Scale – Use the underlying object’s scale, if possible. Preserve your dynamic batching with no extra work!
  • Enhanced Rotation – Use Degrees, or Radians, and watch as U2DEX converts between the two in real time!
  • Compatible with Unity Free and Pro
  • FULL source code access!

There’s more in-depth info on it’s product page.
In addition, you can now talk about stuff on our forum!

By the way, U2DEX is only $10, which makes it super cheap.
You can buy it here!  (I’ll love you forever!)

That’s it for today.  Hopefully some people get some use out of U2DEX — I had fun making it, so hopefully you have fun using it!

Space Crüesader Patch Is Out!

Space Crüesader version 1.0.1.0 is now out on the Xbox Live Marketplace!

There’s ton of fixes, but the #1 most important thing is that backgrounds can now be disabled entirely, if the player wants it.  Following closely at #2 is that nobody is shunned from the highscore boards if they play with backgrounds or flashes off.  Instead, they get an icon next to their entry for each thing (flashes, backgrounds) that they have disabled.

Another huge plus is that, although the Marketplace name hasn’t been changed, Bing can now find the game!  Woo-hoo!  Turns out that Bing does, in fact, look at the game descriptions, so all I had to do was remove the umlaut from the description.  Now you can find the game by typing in “Space Cr” into the Xbox’s Bing search, and it’ll give you “Space Cruesader”.  Follow that link and it’ll take you right to it!

Have fun!

If you’re interested, you can read the full, behind-the-scenes changelog below.
You might learn something!

Continue reading

Space Crüesader Patch Incoming

A patch for Space Crüesader has been in the works for the past week and a half or so that should address a lot of issues (and non-issues that’ll make the game more user-friendly).  Here’s a short list of some of the changes you can expect:

- A new option to disable backgrounds completely!
- Two more options under “Flashes” that will allow you to lessen the flashes and fireworks,
without completely removing them.
- The ability to always upload scores, even if you have flashes off and backgrounds off.
(New icons will be placed next to entries, so players can tell if that entry was made with
those options off)
- The highscore table will now update every minute if you’re viewing scores.  This should
help if you’re hanging out waiting for a score sync to happen.

This patch should be coming soon, and I’ll post the full change log when it hits the Marketplace.  I’m currently hung up on two things, but once those are cleared up (either for better or for worse) the patch should be out pronto!  Here’s the things I’m currently working on getting sorted:

#1 I’m trying to get the umlaut removed from the ‘ü’ in the Marketplace title so dimwitted Bing can find it (seriously, who would have thought that Bing couldn’t connect the dots? — no pun intended).

#2 I’ve been very diligent about trying to make all old saves compatible with the new format.

At the end of the day, this patch should address a lot of the issues I’ve seen reported.  I know that this is a bit light on the info, but once I’ve got some more info to share, I’ll do so.

Space Crüesader Reviewed by Indie Gamer Chick, Recieves Seal of Approval

On November 30th, a review for Space Crüesader was posted on Indie Gamer Chick.  Generally, this is where my fellow indie developers prepare for the no-doubt brutal shredding of their baby.

I was no exception.

When I emailed Indie Gamer Chick to let her know that my game was out, I was expecting the inevitable review to be pretty brutal.  Twin-stick shooters are a pretty over-crowded market on XBLIG, and while I was confident that mine was a good game, Indie Gamer Chick has gone on record stating that she’s generally not a fan of shooters.  “Better cue up my bitter tears,” I was thinking as I sent the email.

Surprisingly, it was a tear-free experience.  A few days after I sent the email, Indie Gamer Chick had the review up, and it was uncharacteristically positive.   In fact, Space Crüesader is now proud to bear the Indie Gamer Chick Seal of Approval, which can be seen on its product page.  She even went so far as to say it’s one of the best titles on XBLIG, and that she loved it!

If you don’t believe her (shame on you!), you can form your own opinion for the low, low price of one dollar.

If you do believe her (good on you!), you can purchase the game from Xbox.com, or find it on the Xbox Dashboard under “Indie” (near the top of the new releases list, as of the time of writing).

If you’re undecided on the matter, then I can’t help you.  I could add a gratuitous link to the game’s marketplace page, but that would be rather silly, wouldn’t it?

Space Crüesader Launches, Fanfare Begins

Let it be known that on the 28th of November, 2012, Space Crüesader launched for all the world to play! (cue fanfare)  You can purchase it on Xbox.com for 80MSP (or $1, in normal monetary terms).

There’s also some pretty sweet reviews coming in as well, like this one and this one.

There’s still a long way to go, and the website will be going through some changes as I work out the best way to showcase the game and news relating to it.  However, the game is out, the reviews thus far have been positive, and I actually made it to release.

That’s got to be worth something, right?   Right?

1 When I said “for all the world to play” I meant the United States, Canada, the United Kingdom, Japan, Germany, France, Italy, Spain, Sweden and Singapore.  I would have said “most of the world”, but that doesn’t sound as nice, does it?

The .Net Dictionary Dilemma

Nine times out of ten, if you were to ask someone “What is the best way to store a key and a value in .Net?” that someone would answer something along the lines “Well, you’d probably want to use a Dictionary.”.

Now, if you were to ask that same question but phrase it slightly differently to “What is the best way to store a key and a value in .Net on the Compact Framework?” you probably wouldn’t get “Dictionary” in your answer.

You see, it is a common (misguided, I should add) notion that Dictionaries are, usually for garbage reasons, completely evil, and you would have to be monumentally stupid to even contemplate using them. Even worse, if you even vaguely approached the idea of using enums as your key value, most (misguided) programmers would have you tarred and feathered…and then marked with the red cross, as you have obviously contracted the plague of incompetence.

So, I went ahead and built an input manager revolving around Dictionaries, with enums as my keys.

You see, I don’t believe that Dictionaries are the absolute devil-spawn of .Net Collections. Sure, there is a grain of truth in the concept of “Dictionaries create garbage, and Dictionaries create lots of garbage when used with enums”, but there is a fairly simple way around that. But first, a quick explanation of why using enums with Dictionaries create garbage.

Dictionary<enum, value> = Garbage

The above statement, while true, doesn’t tell the whole story. Using an enum as your key value (that is, the first argument, as Dictionaries look like Dictionary<Key, value>) will more-than-likely cause Boxing to occur. If you are unfamiliar with what boxing is, the general rule of thumb is this:

Boxing is bad. Avoid it.

The reasoning behind this rather broad statement is that Boxing/Unboxing creates garbage. This is exactly why using enums as your key value creates garbage. To avoid this, you have two options:

1. Cast your enum values to ints before inserting them into your Dictionary. Then use integer keys for accessing the values later.

This isn’t a great option. It makes your code (in my opinion) ugly, and it will make your life a nightmare later when you have to do this for any Dictionary you use. It will also ruin the type safety that generics have in .Net.

2. Don’t use enums as keys, or don’t use a Dictionary.

Bit of a letdown, wasn’t it? This is the definition of “stuck between a rock and a hard place”.

However, there is a magical third option that most everyone glosses over with the “Dictionaries and enums are evil” argument.

The Third Option

Your third option is to utilize an IEqualityComparer.

“Well, how do I do that!?” I hear you screaming at your screen. Don’t worry; I’ll explain how these work.

First, let’s create a normal Dictionary:

Dictionary<Buttons, bool> GamePadButtons = new Dictionary<Buttons, bool>(); // create our
//dictionary for storing Buttons (which are the buttons of an Xbox GamePad)

Great! We’ve got a Dictionary set up to use Buttons as our key, with a bool being used as our value.

Now, if we iterate over this Dictionary, or check one of the values like this:

foreach (Buttons button in GamePadButtons.Keys)
{
        if (GamePadButtons[button] == true)
        {
                 //do something
        }
}

Ah…

There’s the garbage I was on about earlier. Interestingly enough, this code does not generate garbage on a PC, using .Net 4.0 or later. However, if you are using an earlier version, or targeting the .Net Compact Framework (which is what the Xbox uses), you will still get garbage.

And lots of it.

So, then, let’s fix it. This will require that you make a new class (one of the aforementioned IEqualityComparers). Personally, I would make a new .cs file and store all of my IEqualityComparers in that single .cs file. If we follow the sage advice of Nick Gravelyn, we should end up with something like this:

public class GamePadButtonsEqualityComparer : IEqualityComparer
    {
        private static GamePadButtonsEqualityComparer defaultInstance;
        public static GamePadButtonsEqualityComparer Default
        {
            get
            {
                if (defaultInstance == null)
                    defaultInstance = new GamePadButtonsEqualityComparer();
                return defaultInstance;
            }
        }

        public bool Equals(Buttons x, Buttons y)
        {
            return x == y;
        }

        public int GetHashCode(Buttons obj)
        {
            return obj.GetHashCode();
        }
    }

We’ve now created an IEqualityComparer for the Buttons class! Now, all it’ll take to erase the garbage created by our Dictionary is to change this one line of code:

Dictionary<Buttons, bool> GamePadButtons = new Dictionary<Buttons, bool>();// our old line

Dictionary<Buttons, bool> GamePadButtons = new Dictionary<Buttons, bool>(GamePadButtonsEqualityComparer.Default); // our new line

We’ve fixed it! That single line of code has solved all of our Dictionary-induced headach–

Wait. That hasn’t done anything at all! Where have we gone wrong?

It turns out that we need to change one more line of code to completely erradicate the garbage. This time, the line of code is located in our IEqualityComparer:

return obj.GetHashCode();// our old line

return (int)obj;// our new line

There we go. It turns out that GetHashCode does generate some garbage here, so insead of using that method (which returns an int), we just manually cast the object to an int ourselves.

Problem solved! All of the garbage generated by our Dictionary+enum pair has been resolved.

Now for the bad news. You’ll have to make an IEqualityComparer for every enum you want to use. (For example, if you wanted to use the Keys enum for Keyboard keys, you’ll need to make an IEqualityComparer for that.) However, the code is exactly the same as the one we’ve made here, only you’ll use Keys instead of Buttons. So, yes, this would qualify as boilerplate code, but it’s much easier (and less ugly) than having to cast to ints everywhere!

Conclusion

Dictionaries are great for storing keys and values. By having eliminated one of the drawbacks (if you can call it that) of using them, we’ve now made it easier to correctly disprove the argument “Dictionaries with enums are completely and utterly evil, and only the incompetent use them”. Albeit, it’s not the most user-friendly way of creating Dictionaries — having to create an IEqualityComparer for every unique enum you want to use is not fun — but it’s much better (in my opinion) than casting to ints, or worse, not using Dictionaries. (and maybe using a double-dimension array, or two separate arrays/lists, etc. instead)

MediaPlayer: How to Check if it Has Control

I’ve been working with XNA’s MediaPlayer class a lot lately, and I’ve come to learn many of the typical pitfalls associated with it.  (Hint: There’s approximately a million of them)

One of the biggest is the fact that it’s so slow.  I mean, it will literally slow your game to a crawl if you check Volume more than a few times every few seconds.  However, I’m getting off-track here.  Maybe I’ll go into that in some other post.

For now, I’m going to post about something much more important.  You see, XNA has this nifty feature that will mute either your Music category in XACT, or the MediaPlayer class, when the user starts to play music through the Guide.  This is fantastic, and I’ve never had a problem with it (I’m an XACT man, myself), until I started using MediaPlayer for my Custom Playlists (so the user can play their own music in-game).

MediaPlayer pitfall #1,000,001: While MediaPlayer will mute your music when music is played through the Guide, it will not resume your music if that music from the Guide is turned off!  Like a spoiled, bratty kid, it will do half of what it’s told, but then firmly dig in and say “Screw you!”.

So, then, how do we go about fixing this behavior?

I got to thinking, and realized that polling the MediaPlayer.GameHasControl will probably be slow, so checking it every frame was going to be a no-no.  So, then I came up with this:

// Declarations
bool GuideWasVisible = false; // a bool we'll use to keep track of the last Guide status

bool MediaPlayerHasControl = true; // a bool we'll use to cache the
                                                    //MediaPlayer.GameHasControl value

// The following should probably be wrapped in a class.
if(Guide.IsVisible)
{
   if(!GuideWasVisible)// since we'll set it anyway,
                                //we can use it to stop subsequent updates
      {
        if(MediaPlayer.GameHasControl) // check if the MediaPlayer has control
         {
            MediaPlayerHasControl = true;
         }
         else
         {
            MediaPlayerHasControl = false;
         }
         GuideWasVisible = true;// set GuideWasVisible, since the Guide is Open
      }
   }

if(GuideWasVisible && !Guide.IsVisible)
{
   bool MediaPlayerRegainedControl = false; // now we check if it gained control back
   if(MediaPlayer.GameHasControl) // check if the MediaPlayer has control
         {
            MediaPlayerRegainedControl = true;
         }
         else
         {
            MediaPlayerRegainedControl = false;
         }
   if(!MediaPlayerHasControl && MediaPlayerRegainedControl)
     {
        StartNewSong();// the MediaPlayer did not have control before, but now it does,
                               // so you can start a new song.
     }
   GuideWasVisible = false;// reset this, so it only does the MediaPlayer check once.
}

The code itself is pretty self-explanatory (but not especially clean, it can probably be improved), but I’ll run through the gist of it.

Essentially, MediaPlayer is slow, so we can’t check the GameHasControl flag every update. However, we can check it once, if the Guide gets opened, and check again when it’s closed. This code hinges on the fact that a user must open the Guide to turn on their custom playlists.

Then, when it’s first opened, we cache the current value of the MediaPlayer.GameHasControl. Then, when the Guide is closed, we check the current value of the MediaPlayer.GameHasControl, and compare it to the value when it was first opened. If the previous value was that it did NOT have control, and the current value is that it DOES have control, we start a new song, since the user stopped their Guide music.

Now, I will admit, this isn’t without it’s problems. First, if the user pauses the music from the Guide, the GameHasControl flag will still be false. Don’t ask me why, I have no clue why it still does this.

Label that MediaPlayer pitfall #1,000,002.

Also, you will more-than-likely need to adjust the code to fit your needs. For instance, I pause my Jukebox class when it’s detected that the game doesn’t have control. I also set a flag when it has regained control to play a new song when the game is unpaused (as I pause when the Guide is opened).

At the end of the day, it’s a workaround for a problem that shouldn’t exist in the first place. It works — but it’s not ideal.

Announcing Space Crüesader!

My entry for Microsoft’s annual Dream.Build.Play contest is finally ready!

Space Crüesader is a twin-stick space shooter, and, incidentally, my very first game.  It’s been a labor of love for some time now, and I’m excited to finally be able to show it off.  It’s 99% completed, I just have a few more finishing touches, but overall it’s in the state it will ship in later this year.

If you’re interested, you can read more about the game and see its trailer at the game’s product page, located over here.

The Birth of a New Blog

Hello, world!

This will be my new blog for (primarily) game development and programming in general.

There’s not much for me to say at this point, so I’ll leave this short and sweet, but there will be more blog posts with more substantial content soon.