I can access member variables and functions in one script from another script without problem, buy apparently not member classes. For example:
GUIToolkit.js:
var v1 : int = 1;
static var v2 : int = 2;
class WindowHelper { /* … */ }
static var instance : GUIToolkit;
function Start() { instance = this; }
GUI.js:
print(GUIToolkit.instance.v1);
print(GUIToolkit.v2);
var v = new GUIToolkit.WindowHelper();
var v = new GUIToolkit.instance.WindowHelper();
The last two lines produce errors: Unity says that WindowHelper is not a member of GUIToolkit. What gives?
The reason I want to declare member classes (rather than having a separate script file for each class) is (a) to avoid having to attach a large number of scripts to a game object, and (b) so that the set of classes are scoped to a namespace (i.e. I can refer to GUIToolkit.WindowHelper, rather than just WindowHelper).
Is there a way to do this with Unity Javascript? Do I need to switch to C# (or even Boo)?
I had many internal debates around the C# vs. Javascript, but in the end I figured that it would be easier to isolate functionality into separate assemblies (when small projects turn into large projects) if the code was already in C#.
That seems to imply Unity Javascript is not the same as Mono JScript, since otherwise you could still use assemblies with Javascript right?
Anyway, I think I’m going to bite the bullet and learn some C#, since Unity Javascript is too dissimilar to regular Javascript to be comfortable; in the end it’ll be faster to learn a new language (that’s well defined and documented) than try to figure out all the nuances of Unity Javascript.
And yes… that was my thinking on it. With the work that I currently do the class structures are definitely going beyond just being scripting modules.
Because it’s in C# I can simply copy and paste it into a Visual Studio Class Library project (And I assume the same holds true for a mono development environment).
This means that I could re-use the same components that I prototyped in Unity.
For example: Currently I’m thinking that I want Unity to talk to a web-server using web-services and I want to transmit a serialized gamestate entity in it’s entirety. However from the web-server I still might want to have the ability to decode the savegame to pull some properties out. Because all my entities are defined in C# creating a separate assembly and deserializing it is trivial.
Also, if I pay special attention to component based development it means that it becomes easier to plug in different networking modules (such as a https mesh streaming component for GIS that I collaborated on with a friend), or I could even take entire portions of my entity management system and stick it into an XNA game.
I’m not sure how nicely JScript will play with Unity Javascript, maybe it is compatible after all. But I certainly haven’t gotten that impression.