Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jint + XNA (C#)

Tags:

c#

xna

jint

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)?

like image 504
user241949 Avatar asked Jan 01 '10 14:01

user241949


2 Answers

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;
    }
}
like image 65
Nicolas Penin Avatar answered Nov 09 '22 22:11

Nicolas Penin


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;
    }
}
like image 2
Aviad P. Avatar answered Nov 09 '22 23:11

Aviad P.