private var playerList = new ArrayList();
class PlayerNode {
var playerName : String;
var networkPlayer : NetworkPlayer;
}
private var chatEntries = new ArrayList();
class ChatEntry
{
var name : String= "";
var text : String= "";
}
Is it the exact same thing as making .js files called PlayerNode and ChatEntry ? Or are they some nuances ?
Reverse engineering. I am still looking carefully at the M2H tutorial, it was there. And few lines later, there is :
var newEntry : PlayerNode = new PlayerNode();
newEntry.playerName=playerName;
newEntry.networkPlayer=Network.player;
I thought a gameObject was required when instantiating a component (array and such things are not component and do not require gameObjects), so it bother me.
Must I understand it is not the same thing ? (writing in code class SpecialName, and creating a SpecialName.js file)
You can declare public classes in files containing (and named for) other classes in both JS and C#. Remember that Javascript files are essentially wrapped by
class MyClassName extends MonoBehaviour {
//The contents of your file
}
So the short answer is, yes, in that code you posted, it’s like having separate files like so:
PlayerNode.js
var playerName : String;
var networkPlayer : NetworkPlayer;
ChatEntry.js
var name : String= "";
var text : String= "";
ChatManager.js
private var playerList = new ArrayList();
prvate var chatEntries = new ArrayList();
//etc.
It’s helpful in Javascript for things like a chat system where you want a separate class containing message information (sender, message, etc). Since you can’t make structs in JS, it can be convenient to place this class with your chat manager class. I wouldn’t recommend putting more than one larger class in the same file mostly because it makes it harder to debug problems.
Reading your answer, burnumd, I got some additional thoughts, thank you.
I tried something :
class ClassTest {
var sentence : String = "Sentence from script reached";
function RelatedFunction () {
print ("Related function reached");
}
}
function Start () {
var instance : ClassTest = new ClassTest ();
print ("instantiated");
print (instance.sentence);
instance.RelatedFunction ();
}
Everything goes smoothly except the function RelatedFunction is not called.
In the case I write :
class ClassTest extends MonoBehaviour
The function is called with the rest but before that, I get a warning message :
I guess using the word “class” to define some exotics kind of container (like an entry chat, or a player node, or a card) is ok, but is not appropriate for functions ?
The function gets reached, it’s just that print is a method of MonoBehaviour. So that’s why it works when your inner class extends MonoBehaviour (see here for more info). Replace “print” with “Debug.Log” and it works as is. You should consider using Debug all the time anyway for debugging purposes, as it allows you to specify the context for the message as well as set different levels (Log, LogWarning, and LogError). As for the warning you mention, you’re not supposed to use “new” to create MB’s, as there’s a lot of set-up done internally by Unity with those. If you want to create a MB in a scene, here’s is the preferred way:
var holder : GameObject = new GameObject ("MonoBehaviourHolder");
var instance : MyClass = holder.AddComponent(MyClass);
It was always getting reached, it’s just that print wasn’t doing anything.
Not necessarily. If you want to create the MonoBehaviour from another MonoBehaviour, you can just use var instance : MyClass = gameObject.AddComponent(MyClass); to add it to the GO the existing MB is already on.
gameObject.GetComponents get all components of that type, so you can do a For loop and find the one you need.
Its Slow so you might as well have just made an array of MyClass if you have 100! (or references to them)