Jint + XNA (C#)

好久不见. 提交于 2019-12-07 03:32:08

问题


Is it possible to use jint to manipulate a 3D environment created with XNA (C#), and to add functionality to this environment (again using jint)?


回答1:


As a contributor to Jint, I would recommend you Jint. Jint makes it more simple than what Lua does. Moreover, I don't know if this is possible with Lua, but you can give it .NET objects and play with them in javascript (Jint stands for Javascript INTpreter). You can also secure your application with Permissions Set. Here is the same code provided before with Jint :

    class Program
{
    static void Main(string[] args)
    {
        Program p = new Program();
        p.Run();
    }

    private void Run()
    {
        JintEngine engine = new JintEngine();
        engine.SetFunction("GTest", new Jint.Delegates.Func<object, double>(LUA_GTest));
        engine.Run("GTest([['3,3']])");
    }

    private double LUA_GTest(object d)
    {
        Console.WriteLine("Got {0} - {1}", d.GetType().ToString(), d.ToString());
        while (d is ArrayList)
        {
            d = ((ArrayList)d)[0];
            Console.WriteLine("Got {0} - {1}", d.GetType().ToString(), d.ToString());
        }
        if (d is string)
        {
            d = double.Parse((string)d);
            Console.WriteLine("Got {0} - {1}", d.GetType().ToString(), d.ToString());
        }
        if (d is double)
            return (double)d * 2;
        return 0;
    }
}



回答2:


Jint is an option, LUA is an option check out LuaForge

LUA is a really fun and easy to use language, with nice support for cooperative multitasking (coroutines). Its basic data type is a table (which is a cross between a dictionary and an array) which is very flexible and powerful.

Here's something I wrote up just now just to test it. I am registering a function for the script called GTest which maps to a C# method in my object called LUA_GTest. The method accepts a general object, and in the script I'm passing to it a table containing a table containing a string representing a double. In C# i'm unwrapping everything and returning a value based on the double value.

class Program
{
    static void Main(string[] args)
    {
        Program p = new Program();
        p.Run();
    }

    private void Run()
    {
        Lua lua = new Lua();
        var methodInfo = typeof(Program).GetMethod("LUA_GTest", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic);
        lua.RegisterFunction("GTest", this, methodInfo);
        lua.DoString("GTest({{\"3.3\"}})");
    }

    private double LUA_GTest(object d)
    {
        Console.WriteLine("Got {0} - {1}", d.GetType().ToString(), d.ToString());
        while (d is LuaTable)
        {
            d = ((LuaTable)d)[1];
            Console.WriteLine("Got {0} - {1}", d.GetType().ToString(), d.ToString());
        }
        if (d is string)
        {
            d = double.Parse((string)d);
            Console.WriteLine("Got {0} - {1}", d.GetType().ToString(), d.ToString());
        }
        if (d is double)
            return (double)d * 2;
        return 0;
    }
}



回答3:


Take a look at this SO question, considering how to choose a scripting platform for .Net.

In general, sure you can build a scripting engine into your XNA application. Using a scripting engine and providing hooks into your app is not much different than calling external assemblies through public interfaces.



来源:https://stackoverflow.com/questions/1988767/jint-xna-c

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!