This walkthrough will just give you a taste of MoonSharp and its simplicity and power. There are better ways to use MoonSharp, but this is the easiest one. Other tutorials will dig further and teach you how to use MoonSharp at its best. For now, let’s get started.
Most tutorials on this site are C# only - this is the only page with some pointers to get VB.NET users started.
MoonSharp is compatible with all CLR languages - C#, VB.NET, C++/CLI, F#, Boo and whatever else you like. It could also work with DLR languages (IronPython, IronRuby, etc.).
Examples, however, will only be produced for C# (and in the case of this introductory page, VB.NET) as keeping examples in all languages is a huge effort.
Most tutorials are implemented in the tutorial project on GitHub.
The first step is to get MoonSharp in your IDE. There are several paths to this, depending on what IDE you are using - Visual Studio, MonoDevelop, SharpDevelop or Unity.
In the package manager, type:
Otherwise, right click on “References”, “Manage NuGet Packages”, open the “Online” dropdown, select nuget.org and search for a package named “MoonSharp”.
Under the “Project” menu, select “Add Nuget packages…”. In the following window, search for MoonSharp
.
Select the project whose id is exactly MoonSharp
.
Refer to the documentation of your IDE to add MoonSharp.Interpreter.dll
, contained in the folder of the target of your choice as a dependency.
A Unity asset store package is pending approval now and will be at this URL as soon as it gets approved. As soon as it gets approved, using it becomes the recommended way to install MoonSharp in Unity.
Put the MoonSharp.Interpreter.dll
, contained in the interpreter/net35
folder
of the MoonSharp distribution, in your Assets/Plugins
folder.
If Windows Store apps / Windows Phone support is needed, copy MoonSharp.Interpreter.dll
contained
in the interpreter/portable-net40
folder of the MoonSharp distribution, in an Assets/Plugins/WSA
folder.
Then, follow this guide.
After this, add them as references from MonoDevelop-Unity.
Note: if you are going to use MoonSharp in IL2CPP projects, also create/edit a link.xml inside your Assets directory with this contents:
Import the MoonSharp.Interpreter namespace into your code, adding the following lines at the top of your code:
In C#:
In VB.NET:
Here we create a function MoonSharpFactorial which calculates a factorial using MoonSharp.
In C#:
In VB.NET:
To run your code, of course, you just have to call the MoonSharpFactorial function somewhere else in your code.
Now you are ready to jump at the other tutorials.