CS Games Competition: Game Development (Part 1)

So one of the competitions I worked on for the CS Games this year was Game Development. For this, we provided a 2d platformer based on a sample XNA project provided by Microsoft. The final product provided to students allowed them to do the following:

  • build levels without performing any coding
  • extend the framework by making additional AIs, items, tiles, anything they wanted to customize their game
  • choose from a large variety of sprites/sound effects/visual effects to give the game a more unique feel.

Something to keep in mind with all the submissions, competitors only had 3 hours to produce something, given the time limit, I’m really pleased with what came out of it at the end and I think everyone had a really good time.

Check out the following video to see some of the cool stuff people came up with.

C# Events and Garbage Collection

A friend asked me earlier about garbage collection in C#.  What happens to an object with Events when we set it to null, will the references in the event prevent it from being GCed?

Lets say we’ve got 2 classes, Listener and Generator.  Generator has an event called SayMyName and each Listener registers to that event when it gets created.

Notice in the Listener class we dont actually store the Generator in a class level variable (or property). What links these two together is stored in the Generator reference to the Listener is added to the Generator

public class Listener
{
	static List instances = new List();

	public Listener(Generator generator)
	{
		instances.Add(new WeakReference(this));
		generator.SayMyName += new Generator.SayMyNameHandler(Generator_MyEvent);
	}

	void Generator_MyEvent(string name)
	{
		Console.WriteLine(name);
	}

	public static void TestReferences()
	{
		int cnt = 0;
		foreach (WeakReference instance in instances)
		{
			Console.WriteLine("Instance " + (cnt++) + " is " + (instance.IsAlive ? " alive " : " gone"));
		}
	}
}

Now for the Generator class, we’re going to do something a bit special. We’re going to create a static List of WeakReferences to keep track of the instances and whether they’ve actually been disposed of by the Garbage Collector. WeakReferences are ignored when counting references on an object during garbage collection.

public class Generator
{
	static List instances = new List();

	private string _name = null;
	public string Name {
		get
		{
			return _name;
		}
		set
		{
			_name = value;
			OnSayMyName();
		}
	}

	public event SayMyNameHandler SayMyName;
	public delegate void SayMyNameHandler(string name);

	public Generator()
	{
		instances.Add(new WeakReference(this));
	}

	protected void OnSayMyName()
	{
		if (SayMyName != null)
			SayMyName(Name);
	}

	public static void TestReferences()
	{
		int cnt = 0;
		foreach (WeakReference instance in instances)
		{
			Console.WriteLine("Instance " + (cnt++) + " is " + (instance.IsAlive ? " alive " : " gone"));
		}
	}
}

Now lets try actually making use of these, then try to remove our Generator.

Generator generator = new Generator();
Listener listener = new Listener(generator);

Console.WriteLine("Generator References:");
Generator.TestReferences();
Console.WriteLine("Listener References:");
Listener.TestReferences();

generator = null;
GC.WaitForFullGCComplete();

//No Generator instances will remain, Listener has no reference to Generator
Console.WriteLine("Generator References:");
Generator.TestReferences();
Console.WriteLine("Listener References:");
Listener.TestReferences();

Alternatively, the same cannot be said if we want to get rid of a Listener so easily.

Generator generator = new Generator();
Listener listener = new Listener(generator);

Console.WriteLine("Generator References:");
Generator.TestReferences();
Console.WriteLine("Listener References:");
Listener.TestReferences();

listener = null;
GC.WaitForFullGCComplete();

//Listener instance remains because the reference is held by the Event.
Console.WriteLine("Generator References:");
Generator.TestReferences();
Console.WriteLine("Listener References:");
Listener.TestReferences();

Coding Night of Horrors

On Saturday night, 11 brave souls sat down for an all night event of coding, pizza, and caffeine. Needless to say horrors, wonders, and uncertainties were produced. Some worked on their individual projects, others worked on a joint effort of producing something from scratch.

I introduce to you the SCS World Simulator. A joint project started at 8pm Saturday evening and finished at 7am Sunday morning. Given a small framework to get the project started, a goal, and a lot of caffeine these people were tasked with building a world, characters, AIs (Artificial Intelligences) to control them, a combat system, a method of communication…. and then let the creations run wild and see what happens.

Our conclusion? IT’S ALIVE!!!! People created custom looking characters with their own intelligence (after a fashion). We had ninjas, pirates, pikachus, normal people, bombs, querie-some bears, hobos and dancing Darth Vaderites. Each did their own thing, some interacting verbally, others interacting with a sword, and others that just sat and danced.

Scared to see what comes of this? next week at engineering week we’ll be setup and showing off our little project. We’ll let it run for some hours to see what these wee devils come up with on their own. A big thank you and congratulations to those who participated. To those who missed it? Don’t worry, we’ll do it again!

Return top