Javascript var TYPEing questions

Ok, so we need to use strict type-ing in Unity for iPhone, which is fine, and I am slowly cleaning up some of my not so strict code, but I have a few questions…

(1) ArrayLists? Are they ok?

I am tracking ArrayLists of wayPoints that are just colections of Vector3 positions, but I am NOT using the syntax:-

var wayPoints : Vector3[];

But instead I am using:-

var wayPoints = new Array();

Which according to UnityiPhone, is not ok… but how else can I declare an ArrayList called wayPoints that has a bunch of Vector3’s in it, without using the : Vector[ ] syntax, as that is not what I am after.

By using the var wayPoints : Vector3[ ]; stuff, I then cant use … wayPoints.Add, wayPoints.removeAt, waypoints.insert ect… which I need.

(2) Component Referencing…

I had code like…

target.gameObject.GetComponent("EnemyMoveLogic").KillMe();

which I have had to convert to something like…

var EML : EnemyMoveLogic = target.gameObject.GetComponent("EnemyMoveLogic");
EML.KillMe();

Any reason for this? It’s like I cannot reference a component on a gameObject unless I make a typed reference to it first. Thats a lot of extra code everywhere… I wonder if it is much of a performance hit…

Another problem example is…

static var	s3Levels = new ArrayList();

class LevelDetails 
{ 
   	public var lID : int;
	public var lFilename : String;
	public var lVersion : String;
	public var lName : String; 
   	public var lOwner : String;
	public var lSerial : String;
	public var lUploaded : int;
 	public var lZoom : int;
	public var lFav : int;
	public var lWaypoints : String;
	public var lMapElements : String;
}

This in Unity 2.1, allows me to make an arraylist of LevelDetails… eg:-

var tmp : LevelDetails = new LevelDetails();
tmp.lVersion = "1.0.1";
etc...
s3Levels.Add( tmp);

print(s3Level[0].lVersion);

But in UnityiPhone, I get the error that lVersion is not a member of Object (from the print statement line), because it doesn’t recognise WHAT object type s3Levels is because it was not declared as any type.

Anyone help shed some light as to how this should be structured now?

It’s a performance gain, not a hit. That’s the whole reason for this in the first place, given the iPhone’s relatively slow CPU. You can get the same results by using “#pragma strict” in your scripts with regular Unity.

–Eric

thank you so much! you solved my problem.

var xy : myscript = myobj.GetComponent("myscript");
xy.DoStuff();

Instead of this i was trying lots and lots of stupid things… Damn, it’s so simple when you think about it…

Maybe I can help you with something in return. The standard array works for me:

var arr = new Array (Vector3.up, Vector3.zero, new Vector3(0.3, 0.2, 0.1));

function Start () {
    arr.Add(Vector3.zero);
    print(arr.ToString());
}

Can you be a little bit more specific about what you’re doing with arrays?

I have solved it all…

Basically, I cant reference my levels array like…

s3levels[num].lVersion = 5;

what I need to do is cast a variable as a LevelDetails of THAT array element, then access the members via the variable… eg…

var lvl : LevelDetails = s3levels[num];
lvl.lVersion = 5;

So its all sorted… just… um, a lot for me to change… but 3 hours later, its all changed and working fine :slight_smile:

great to hear!

I still have those 3 hours ahead of me, but at least now I know what to do :slight_smile: