Array syntax?

I’m still very unfamiliar with Unity, but I’ve been slowly absorbing how to go about things.

My question is about how to convert some techniques I had been using in ActionScript into Unity Javascript.
Particularly in regard to arrays.

In ActionScript, I was able to make an array of game item data like this:

var weap = [
	{Name: "M16", Ammo: 2, Rng: 2, Acc: 50, Dmg: 60},
	{Name: "M4", Ammo: 2, Rng: 2, Acc: 60, Dmg: 55},
	{Name: "MP5", Ammo: 3, Rng: 1, Acc: 70, Dmg: 75}
];

This had a number of benefits, including ease of coding, and ease of referencing.
It’s organized, compact, labeled, easy to read, easy to compare and tweak values, etc. (Whereas a vertical listing of unlabeled data is a nightmare.)

When I want to reference values, it’s easy to call and clear to read.
For example, if I want to find the damage of weap index 2, all I had to do to is:

myDamage = weap[2].Dmg;
// returns 75

What’s the proper way to do this in Unity Javascript?
Which array type is best? Is this a 2D Array?

I think the best way is using of classes and generic collectrions like Dictionary<> or List<>.
Code bellow is not JS, but C# (in JS it should be almost the same)

class Weapon{
   public string Name; 
   public int Ammo;
   public float Rng;
   public float Acc;
   public float Dmg;
}

...

Dictionary<string, Weapon> weapons = new Dictionary<string, Weapon>();
weapons.Add("M16", new  Weapon(){ Name = "M16", Ammo = 2, Rng= 2, Acc= 50, Dmg= 60});
weapons.Add("M4",   new  Weapon(){ Name = "M4", Ammo = 2, Rng= 2, Acc=60, Dmg= 55});
weapons.Add("MP5", new  Weapon(){ Name = "MP5", Ammo = 3, Rng= 1, Acc= 70, Dmg= 75});

...

myDamage = weapons["M16"].Dmg;

Patico, how are lists better than arrays if he never needs to change the size at runtime ? It’s just adding some processing to fetch items…

public class Weapon extends System.ValueType
{
    var Name : String;
    var Ammo : int;
    var Rng : int;
    var Acc : int;
    var Dmg : int;

    public function Weapon(name : String,  ammo : int, rng : int, acc : int, dmg : int)
    {
        this.Name = name;
        this.Ammo = ammo;
        this.Rng  = rng ;
        this.Acc  = acc ;
        this.Dmg  = dmg ;
    }
}

public var weap : Weapon[];


function Start () {
    weap = new Weapon[3];
    weap[0] = new Weapon("M16", 2, 2, 50, 60);
    weap[1] = new Weapon("M4", 2, 2, 60, 55);
    weap[2] = new Weapon("MP5", 3, 1, 70, 75);

    //with C# you could do that but unityscript doesn't seem to support it (actually you wouldn't even need the contructor but hey..)
    //weap[2] = new Weapon() {Name: "MP5", Ammo: 3, Rng: 1, Acc: 70, Dmg: 75};
}

ps: there may be a better way to fill the array but I never use unityscript except on this forum so I’m not familliar with all its syntactic sugar.

http://docs.unity3d.com/Documentation/ScriptReference/Array.html

First of all it give you more readable code.
For example:

/// this stroke is easier to understand
myDamage = weapons["M16"].Dmg;

// this is not (probably just for me, everytime when I see such code I ask himself: "Why '2'? What does '2' mean?")
myDamage = weapons[2].Dmg;

The other reason is spead of reading data, Dictionaries looking for data more quickly then array.
Also you can setup initial size of List or Dictionary while initialization via contructor.

That’s not the case at all; if you’re just retrieving a value from the collection, getting an entry in an array by index value is much (much MUCH) faster than using a string value to reference an entry in a Dictionary. What you should do is use an array, and use an enum to reference it.

enum WeaponType {M16, M4, MP5}

myDamage = weapons[WeaponType.M16].damage;

This way you get the efficiency of using ints to reference array entries while remaining human-readable. If there’s ever any case where it would make sense to replace strings with enums, do it. (I would also recommend not making potentially confusing abbreviations, which doesn’t have any benefits; just write stuff out…“damage” vs “dmg”.)

–Eric

Thanks for the replies. It looks pretty much like what I want. But I don’t completely understand it.

I tried Googling some of the terms, and I guess this is a structure of some kind… whatever that means.
System.ValueType doesn’t appear in the unity script reference.
I have no idea what’s going on in the first 17 lines. It looks awfully redundant, seeing as each label needs to be defined three separate times.
Why does Weapon appear to be a class, a function, and an array?
Can someone explain what is happening here?

On the plus side, I was able to grok enum - (Though you can’t look up enum in the unity reference either.)

Almost none of the general programming/.NET/Mono stuff is in the Unity reference, because it’s all thoroughly documented on MSDN so there’s no need to repeat it. For how classes work, either look up C# classes (since Unityscript works the same way except for the syntax differences), or else look up Actionscript3 classes, which also work the same way and have basically the same syntax as Unityscript.

–Eric

/*
 * This is the blueprint of you weapon, you're just designing what composes it.
 * It's about the same thing you do when you create a UnityScript and give it public variables,
 * except that you cannot attach it to a gameObject as a component, it just holds data and potentially functions
 */
public class Weapon extends System.ValueType
{
    var Name : String;
    var Ammo : int;
    var Rng : int;
    var Acc : int;
    var Dmg : int;

    //This isn't just a function, it's called a "constructor". It has the exact same name as the class and it's called upon instanciation (with the "new" keyword...)
    public function Weapon(name : String,  ammo : int, rng : int, acc : int, dmg : int)
    {
        this.Name = name;
        this.Ammo = ammo;
        this.Rng  = rng ;
        this.Acc  = acc ;
        this.Dmg  = dmg ;
    }
}

//Now this is an actual variable of your component, and it is an array of objects of the previously defined class type.
public var weap : Weapon[];

Ok, I think I’m understanding more about classes and constructors.
At least, everything seems to be making sense now. Thanks for the help.

But I’m not sure why Weapon extends System.ValueType. As opposed to just simply: class Weapon?
The extending wasn’t used in similar examples I found, so why is the inheritance necessary here?

Extending System.ValueType makes it a struct, since there isn’t a “struct” keyword in Unityscript. (Which, by the way, should not be done in this case. It should just be a normal class.)

–Eric