Classes and Javascript

Hi, looked around on the internet on different classes tutorials and examples.
I know that javascript by default creates a class that is derived from MonoBehaviour and has the same name as the script.
And also that making more than one class per script is not that good.

I’m making a arena game and so far i have one script with ALOT of variables for all the enemies and was thinking of making a class to shorten the inspector down a bit and making it easier to create new types of enemies.
But since i got everything on one script my question is if i should split up the script into a couple of smaller scripts. Like one for movement, one for attacking and maybe one for taking damage. Then it would make it better to create classes and easier to create new types of enemies.(And shorten down the awfully long script)
And if i do this what is the best way to communicate between these scripts?

Is this what i should be trying to do or should i try to this in another way, if so what?

Thanks for your time.
//Randomly

The coolest thing about programming is that there is no real wrong or right way to do things. It all comes up in how you can set it up in your head.

For your project it sounds like you have all the available features for your character on one script. So players or ai would have their own scripts and copying the abilities from one to the other is as easy as copy and paste…

Now, for what your asking… The word is INHERITANCE. That will make the whole job a thousand times easier. An upper cut, is an attack, which is an ability. not to be confused with a jump, which is a movement which is an ability. Using inheritance you can sub divide things out and still call them abilities. You can even reference them as such. :wink:

Check out this link to something I wrote out for someone yesterday:
http://forum.unity3d.com/threads/107893-confused-on-structs?p=713794&viewfull=1#post713794

Since you put it like that i think i will split up my codes a bit so everything is not on the same script as it gets quite annoying after some time with stupidly long scripts which are impossible to find what you are looking for in.

I dont really understand what you are meaning with inheritance. If you could explain that for me it would be highly appreciated.
And i have seen you around these forums a bit and you should know where i can find a good tutorial on classes how they can be used in a good and efficient way. Would be very cool if you could help me with this. (Writing in javascript by the way)

Inheritances are a way to make similar classes without re-declaring the common variables. Let’s take an example.

Suppose you have an Animal Class. This class is like

class Animal {

var legs : int;
var eyes : int;
var teeth : int;

function Animal (legs : int, eyes : int, teeth : int) {

      this.legs = legs;
      this.eyes = eyes;
      this.teeth = teeth;

}

}

A simple example of a generic dog based on that class would be :

var dog : Animal = new Animal(4,2,24);

which would construct a new Animal called dog with 4 legs, 2 eyes and 24 teeth.

Now assume that you want to make a class that is called Pet. This class for the example purpose would have all of the variables of the animal class, but since we’re talking about pets, it should also have a String variable for name (since we always name our pets). Instead of just copy pasting all the variables from the Animal class on the Pet class, you can make Pet class inherit from Animal in this way :

class Pet extends Animal {

var name : String;

function Pet (name : String, legs : int, eyes : int, teeth : int) {
      this.name = name;
      super.legs = legs;
      super.eyes = eyes;
      super.teeth = teeth;
}

}

Now you can simply create a pet like this :

var myPet : Pet = new Pet("Barfy",4,2,24);

which would create a Pet called Barfy, with 4 legs, 2 eyes and 24 teeth.

Note that when referencing variables of the current class we use the this keyword while referencing variables of the parent class (the one we extend to) we use the super keyword.

I cover classes, inheritances and OO programming extensively on my tutorials on youtube. Link is on my signature.

Wow, thank you. I think i get it now, gonna watch your tutorials today and see what i can learn.