Questions about classes.

Hey guys I have two questions about classes in JS.

1) I see a lot of people using the term class and script interchangeably ( the actual .js files themselves). Is this a correct usage of the word?

2) I'm trying to write a classe in JS using examples and tutorials all over the community sites but Unity just seems to love giving me errors. It's my understanding that you can put all sorts of variables and functions inside of classes but I'm not having any success. Here's a bit from the .js file as an example;

class Fighter{

    //Fighter's stats
    ... // bunch of vars here for things like HP, Strength, etc.

    function OnGUI(){
        // the buttons that appear inside the 
        //abilities window and their functions  
        function Abilities (windowID){

            var AttackCommand = GUI.Button(Rect(10, 25, 60, 20), "Attack");

            if (AttackCommand == true){

                bEquations.AttackMonster(Fighter, Monster);
                Abilitieswinswitch = false;
                Timer = 1;
            }

            ... // more functions 

}

repeat for additional character classes like wizard, cleric etc. Now the problem is that Unity gives me a bunch of errors such as;

expecting } found 'function'.

expecting } found 'Abilities'.

';' expected insert semicolon.

Unexpected token: var

just about everywhere I have a function or variable inside of a function. Is this just a simple syntax error or am I violating rules?

Thanks in advance!

You can't put functions inside other functions. Your Fighter class would have to derive from Monobehaviour in order to have a working OnGUI function in it, but it's unnecessary...in JS, the script is automatically a class that derives from Monobehaviour. So you can just name the script Fighter and it's a Monobehaviour-derived class called Fighter. In C#, you have to explicitly do this yourself. It's not a problem to have other classes in the script, but it depends on what you're doing whether it's appropriate...you wouldn't want Wizard or Thief in the Fighter script, for example; they should be separate scripts.

To answer one of your questions in short, yes, you should typically split up a lot of your work into several scripts where applicable.

In fact, if you're programming with C# and are integrated with Visual Studio, one of the first things you notice while working inside Visual Studio is that each script appears in the solution as a class, like it would if you were making a C# application from scratch, so that might be rather telling. Now, maybe class isn't the proper wordage, but each separate script can be used in a similar fashion.

The only thing is that each script isn't necessarily a self-contained prefab object, but it can hold data for your prefabs, working in tandem with prefabs to basically behave like a class.

But this is diving in too deep and the important thing to take from this is probably akin to what s4vi0r said. Each separate game object that has a script attached to it owns its own self-contained version of the script, which in my opinion makes game design logic far easier.

Howdy Catsby,

This link should help you a lot. Tells about unity javascript vs. traditional javascript, syntax classes.. whole nine yards.

http://www.unifycommunity.com/wiki/index.php?title=Head_First_into_Unity_with_JavaScript

In response to your comment (was to big to fit in comment section)

Honestly, The games I normally make aren't complex enough for classes. I might use classes if I wanted to inherit some info from a class I had already created. Like a Person base class. Then I would inherit Person when I create Good Guy class and Bad guy class. Coming from C++ background I would normally do this.

I sort of just try to stay away from classes in Unity JS unless I think it will speed up my process.

I will just treat the script as the class. So if I make a Enemy Gameobject. I will make a script that has all of his members and functions.

Whats nice is I can prefab this enemy. So I can create another just like him with that script attached to it. And his script which holds his info is unique to him.

My enemy script might look like this:

var health : int;

    function Start()
    {
      health = 100;
    }

    function Update()
    {

    }

    function TakeDamage()
    {
      health -= 1;
      if(health == 0)
      {
        Destroy(this.gameObject);
      }
    }

I hope this helps.