Getting Started
Below is a quick tutorial to get started with Custom JavaScript scripting.
Before continuing make sure that you install the SDK.
Create a JavaScript File
To start, let's create a JavaScript file that we will use to code.
Right click somewhere in your project folder.
Select Create > Javascript File.
Name your Javascript file as you wish.
Writing a Script
The Metaverse Scripting engine provides some basic interop that is familiar to those who have experience with Unity C# components. See the example below:
As you can see, much (if not most) of what you can do in Unity can be done in JavaScript.
Slight Caveats
Since the JavaScript is being translated into C#, some things may have to be done with odd conventions.
JavaScript Object and Array Initialization
Objects and arrays cannot be initialized with variables, instead they must be initialized, and then the variables assigned afterward, like so:
Failure to do this will result in missing variable declarations and/or array elements.
Method Overloads & Default Parameters
When calling a C# method from JavaScript, it's important to understand that the system does not automatically resolve overloads. Consequently, it's uncertain which version of an overloaded method will be called. Moreover, methods won't automatically resolve parameters that have default values. To effectively call C# methods from JavaScript, one must consider these limitations and possibly use more explicit methods or handle parameters in a way that ensures the correct method overload is used and default parameters are appropriately managed.
Namespaces
To use namespaces, declare them with const MyNamespace = importNamespace('myCSharpNamespace');
. References to types within this namespace must explicitly use MyNamespace.MyClass
since namespaces are not automatically referenced.
Performance
One should strongly consider the performance implications of using JavaScript for custom scripting (as opposed to Visual Scripting and PlayMaker) since it can cause potentially large GC allocations.
If you do utilize JavaScript, use a system/manager/service design pattern, rather than individual components per object.
If you'd like more information on how translation from C# to JavaScript might work, consult the Jint documentation.
For information on what functions are not allowed, click here.
Attaching a Script to a Game Object
To allow your script to run on a specific Game Object, you'll want to follow these instructions.
Add the
Metaverse Script
component to your game object of choice.Once added, assign the JavaScript file to the
JavaScript File
field in the inspector.
You've successfully created your first custom script! Now you can hit the Play button and test it out! đ
Last updated