2D Array and XML

Hi,

I am using a MultiDim array in JS.

I am reading a list of questions and answer from an xml file. I am trying to read them into an array (based on http://www.unifycommunity.com/wiki/index.php?title=JavascriptMultiDimArrays.

My code is:

import System.Xml; 

// npc data 
var npcName : String; 
var npcType : String; 

// chat data 
var maxData : int; 
var showData : int; 
var q: int;
var answers: int;
var questions = MultiDim.StringArray2D(10,6);
var showMessage = false; 
var player : GameObject; 

function OnTriggerStay (collider : Collider) { 
	
     if (collider.gameObject == player) {showMessage = true;} 
} 

function OnTriggerExit (collider : Collider) { 
     if (collider.gameObject == player) {showMessage = false;} 
} 

function OnGUI()
{		
	GUI.Box (Rect (20,140,140,20), "QUIZ");
		// ensures I don't try to show data I don't have 
   	  
      GUI.Label(Rect(20,170,200,20), npcType+":"+npcName); 
      GUI.Label(Rect(20,300,200,100), questions(q,0)); 
      GUI.Label(Rect(20,330,200,100), questions(q,1)); 
      GUI.Label(Rect(20,360,200,100), questions(q,2)); 
      GUI.Label(Rect(20,390,200,100), questions(q,3)); 
      GUI.Label(Rect(20,420,200,100), questions(q,4)); 
      GUI.Label(Rect(20,450,200,100), questions(q,5)); 	
}

function Start() 
{ 
   // initialise data 
   maxData = 0; 
   showData = 0; 
   npcName = "unset"; 

   //readxml from chat.xml in project folder (Same folder where Assets and Library are in the Editor) 
      var reader:XmlReader = XmlReader.Create("questions.xml"); 
     //while there is data read it 
      while(reader.Read()) 
      { 
      //when you find a npc tag do this 
         if(reader.IsStartElement("npc")) 
         { 
         // get attributes from npc tag 
         npcName=reader.GetAttribute("name"); 
         npcType = reader.GetAttribute("npcType"); 
         maxData = parseInt(reader.GetAttribute("entries"));          
         //allocate string pointer array 
         questions = new MultiDim.StringArray2D(6,maxData);          
         //read speach elements (showdata is used instead of having a new int I reset it later) 
         for(showData = 0;showData<maxData;showData++) 
         { 
            reader.Read(); 
            if(reader.IsStartElement("question")) 
            { 
               //fill strings 
               questions(showData,0) = reader.ReadString(); 
            } 
            for(answers = 1; answers<6; answers++)
            {
            	reader.Read();
            	if(reader.IsStartElement("answer")) 
            	{ 
               		//fill strings 
               		questions(showData,answers) = reader.ReadString(); 
            	} 
            }	
         } 
         //reset showData index 
         showData=0; 
         } 
      }       
      q = Random.Range(showData,maxData);    
}

However, I get error on the following lines from above:

GUI.Label(Rect(20,300,200,100), questions(q,0));
GUI.Label(Rect(20,330,200,100), questions(q,1));
GUI.Label(Rect(20,360,200,100), questions(q,2));
GUI.Label(Rect(20,390,200,100), questions(q,3));
GUI.Label(Rect(20,420,200,100), questions(q,4));
GUI.Label(Rect(20,450,200,100), questions(q,5));

The error says:

It is not possible to invoke an expression of type ‘(String, 2)’.

Any ideas why this is happening? All I need is a 2D array to store the questions and answers after reading them from the xml file.

Thanks,

Ads

you need to specify int indices if you want to use multidim arrays, they don’t support string keying.

Sorry for my ignorance, but I do not understand what you mean. I thought I could define a 2D array for holding strings and then access any specific item using
ArrayName(1,1) etc… What to you by int indices…

sorry, error on my end.
For some undefineable reason I thought you had a string in q :frowning: