Created
May 18, 2017 02:18
-
-
Save unitycoder/fc656e639500de43cec8b08ee5e287a5 to your computer and use it in GitHub Desktop.
Moonsharp Custom Type Converters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// custom type converters | |
// https://groups.google.com/forum/#!topic/moonsharp/On6dhfrpn1A | |
public static class ScriptHelper | |
{ | |
public static void RegisterSimpleFunc<T>() | |
{ | |
Script.GlobalOptions.CustomConverters.SetScriptToClrCustomConversion(DataType.Function, typeof(Func<T>), | |
v => | |
{ | |
var function = v.Function; | |
return (Func<T>) (() => function.Call().ToObject<T>()); | |
} | |
); | |
} | |
public static void RegisterSimpleFunc<T1, TResult>() | |
{ | |
Script.GlobalOptions.CustomConverters.SetScriptToClrCustomConversion(DataType.Function, typeof(Func<T1, TResult>), | |
v => | |
{ | |
var function = v.Function; | |
return (Func<T1, TResult>) ((T1 p1) => function.Call(p1).ToObject<TResult>()); | |
} | |
); | |
} | |
public static void RegisterSimpleAction<T>() | |
{ | |
Script.GlobalOptions.CustomConverters.SetScriptToClrCustomConversion(DataType.Function, typeof(Action<T>), | |
v => | |
{ | |
var function = v.Function; | |
return (Action<T>) (p => function.Call(p)); | |
} | |
); | |
} | |
public static void RegisterSimpleAction() | |
{ | |
Script.GlobalOptions.CustomConverters.SetScriptToClrCustomConversion(DataType.Function, typeof(Action), | |
v => | |
{ | |
var function = v.Function; | |
return (Action) (() => function.Call()); | |
} | |
); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// use it with | |
ScriptHelper.RegisterSimpleFunc<Vector3>(); | |
ScriptHelper.RegisterSimpleFunc<bool>(); | |
ScriptHelper.RegisterSimpleFunc<float>(); | |
ScriptHelper.RegisterSimpleFunc<Unit>(); | |
ScriptHelper.RegisterSimpleFunc<List<Unit>>(); | |
ScriptHelper.RegisterSimpleFunc<List<Vector3>>(); | |
ScriptHelper.RegisterSimpleFunc<String>(); | |
ScriptHelper.RegisterSimpleFunc<List<UnitViewComponent>>(); | |
ScriptHelper.RegisterSimpleFunc<List<String>>(); | |
ScriptHelper.RegisterSimpleAction(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment