From Unify Community Wiki
(Difference between revisions)
Jump to: navigation, search
m (Reverted edits by Joseph05408 (Talk); changed back to last version by Mindlace)
m (added category tags)
Line 42: Line 42:
; Remove(aMember) : Removes aMember. Does not raise an error if aMember doesn't exist in the array.
; Remove(aMember) : Removes aMember. Does not raise an error if aMember doesn't exist in the array.
; Add(anObject) : Equivalent to <code>anArray[] = aMember;</code> except that <code>anArray.Add(anObject)</code> works.
; Add(anObject) : Equivalent to <code>anArray[] = aMember;</code> except that <code>anArray.Add(anObject)</code> works.
[[Category: Legacy]]
[[Category: JavaScript]]

Latest revision as of 03:43, 12 November 2018

When using JavaScript, the basic data structures do not behave as you expect. There are other data structures you can use, but they have syntactic nuances. You can read the MSDN page about these data structures but the basics are collected here.


[edit] Static Typing

Unity uses Javascript's data typing features to help it figure out the types of the variables you use. You are often not required to use the static typing features because the Unity engine will try to figure out what type you mean. When you use dynamic typing, sometimes the error messages are less clear than when you use static typing - so if you get an incomprehensible error message, try adding type definitions to your variables, especially loop variables - eg for (var someVar: GameObject in anArray).

variable creation function definition
var someVar: VariableType; function fooBar(variable: VariableType)

[edit] Variable Creation/Assignment

someVar = "foo";
var someVar = "foo";
var someVar: VariableType;
var someVar: VariableType = "foo";

The two dynamic examples are more-or-less identical in browsers. However, Unity always interprets someVar = "foo"; as being assignment to an already existing variable, and var someVar = "foo"; as creating a variable in the current scope. This can mean that you're accidentally creating someVar if you use the var prefix when you mean to assign to an existing variable. Inspector Tip: Creating "blank" variables in the global namespace (outside of any function) using the var someVar: VariableType; syntax is necessary for it to show up in the Inspector.

[edit] Data Structures

[edit] Hash Tables

Hash tables are an important exception to all the suggestions above. You cannot initialize an empty hash-table using var aHash: Hash; . You must use var aHash = {}; to initialize a hash table.

Significant functions/methods of the hash table:

An integer number of elements in the hash table.
A collection (list-like data structure) of the keys in the hash table.
Is aKey a key in the hash table?
Removes a key/value pair.
Add(aKey, aValue) 
Equivalent to aHash[aKey] = aValue;

[edit] ArrayLists

You might be tempted to use new Array(); for your arrays. While this is understandable, the Array implementation has some significant limits, notably not supporting the indexOf method. The ArrayList has many of the convenience methods one might want. Warnings:

  • You cannot create a null ArrayList using var someArray: ArrayList; and subsequently access that ArrayList's members without first creating it. Workaround: in your Awake function, do var someArray = new ArrayList();
  • You cannot access the ArrayList constructor - var someArray = new ArrayList(foo, bar, baz); doesn't work. Workaround: You must create the ArrayList and subsequently populate it.

Significant functions/methods of the array list:

An integer number of elements in the array. Use this instead of length. e.g. for (i=0; i < anArrayList.Count; i++) { ... }
Is aMember an element in the hash table? e.g. if (anArrayList.Contains(anObject)) { ... }.
Removes aMember. Does not raise an error if aMember doesn't exist in the array.
Equivalent to anArray[] = aMember; except that anArray.Add(anObject) works.
Personal tools