Search Unity

  1. Unity 2018.3 is now released.
    Dismiss Notice
  2. The Unity Pro & Visual Studio Professional Bundle gives you the tools you need to develop faster & collaborate more efficiently. Learn more.
    Dismiss Notice
  3. Our response to Improbable’s blog post (and why you can keep working on your SpatialOS game). Read more here
    Dismiss Notice
  4. Want to provide direct feedback to the Unity team? Join the Unity Advisory Panel.
    Dismiss Notice
  5. Improve your Unity skills with a certified instructor in a private, interactive classroom. Watch the overview now.
    Dismiss Notice

Creating a Dictionary of Lists in Javascript

Discussion in 'Scripting' started by Wibbs, Mar 2, 2011.

  1. Wibbs

    Wibbs

    Joined:
    Jan 29, 2011
    Posts:
    17
    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:
    Code (csharp):
    1. private var registeredListeners : Dictionary.<String, List.<BaseEventListener>>;
    and the declaration in the class constructor is:
    Code (csharp):
    1. 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
     
  2. Ahonek

    Ahonek

    Joined:
    Jan 12, 2011
    Posts:
    140
  3. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    31,917
    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:

    Code (csharp):
    1. private var registeredListeners : Dictionary.<String, List.<BaseEventListener> >;
    --Eric
     
    barretto likes this.
  4. Wibbs

    Wibbs

    Joined:
    Jan 29, 2011
    Posts:
    17
    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:
    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections.Generic;
    3.  
    4. public static class GenericList {
    5.     public static List<BaseEventListener> EventListenerList ()
    6.     {
    7.         return new List<BaseEventListener>();
    8.     }
    9. }
    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...

    Code (csharp):
    1. private var registeredListeners : Dictionary.<String, what goes here?>
    Thanks for your help so far,

    Wibbs
     
  5. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    31,917
    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
     
  6. Wibbs

    Wibbs

    Joined:
    Jan 29, 2011
    Posts:
    17
    Thank you very much - there is no way I would have ever figured that out :)