JavaScript Array Support...

Hello fellow geeks!
The reason Im writing in that im trying to create some Javascripts for my project,
however when building the project it mentions an error on correct js syntax…

IT CANNOT RECOGNIZE THE SPLICE MEMBER OF AN ARRAY

above is a copy of the script

//////////////// SCRIPT START ///////////////////
class Queue
{
var queue = [ ];
var queueLenght = 0;

function IsEmpty()
{
return (queueLenght == 0);
}

function GetSize()
{
return queueLenght;
}

function Enqueue(element)
{
queue[queueLenght] = element;
queueLenght += 1;
}

function Dequeue()
{
var element;

if (queueLenght > 0)
{
queue.splice(0); // WHY IS THIS NOT WORKING IN UNITY?
queueLenght -= 1;
}

return element;
}

function GetElement(index)
{
var element;

if( index < queue.length
index >= 0 )
{
element = queue[queueLenght];
}

return element;
}

function GetOldestElement()
{
var element;

if (queue.length)
{
element = queue[queueLenght];
}

return element;
}

}
///////////////// SCRIPT END ////////////////////

ANY CLUES ANYONE?

THANX :smile:

  1. You can use the [ code ] and [ /code ] tags (without the spaces) to make your code readable.

  2. There are two types of arrays in Unity’s JavaScript. The internal .net ones and the Unity’s Array class.
    The documentation for the Unity Array is here:
    http://unity3d.com/support/documentation/ScriptReference/Array.html
    And the .net one here:
    Array Class (System) | Microsoft Learn

  3. You are using the .net arrays, which are fixed-size. Your code will throw an Index Out Of Bounds Exception when you try to run it.
    You either have to recreate/copy the array each time you need to change its length, use an sufficiently large array or use Unity’s Array which can be resized on-the-fly but are slower.

  4. Since the .net arrays are fixed-length, you cannot remove elements.
    You can remove elements in Unity Arrays with RemoveAt(pos).

  5. You don’t need to save the length of the array. You can use array.GetLength(0) for .net arrays and array.length for Unity Arrays.

According to the docs they point out that the supported is the one specified in the MS Docs

:x

SO WHAT IS THE DIFFERENCE BETWEEN GOOD OL JAVASCRIPT AND THE JAVASCRIPT ON THE MS DOCS…

:x
WHICH ONE IS THE ‘REAL’ JAVASCRIPT…

:x
AND HENCE WHAT OTHER FEATURES ARE AVAILABLE/NOT AVAILABLE IN THE UNITY JS AS OPPOSED TO THE ‘CLASSIC’ JS…

thanx!

Unity JS is not JS, it just happens to have similar syntax to make it easier to pick up on. And please turn off capslock.

I started to (but never really got much of anywhere with it) keep a record of some differences between Unity JS and standard JS on the wiki, here. As you discover more differences, feel free to add them there, and ask around on the forums.