Detecting Property Changes

My little rant generated some questions that it may be easier to clear up here than 140 characters at a time.

Sometimes you want to execute some functionality when changing the value of a property. The key word there is “changing”. In order to know if the property changed you must compare the new value to the current value. It looks something like this:

// some field in your class
int value;  

void SetValue(int newValue)
{
  if (value != newValue)
  {
    value = newValue;
    // do some stuff that you only want to do if the value changes
  }
}

Works great. I mean, why do extra work if some other code is setting a value to the same value?

But does it work with floating point? I mean, we’re never supposed to do equality comparisons with floating point right? But in this pattern that’s not what we’re really doing is it? We’re just concerned that the value changed. How we arrived at the value being passed in isn’t relevant. This method just cares that the value is different from the previous value.

And this was the source of my frustration in my previous post. I wanted to know that the value had changed, not that it was close. And in my mind at least, using an equality operator is the proper way to do that.

Here is the code behind Unity’s equality operator. Inequality is the same with the opposite sign.

// UnityEngine.Vector3
public static bool operator ==(Vector3 lhs, Vector3 rhs)
{
  return Vector3.SqrMagnitude(lhs - rhs) < 9.99999944E-11f;
}

That works great. It isn’t a bug. It is valuable and even necessary functionality when you’re dealing with floating point and vectors and such. I just believe it doesn’t belong in the operator that’s testing for equality, because close is not the same thing as equals. But maybe that’s just me.

Here is some of the code for the Equals method.

// UnityEngine.Vector3
public override bool Equals(object other)
{
  Vector3 vector = (Vector3)other;
  return this.x.Equals(vector.x) && this.y.Equals(vector.y) && this.z.Equals(vector.z);
}

That is not close, that is equal. And that is what I expect from my equality operators.

This has nothing to do with dealing with floating point rounding errors or the wisdom of comparing calculated floating point values or anything like that. It has to do with expected behavior. But perhaps my expectations are unreasonable and that makes me a clown.

Should Unity change it? Probably not at this point. Who knows what it would break.

And that’s all I have to say about that.

Past Lives

I’ve taken a bit of a programming hiatus so far this summer, with the exception of the WordPress plugin I mentioned last time which I just spent a couple hours a week on over the past several months. Kind of taking a deep breath before plunging into making a game for the upcoming Windows Phone 7. During my break I’ve spent some time thinking about some of the projects I’ve worked on over the years – some that I’ve hated, and some that I’ve loved. One that always stands out for me is a little something called MariusNet, and I thought I’d share the story.

I’ve taken a bit of a programming hiatus so far this summer, with the exception of the WordPress plugin I mentioned last time which I just spent a couple hours a week on over the past several months. I’m kind of taking a deep breath before plunging into making a game for the upcoming Windows Phone 7.  During my break I’ve spent some time thinking about the projects I’ve worked on over the years – some that I’ve hated, and some that I’ve loved. One that always stands out for me is a little something called MariusNet, and I thought I’d share the story.

Back in the days before Bungie was famous for Halo, they made this little game called Myth. I thought it was intriguing, but never played it much. My friend Conner (to use his Myth gamer name) was really into it though, and I followed his exploits from a distance. I did eventually create an account, and since I had recently seen the musical Les Miserables, “Marius” seemed like a good name for a fantasy game.

One of the really cool things about Myth was the central server everyone logged into for chatting and starting games. A large community formed through that, and lifelong friendships were born. Many people formed into gaming clans. Conner was one of the early members of Clan Plaid, which remains an active gaming clan to this day, although they’ve long since branched out to other games. I was recruited later (9/27/2001 to be exact, as I sit here looking at my Marius #CP# beer mug) but was never very active, to my regret.

Sometime after Myth II was released, Bungie started working on some really cool new technology, and a PC game they were going to call Halo. One of the ideas that was talked about early on was the ability to go anywhere on the entire ringed planet. Sometime thereafter, Bungie was purchased by Microsoft, and they moved their offices from Chicago out to the west coast. The “go anywhere” thing obviously never panned out.

Bungie’s move ended up being the driving force behind what ended up becoming MariusNet. It seems that somehow they had lost the source code to their Myth I game server, and being the mad hackers they were, they had hard coded internal network addresses into the server (or something like that), and were unable to get it running again in their new offices. Suddenly several hundred people were no longer able to play the game they loved. My friend Conner approached me and asked if I’d be willing to help him reverse engineer a replacement Myth game server, working on the assumption that the Myth I server functioned very similarly to the still running Myth II server. I eventually agreed.

We found some packet sniffing software, a disassembler, and I abandoned my wife and kids for a couple weeks while we worked. Somewhere along the line we started calling it MariusNet (kind of similar to Bungie.net, get it?) To make a long story short, we eventually (with a couple of key bits of info from one of the Bungie guys) hacked together a more or less stable server that allowed Myth I players to connect, chat, start and play games. Over time we improved the server, re-engineered the login system, added game stats and ranking – the whole works. We even eventually added support for Myth II, Myth III, and even Bungie’s original game Marathon.

Once Bungie realized we had something permanent, they officially discontinued their Myth I server support – which we took as their official blessing that they were fine with our reverse-engineering efforts. They even found time to interview Conner and I. And as thanks for our efforts, we were invited to the original Xbox and Halo launch party in Chicago. We played in the very first public game of Halo, and ate breakfast with the Bungie guys afterwards (yes, it ran that late).

I dropped out of supporting the server quite a few years ago, but some good people took over. I don’t think about MariusNet very often, but every now and then I’ll go back for a visit to see that my namesake is still happily chugging along.  As I write this, there are 31 people playing Myth II, and 12 playing Marathon. “Thriving” may be too strong of a word for the community, especially compared to its glory days, but it’s a good feeling to think that I had some small part in helping people continue playing a game they loved, and that some of those people are still playing even years later.

Anyway, hope you don’t mind my little trip down memory lane. Tomorrow I think I’ll start making some new memories on the Windows Phone 7 simulator.

Visit MariusNet: mariusnet.com

What Next?

So, I worked on Guardian since May, and finally finished it up several weeks ago and it’s up on Xbox Live Indie Games. I won’t be purchasing a fancy car any time soon, but I might be able to use the proceeds for a Sunburn Lighting Engine license in a few weeks. I’ll buy the license anyway, but it’d be cool to do it with money earned from my game.

I spent way too much time and energy finishing Guardian, but I’m pleased to know that I still have it in me after all these years. Still, I want to move back to working on these things as a hobby rather than a money-making enterprise. It was much more enjoyable and relaxing.

So, after a couple weeks off letting my brain recover, I’ve decided to dust off my procedural planet engine and work on it some more. By “dust off”, I mean completely scrap it and start over, using what I learned during the first iteration, and some of the things I’ve learned since while working on other things.

I also plan get back to posting useful things on this blog.

So, I worked on Guardian since May, and finally finished it up several weeks ago and it’s up on Xbox Live Indie Games. I won’t be purchasing a fancy car any time soon, but I might be able to use the proceeds for a Sunburn Lighting Engine license in a few weeks. I’ll buy the license anyway, but it’d be cool to do it with money earned from my game.

I spent way too much time and energy finishing Guardian, but I’m pleased to know that I still have it in me after all these years. Still, I want to move back to working on these things as a hobby rather than a money-making enterprise. It was much more enjoyable and relaxing.

So, after a couple weeks off letting my brain recover, I’ve decided to dust off my procedural planet engine and work on it some more. By “dust off”, I mean completely scrap it and start over, using what I learned during the first iteration, and some of the things I’ve learned since while working on other things.

I also plan get back to posting useful things on this blog.

In the beginning…

I’ve always considered programming an art form. As a result, I try to create code that is clean, readable, and easy to follow – just the way I like my art. I don’t like crappy art, and don’t like crappy code either.

I’ve resisted doing one of these blog things for quite some time now. But lately I’ve felt some motivation to start writing about writing code, and maybe share some of my coding experience.

I got my first computer when I was 12. An Atari 600XL, way back in 1980. I taught myself BASIC, and then 6502 Assembler. I’ve written code in one form or another nearly every day of the 29 years since since then, and enjoyed nearly every minute of it.

I’ve always considered programming an art form. As a result, I try to create code that is clean, readable, and easy to follow – just the way I like my art.  I don’t like crappy art, and don’t like crappy code either.

Of course, now that I’m an old man, I’ve learned that sometimes you have to be practical and just get things done. My code is generally a mix of what I consider some nicely designed parts, and some ugly but functional parts. Although, what comprises good art is pretty subjective, so you as readers may think it’s all ugly crap. Either way, hopefully I can provide some useful information to anyone who is interested.

I’m not 100 percent sure where I want this blog to go. I plan to mostly talk about coding. My whims take me in quite a few directions – sometimes I work on 3D game type things, sometimes websites, sometimes iPhone apps, and any number of other subjects. I’ll probably talk about all of it, and maybe even some non-programming subjects. Generally subjects that are geeky in nature.

Finally, as this thing gets going, bear with the disorganization for awhile, and hopefully at some point geeks of all persuasions will find Crappy Coding an interesting place to spend some time.