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.

The Parse Unity library is okay, not as fully fleshed as some of the other languages they support but good enough for what I needed apart from one critical point, the lack of type-safety in the queries such as:

new ParseQuery<Armor>().WhereLessThanOrEqualTo("cost", 13);

This annoyed me so much that I decided to write some extension methods that take advantage of Lambda functions to provide the type-safety. The above now becomes:

new ParseQuery<Armor>().WhereLessThanOrEqualTo(a => a.Cost, 13);

The library can also handle chains such as

new ParseQuery<Player>().Include(p => p.Stats.Heath.Remaining); // becomes "stats.health.remaining"

The library also handles interfaces by introducing a new attribute “ParseFieldType”.

[ParseClassName("Father")]
public class Father : ParseObject, IFather
{
	[ParseFieldName("daughter")]
	[ParseFieldType(typeof(Child))]
	public IChild Daughter { get; set; }
}

new ParseQuery<Father>().Include(f => f.Daughter.Name); // becomes "daughter.name" and works because ParseFieldType redirects the chain to Child rather than IChild

It can even handle lists

[ParseClassName("Father")]
public class Father : ParseObject, IFather
{
	[ParseFieldName("children")]
	[ParseFieldType(typeof(Child))]
	public List<IChild> Children { get; set; }
}

new ParseQuery<Father>().Include(f => f.Children[0].Name); // becomes "children.name"

To get started go grab the .dll from my Github project for the library: https://github.com/mikecann/Unity-Parse-Helpers

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.

You can download the extension here: https://chrome.google.com/webstore/detail/post-to-tumblr/dbpicbbcpanckagpdjflgojlknomoiah?hl=en

Over the years it has gone through many different versions and rewrites, checkout the following posts for more info: