x


Compilor Error using Dictonary collection

Hi everyone,

Ive got some code here that utilises a dictionary construct. And in this dictionary construct i have specified a gameobject as the key type and a GENERIC list of type component as the value type. However i seem to be recieving some compile errors. I just want to make sure that i am using the types correctly... I am getting these sort of compile errors;

Insert "semi colon on line 1" (which i already have),and for line 8, i get 3 errors "expecting > found >>" ," expecting > found ("," expecting ( found )"..

Also is looping over a dictionary the same as looping through a hashtable?

Thanks alot in advance

The code i have is here.

    using System.Collections.Generic;


var componentsExtracted:Dictionary;

function Start () 
{
    componentsExtracted = new Dictionary.<GameObject,List.<Component>>();
}


function findComponent(selectedObject:GameObject) 
{
    if(Input.GetKeyUp(KeyCode.E)) {
    //RETRIEVES ALL COMPONENTS IN THE SELECTED OBJECT  
       var componentsFound:Component[] =  selectedObject.GetComponents(typeof(Component));
    //RETRIEVE A RANDOM COMPONENT
       var component:Component = findRandomComponent(componentsFound);
    //ADD COMPONENT TO WITH ORIGIN (GAMEOBJECT) REFERENCE TO DICTIONARY
       addToDictionary();                
    }

}

private function findRandomComponent(componentsFound:Component[]) 
{
// INITIALISE COMPONENT CONTAINER
    var chosenComponent:Component;

    do 
    {
    // RETRIEVES A RANDOM NUMBER
       var ranNum:int = Random.Range(0,componentsFound.length);
    // RETRIEVES A CHOSEN COMPONENT
       chosentComponent = componentsFound[ranNum];
    }

// WHILE COMPONENT IS NOT OF TRANSORM...PARTICLEEMITTER....(ADD MORE)
    while((typeof(chosenComponent)!= Transform) && (typeof(chosenComponent)!= ParticleEmitter));

// RETURN RANDOM COMPONENT
    return chosenComponent;
}

private function addToDictionary() {


}
more ▼

asked Apr 05 '12 at 02:04 AM

naqvir gravatar image

naqvir
89 9 18 19

(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

honestly i cant believe this , but by adding a space to line 8 it removes 3 of the compilor errors.

Before

 componentsExtracted = new Dictionary.<GameObject,List.<Component>>();

Correct verision

 componentsExtracted = new Dictionary.<GameObject,List.<Component> >();

I have no idea why that worked, which is annoying...... :S

I am still getting the import decleration compile error though........... better start checking spaces :/ it expects a semi colon at the end, but i alrady have it :/

using System.Collections.Generic;
more ▼

answered Apr 05 '12 at 02:30 AM

naqvir gravatar image

naqvir
89 9 18 19

"using" is not a JS keyword, it's C#. In JS you use "import". And yes, the JS compiler has difficulty with nested generic syntax, where it wrongly interprets >> as the bitshift operator. Otherwise, spacing doesn't make a difference.

Apr 05 '12 at 02:38 AM Eric5h5

thanks alot Eric! :D

Apr 05 '12 at 02:44 AM naqvir
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x2097
x1951
x296
x72
x54

asked: Apr 05 '12 at 02:04 AM

Seen: 390 times

Last Updated: Apr 05 '12 at 02:44 AM