Creating a Dictionary of Lists in Javascript

Hey all,

I am trying to use a dictionary, with keys as Strings and values as Generic Lists, but I cannot work out the syntax I need to use.

The class variable is:

private var registeredListeners : Dictionary.<String, List.<BaseEventListener>>;

and the declaration in the class constructor is:

registeredListeners = new Dictionary.<String, List.<BaseEventListener>>();

which Unity really doesn’t like. I’ve tried all sorts of combinations and variations, but just can’t seem to hit on the correct syntax.

Any help would be appreciated,

Wibbs

If you take a look at this thread, near the end: http://forum.unity3d.com/threads/80203-can-someone-explain-to-me-why-this-does-not-work

You’ll see that you can’t do generics like that in Javascript, but if you do it in C# and use your C# code from Javascript, it’ll work.

No, that’s only for Unity 2.6. Generics works as expected (mostly) for JS in Unity 3.

The actual problem in this case is that for some reason whitespace makes a difference; it doesn’t like two >> in a row:

private var registeredListeners : Dictionary.<String, List.<BaseEventListener> >;

–Eric

1 Like

Hi,

I’ve had a look at that post, and understand the approach in principle, but I’m still having a few problems with it.

I now have a class as follows:

using UnityEngine;
using System.Collections.Generic;

public static class GenericList {
	public static List<BaseEventListener> EventListenerList ()
	{
		return new List<BaseEventListener>();
	}
}

However, it does not seem to recognise the BaseEventListener type, which is defined in a seperate JS file.

Also, assuming I can get this approach to work, how would I declare the class variable I want…

private var registeredListeners : Dictionary.<String, what goes here?>

Thanks for your help so far,

Wibbs

Don’t bother with any of that; it’s not going to work here because of compilation order issues and isn’t necessary anyway. See my post above.

–Eric

Thank you very much - there is no way I would have ever figured that out :slight_smile: