-
Notifications
You must be signed in to change notification settings - Fork 1
How to start
Clone this repo, open MHScript.csproj in your VisualStudio and build this project. You will get the dll of this library. Copy dll to your project and add reference to it.
Firstly, we need to create the Engine object and configure it.
private void warning(string message) {
MessageBox.Show(message, "Warning!");
}
...
Engine engine = new Engine(new Engine.WarningFunction(warning));
Warning function is used by engine to indicate not critical errors.
Then you can create your own global functions:
private object test(Engine engine, params object[] args) {
if (args.Length >= 1) {
MessageBox.Show(args[0].ToString());
} else {
MessageBox.Show("Неверное количество аргументов в test(..)!");
}
return null;
}
...
engine.addGlobalFunction("test", new GlobalFunction() {
function = new GlobalFunction.UniversalFunction(test),
//For documentation generator
functionDocsName = "void test(string message)",
functionDocsDescription = "Выводит MessageBox на экран"
});
This function takes the engine object in which the script is executed, and the arguments passed to the function (as the object array). The return value of the function is returned to it's caller. functionDocsName and functionDocsDescription is just a addition for documentation generator (you can pass null, but i recommend to describe they for future using)
And the last step. Compile your script from text and run it in the engine.
string scriptText = "test('Hello, world!');";
Script script = engine.parseScript(scriptText);
script.execute(engine);
After this step the engine will contain all local functions from your script (if you define functions like that: "function name(arg0, arg1, ....) { //code }") For example:
Script script = engine.parseScript("function myTestFunction(message) { test('Your message: ' + message); }");
script.execute(engine);
//Script does nothing, but now we can execute "myTestFunction"
engine.executeFunction("myTestFunction", "Hello, world!");
private object test(Engine engine, params object[] args) {
if (args.Length >= 1) {
MessageBox.Show(args[0].ToString());
} else {
MessageBox.Show("Неверное количество аргументов в test(..)!");
}
return null;
}
private void warning(string message) {
MessageBox.Show(message, "Warning!");
}
private string scriptText = @"
function myTestFunction(message) {
test('Your message: ' + message);
}
";
....
Engine engine = new Engine(new Engine.WarningFunction(warning));
engine.addGlobalFunction("test", new GlobalFunction() {
function = new GlobalFunction.UniversalFunction(test),
functionDocsName = "void test(string message)",
functionDocsDescription = "Выводит MessageBox на экран"
});
Script script = engine.parseScript(scriptText);
script.execute(engine);
engine.executeFunction("myTestFunction", "Hello, world!");