May 2014 Posts

Fixing Unity’s Internal Compiler Error

Fixing Unity’s Internal Compiler Error

As mentioned in my last post, I am working on a Unity game that takes advantage of Parse for Asyncronous multiplayer. Well one nice feature of parse is that it uses Tasks to handle its asynchronicity.

Tasks are very much like JS promises (except they are type-safe) and return when the operation has completed. For example:

obj.SaveAsync().ContinueWith(task =>
{
    if (task.IsCanceled)
    {
        // the save was cancelled.
    }
    else if (task.IsFaulted)
    {
        AggregateException exception = task.Exception;
    }
    else
    {
        // the object was saved successfully.
    }
});

You can chain tasks together like so:

var query = new ParseQuery<ParseObject>("Student")
    .OrderByDescending("gpa");
 
query.FindAsync().ContinueWith(t =>
{
    var students = t.Result;
    IEnumerator<ParseObject> enumerator = students.GetEnumerator();
    enumerator.MoveNext();
    var student = enumerator.Current;
    student["valedictorian"] = true;
    return student.SaveAsync();
}).Unwrap().ContinueWith(t =>
{
    return query.FindAsync();
}).Unwrap().ContinueWith(t =>
{
    var students = t.Result;
    IEnumerator<ParseObject> enumerator = students.GetEnumerator();
    enumerator.MoveNext();
    enumerator.MoveNext();
    var student = enumerator.Current;
    student["salutatorian"] = true;
    return student.SaveAsync();
}).Unwrap().ContinueWith(t =>
{
    // Everything is done!
});

The problem is that if you want to catch errors you must manually check the task return for errors inside each handler. This seemed wasteful to me as all I wanted was a global error handler for that particular task chain, such as can be achieved with Javascript Promises.

Fortunately someone else also had noticed this problem and solved it:

Task.Factory.StartNew(StartBusyIndicator)
	.Then(task => GetWebResponseAsync(url))
	.Then(task => Console.WriteLine(task.Result.Headers))
	.Finally(ExceptionHandler, StopBusyIndicator);

The only problem is that when I tried to implement his C# library Unity started throwing the dreaded Internal Compiler Error:

Internal compiler error. See the console log for more information. output was:
Unhandled Exception: System.ArgumentNullException: Argument cannot be null.

It took me a while to work out what was going on. I managed to simplify the entire problem down to this simple example:

public static class TaskHelpers
{
	public static void Then<TIn>(this Task<TIn> task, Action<Task<TIn>> next)
	{
		task.ContinueWith(t =>
		{		
		});    
	}
}

I posted about this on the Unity forum on Parse’s Forum and even on Parse’s bug tracking system but no one was interested, Parse even told me its not a Parse issue.

It took me quite a while to work out what was going on, but I eventually worked out that if I separated the handler from the Continue With call it would work:

public static class TaskHelpers
{
	public static void Continue<TIn>(this Task<TIn> task, Action<Task<TIn>> next)
	{
		Action<Task> a = t => { };
		task.ContinueWith(a);    
	}
}

Huzzah! It compiles. So I guess this is a lesson learnt. With the Unity Mono compiler, if you are getting Internal Compiler errors then perhaps try separating out the lambdas into variables.

Parse.com Type-Safe Extensions for Unity

Parse.com Type-Safe Extensions for Unity

As mentioned in my previous post I have been working on a multiplayer game built in Unity for a little while now. I ummed and ahhed over the technology choice for the backend for a little while before deciding to go with Parse.com‘s library. The reason being that it looked simple to implement (they have a Unity SDK), they take care of all the backend headaches for me and the pricing model looked fair.

Post To Tumblr

Post To Tumblr

Post To Tumblr is an extension for the Chrome Browser. It started off as a simple one-evening project to solve an annoyance with Tumblr, before long however it had thousands of users. At the time of writing it currently has 32,000+ users.