Converting Javascript arrays to List

I’m having some trouble converting an old script that heavily uses Javascript arrays to List’s, so they can work on Unity Flash. Please, take a look at this script:

 #pragma strict
var upgrade : Upgrade;
static var WeaponArray : WeaponInfo[];
private var Upgrades : Upgrade[];
private var applied : boolean = false;
var classesAllowed : boolean[];

function Start () {
	if(WeaponArray == null)
		WeaponArray = FindObjectsOfType(WeaponInfo) as WeaponInfo[];
}

function Apply () {
	applied = true;
	var temp : Transform;
	var up : Upgrade;
	var upgradeArray = new Array();
	for(var i : int = 0; i < WeaponArray.length; i++){
		var enumIndex : int = WeaponArray[i].weaponClass;
		
		if(classesAllowed[enumIndex]){
			temp = Instantiate(upgrade.gameObject, transform.position, transform.rotation).transform;
			temp.parent = WeaponArray[i].transform;
			temp.name = upgrade.upgradeName;
			up = temp.GetComponent(Upgrade);
			up.Init();
			up.ApplyUpgrade();
			up.showInStore = false;
			upgradeArray.Push(up);
		}
	}
	Upgrades = upgradeArray.ToBuiltin(Upgrade) as Upgrade[];
	
	this.SendMessage("Apply");
}

function UnApply () {
	applied = false;
	this.SendMessage("Remove");
	for(var i : int = 0; i < Upgrades.length; i++){
		Upgrades[i].DeleteUpgrade();
	}
}

Taking WeaponArray as an example:

  • How can i declare a new list? Like this? (static var WeaponArray : List. = new List.():wink:
  • How can i discover the size of the list? (so i can replace the .length calls)
  • How do i receive a list when calling FindObjectsOfType?

For the list size, use List.Count. List<T> Class (System.Collections.Generic) | Microsoft Learn FindObjectsOfType always returns a built-in array. The only Javascript array you’re using in that code is upgradeArray.

–Eric

Is there a way of making FindObjectsOfType return List’s? Or make it compatible with Unity Flash?

As I said, FindObjectsOfType always returns a built-in array. I can’t imagine that built-in arrays don’t work with Flash; that would make it entirely broken since the Unity API uses built-in arrays heavily. Built-in arrays are not Javascript arrays.

–Eric