Search Unity

Problem with List Generics in Javascript

Discussion in 'Scripting' started by pancake, Sep 16, 2012.

  1. pancake

    pancake

    Joined:
    Sep 16, 2012
    Posts:
    6
    Hello,

    i try to do the following:

    within Edit Mode i add a Script to an Gameobject and initalize the Scripts values. All happens via an EditorWindow-Class.

    One of the values inside TheComponent is a generic list. In this list should be different subclasses of a single parent class. My idea was to create an interface, the parent class implements it, and then creating subclasses of the parent class, which finally would be stored in the list.

    So much about the theory...

    I spend some time testing now this stuff, but i couldn't get it to work the way i want it, so the brief question would be: Is it actually possible to solve the described problem in Unitys Javascript?

    Although i I found little to none documentation about how to use interfaces and generic lists in Javascript i refer to this site http://docs.unity3d.com/Documentation/Manual/MonoUpgradeDetails.html which promises that there seems to be a way to use them.

    Without any further comment (it might be a little messed), here is one of my latest test-setups:

    TheComponent.js:
    Code (csharp):
    1. #pragma strict
    2. #pragma downcast
    3.  
    4. import System.Collections.Generic;
    5.  
    6. // test 1
    7. var words:List.<String> = new List.<String>(); // init in Start()
    8. var words2:List.<String> = new List.<String>(); // init in edit mode
    9.  
    10. // test 2
    11. var books:List.<IBook> = new List.<IBook> (); // init in Start()
    12. var books2:List.<IBook> = new List.<IBook> (); // init in edit mode
    13.  
    14. // test 3
    15. var notebooks:List.<Notebook> = new List.<Notebook>(); //init in Start()
    16. var notebooks2:List.<Notebook> = new List.<Notebook>(); //init in edit mode
    17.  
    18. function Start () {
    19.  
    20.     /// test 1
    21.     Debug.Log("Test 1:");
    22.  
    23.     words.Add("a");
    24.     words.Add("b");
    25.     words.Add("c");
    26.  
    27.     for (word in words) {
    28.         Debug.Log(word);
    29.     }
    30.  
    31.     Debug.Log("words2:List.<String> initalized in Edit Mode:");
    32.  
    33.     for (word in words2)
    34.         Debug.Log(word);
    35.  
    36.     /// test 2 
    37.     Debug.Log("Test 2:");
    38.  
    39.     books.Add(new Notebook("Arschus Aspire"));
    40.     books.Add(new Rulebook("WH40k Core Rules"));
    41.     books.Add(new Handbook("XYZ Handbook"));
    42.     books.Add(new SubNotebook("ABC SubNotebook"));
    43.  
    44.     for (book in books) {
    45.         Debug.Log(book);
    46.         book.Info();
    47.     }
    48.    
    49.     Debug.Log("Book list, initalized in edit mode:");
    50.     Debug.Log(books2.Count);
    51.  
    52.     for (book in books2) {
    53.         Debug.Log(book);
    54.         book.Info();
    55.     }
    56.  
    57.     /// test 3
    58.     Debug.Log("Test 3:");
    59.  
    60.     notebooks.Add(new SubNotebook("XYZ subnotebook"));
    61.  
    62.     for (notebook in notebooks) {
    63.         Debug.Log(notebook);
    64.         notebook.Info();
    65.     }
    66.  
    67.     Debug.Log(notebooks2.Count);
    68.  
    69.     for (notebook in notebooks2) {
    70.         Debug.Log(notebook);
    71.         notebook.Info();
    72.     }  
    73. }
    74.  
    75. interface IBook {
    76.     function Info();
    77. }
    78.  
    79. class Book implements IBook {
    80.     function Info() {
    81.         Debug.Log("Book.Info()");
    82.     }
    83. }
    84.  
    85. class Handbook implements IBook {
    86.     var name:String;
    87.  
    88.     function Handbook(name:String) {
    89.         this.name = name;
    90.     }
    91.  
    92.     function Info() {
    93.         Debug.Log("This is a handbook with the name: " + name);
    94.     }
    95. }
    96.  
    97. class Rulebook implements IBook {
    98.     var name:String;
    99.  
    100.     function Rulebook(name:String) {
    101.         this.name = name;
    102.     }
    103.  
    104.     function Info() {
    105.         Debug.Log("This is a rulebook with the name: " + name);
    106.     }
    107. }
    108.  
    109. class Notebook implements IBook {
    110.     var name:String;
    111.  
    112.     function Notebook(name:String) {
    113.         this.name = name;
    114.     }
    115.  
    116.     function Info() {
    117.         Debug.Log("This is a Notebook with the name: " + name);
    118.     }
    119. }
    120.  
    121. class SubNotebook extends Notebook {
    122.     function SubNotebook(name:String) {
    123.         super(name);
    124.     }
    125.  
    126.     function Info() {
    127.         Debug.Log("This is a SubNotebook with the name: " + name);
    128.     }
    129. }
    BookEditorUI.js (in Editor Folder):
    Code (csharp):
    1. #pragma strict
    2.  
    3. class BookEditorUI extends EditorWindow {  
    4.     private var hs:TheComponent;
    5.  
    6.     @MenuItem ("Window/Test/BookEditorUI")
    7.     static function Init () {    
    8.         EditorWindow.GetWindow(BookEditorUI);      
    9.     }
    10.  
    11.     function TestInit() {
    12.         // test 1
    13.         Debug.Log("Test 1 (Edit Mode):");  
    14.  
    15.         hs.words2.Add("aa");
    16.         hs.words2.Add("bb");
    17.  
    18.         for (word in hs.words2)
    19.             Debug.Log(word);
    20.  
    21.         // test 2
    22.         Debug.Log("Test 2 (Edit Mode):");
    23.  
    24.         hs.books2.Add(new Notebook("Arschus Aspire"));
    25.         hs.books2.Add(new Rulebook("WH40k Core Rules"));
    26.         hs.books2.Add(new Handbook("XYZ Handbook"));
    27.         hs.books2.Add(new SubNotebook("ABC SubNotebook"));
    28.  
    29.         for (book in hs.books2) {
    30.             Debug.Log(book);
    31.             book.Info();
    32.         }
    33.  
    34.         // test 3
    35.         Debug.Log("Test 3 (Edit Mode):");
    36.  
    37.         hs.notebooks2.Add(new SubNotebook("Sub-Edit-Test"));
    38.  
    39.         for (notebook in hs.notebooks2) {
    40.             Debug.Log(notebook);
    41.             notebook.Info();
    42.         }
    43.     }
    44.  
    45.     function OnGUI () {
    46.         var obj = Selection.activeGameObject;
    47.  
    48.         if (GUILayout.Button("Add Component")) {           
    49.             if (obj.GetComponent(TheComponent) == null)
    50.                 hs = obj.AddComponent(TheComponent);               
    51.         }
    52.        
    53.         GUI.enabled = (obj.GetComponent(TheComponent) != null) ? true : false;
    54.         if (GUILayout.Button("Init Component"))
    55.                 TestInit();
    56.  
    57.         GUI.enabled = true;
    58.     }
    59. }
     
  2. pancake

    pancake

    Joined:
    Sep 16, 2012
    Posts:
    6
    I still have no solution, so if anyone can help me with this topic i would be thankful.

    (Today i also try to find a way by mixing in some C#. But the original intent was and still is to stay within javascript)
     
  3. pancake

    pancake

    Joined:
    Sep 16, 2012
    Posts:
    6
    Oh well, C# is far away from Scope, though, i could have accepted it partly. The hope was to use the javascripts generic list filled with C#-objects. But No success as expected.

    The main problem i have is, when i put content into the list in edit mode by using a custom javascript UI (extending EditorWindow) the data will be lost as soon i switch into play mode. :confused:

    In the one way i didn't loose the data, the subclasses added to the list seem to be transformed into the parent class which doesn't help me at all.

    Yet if i do the same stuff with normal data types (no list, no generic), it works.

    Also the javascripts generic list works when initializing it in playmode (e.g. in start()) which is really frustrating because it shows that the list could be used.


    Ok, here is the new idea:
    I think about setting up a gameobject for each list item in edit mode and then in play mode fill the generics list using these go's and delete them afterwards.

    Maybe it makes sense, maybe not. :D:D I just have the feeling i miss some greater thought behind it.

    And still, if anyone can give me advise about what i am doing (wrong) here i would be very thankful.
     
  4. JohnnyA

    JohnnyA

    Joined:
    Apr 9, 2010
    Posts:
    5,041
  5. pancake

    pancake

    Joined:
    Sep 16, 2012
    Posts:
    6
    For me this reads like bad news. But Thanks to you for the reply+new info, JohnnyA!