Showing posts with label programming. Show all posts
Showing posts with label programming. Show all posts

Tuesday, January 8, 2013

The Math Behind Vision Cones in Unity 3D



Lately, I've been tinkering with with an early Unity 3D prototype of a top-down action game and I figured I should implement vision cones to add a stealth element to dealing with the enemies. My initial solution was to stick a cube collider in front of enemies to represent their vision.

Sunday, June 24, 2012

C# Delegates, Destructors, and Garbage Collection

Just this past week, I've encountered a very specific memory leak in Unity 3D that could happen just generally coding in C# as well. Basically the leak involves subscribing to a delegate in a class' Constructor and then unsubscribing to that delegate in the class's Deconstructor. Here's the gist of what I was trying to do.



I thought this class -- which did not derive from MonoBehaviour -- would act like MonoBehaviours and call its destructor when a scene was changed. That was not the case at all. When I ran the program, the SNController objects would stick around and the loggedIn and loginFailed functions would be called multiple times as I exited and entered scenes.

It turns out that -- because C# is a managed language -- destructors for objects are called when the objects are cleaned up by the Garbage Collector(GC).

So why didn't the GC destroy the SNController object? The problem was that the GC saw that this object was still subscribed to delegates from the FacebookManager class and would not destroy the object. Therefore, the Destructor would never be called, creating a Catch 22 situation.

I needed to unsubscribe from these delegates explicitly when I was done with the SNController object. To do that, I wrote a Dispose method.



Since SNController was instantiated as a member of a MonoBehaviour object, I would call SNController's Dispose method in the MonoBehaviour's OnDestroy method. This eliminated any extraneous references from the SNController object at the end of the scene and GC would destroy the object.

Monday, February 8, 2010

Transitioning to Box2D 2.1: b2Fixtures

My game development team got a bit of a shot a few weeks ago when found that our ActionScript 3.0 physics engine, Box2D had released the Alpha version for their 2.1 update called 2.1a. Strangely, the C++ version of Box2D has yet to release this update.

I actually stumbled into downloading the new version while looking for a way to create multi-shape bodies. I figured the examples that come with the Box2D source would show me how. Boy was I in for a surprise: what were all these b2Fixtures doing in the code?

Monday, January 18, 2010

Getting started with Git Part 2: Branching and Tracking

Part 1 of Getting started with Git can be found here.

Now that we've pulled and pushed code with a remote repository it's time get more advanced. One of the more elegant features of Git or any source control is the ability to branch code and work without fear of breaking the original source. With Git's local and remote repositories, the options become even more robust.

Saturday, January 9, 2010

Getting started with Git

One of the toughest things to learn during this game project was actually our source control Git. Phil(our most experienced member) insisted we use Git because of its superiority to SVN and the like. Git allows you to create both a remote and local repository of your code. This means you can branch and merge code all you like on your local repository and only interact with the remote repository when you need to.

I've used SVN before so I didn't think much about learning Git. Ironically, after a week of trying to share code between the four programmers in our group, Phil was probably the least Git literate in our group. Git's cryptic interface can be pretty unforgiving, especially if you don't know Git's recommended workflow.

So I decided to write a guide to getting started with Git on Windows.

Monday, December 14, 2009

Listing Authors in a WordpressMU page

I'm trying to give my staff more of a presence on GamesPlusBlog. They need to feel ownership of their piece of the blog and their content within.

One way of doing this is recognizing when authors join the site. For awhile now, our About Us page only displayed the three founders of the blog - Tim, Mike, and me. Ever since we began bringing on new writers, Mike has suggested we list them on this page. Pragmatic programmer that I am, however, I didn't want to have a list to curate every time an Author joined the site.

The obvious solution is have PHP and Wordpress generate a list of blog authors and display them onto the About Us page.