- Home /
Inheriting from classes from other files (Javascript)
I'd like to say before anything else that I'm still fairly new with programming in general. Especially when dealing with classes.
Basically in my setup that I have now, I have 'Entities.js' which contains all my functions and base classes that other scripts use. Other scripts use it by having 'import Entities;' at the top. This script is not meant to be put on a gameobject. In Entities.js I have a base class called 'CharacterBase'. It's the base class for the player, enemies, or anything that moves and can die.
When writing the code for the player in a separate .js file, I want the file itself to inherit from the CharacterBase class so I can then attach it to a gameobject. (Unity treats each script file its own unique class.)
I sort of tried to work around this issue when I first encountered it by just defining the player class within the file and then creating an instance of it. I ended up naming the file 'PlayerInit.js' instead of the intended 'Player.js'. As my project has been growing, I have quickly realized that it is going to cause a lot of issues. One of these issues includes not being able to get data from the attached gameobject or not being able to detect collisions. Another issue is other scripts having to go through multiple classes to call a single function.
I know that if I was doing this in C# none of this would be an issue because you must specify what the class the file inherits from. I rather dislike some of the syntax that C# uses, as well of how it instances objects, so I would really prefer to stick to Javascript if possible.
Below are the codes for my files.
If I have my class setups completely wrong, please tell me how I should be doing it. I'm using pastebin links because they are kind of lengthy.
Answer by Eric5h5 · Feb 22, 2013 at 04:39 AM
The only thing different about Unityscript compared to C#, regarding classes, is that script files are automatically classes inheriting from MonoBehaviour. But you can ignore that if you want, and it doesn't prevent you from making other classes that don't inherit from MonoBehaviour. The only thing it does is make it easier to write scripts that include MonoBehaviour stuff like Update, but you can explicitly create a class that extends MonoBehaviour if you want.
However, judging from statements like "One of these issues includes not being able to get data from the attached gameobject or not being able to detect collisions", it sounds like maybe you shouldn't be using classes extending other classes in the first place, but should be using a component model. In Unity, you add Components to GameObjects, and can use GetComponent as needed. So instead of things like "blah = new Foo()", you Instantiate prefabs that have various scripts attached as desired.
As an aside, you don't really need those convert functions, since Unity already does that implicitly:
var v3 = Vector3(1, 2, 3);
var v2 : Vector2 = v3;
Or:
var v2 = Vector2(1, 2);
var v3 : Vector3 = v2;
Wasn't aware that you could do that with vectors.
$$anonymous$$aybe I wasn't quite clear on what I want. Basically I need the 'Player' class defined in 'PlayerInit.js' to become 'Player.js'. (PlayerInit is removed completely) This way I can attach the player class to a gameobject.
The issue I have is that I cannot tell unity that I want a class defined as a javascript file to inherit another class. It just defaults to inheriting $$anonymous$$onoBehaviour when it compiles. Where as in C# you $$anonymous$$UST say what the file inherits.
example.cs
using UnityEngine;
using System.Collections;
public class example : $$anonymous$$onoBehaviour {
void Example() {
print( Application.loadedLevel );
}
}
example.js
print ( Application.loadedLevel );
Both scripts cause unity to create a class called 'example'.
If I want 'example.js' to inherit from another class... Where do I specify that I want to do this?
The issue I have is that I cannot tell unity that I want a class defined as a javascript file to inherit another class.
Yes you can; as I said it works the same as C#, except that scripts are automatically classes that inherit from $$anonymous$$onoBehaviour (but you can ignore that if you want). These are the same:
// Foo.js
function Start () {}
// Foo.js
class Foo extends $$anonymous$$onoBehaviour {
function Start () {}
}
The second method is optional. If you don't want to inherit from $$anonymous$$onoBehaviour, you don't have to:
// Foo.js
class Foo {
function Bar () {}
// can't attach this script to a GameObject,
// since Foo doesn't extend $$anonymous$$onoBehaviour
}