tags:

views:

299

answers:

4
+4  Q: 

Jint + XNA (C#)

Hi :) Would it be possible use jint to manipulate a 3D environment created using XNA (C#), and add functionality to this envrionemnt (again using jint)?

Thanks

Q

+1  A: 

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.

Peter Lillevold
+2  A: 

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;
    }
}
Aviad P.
Nice - Lua needs more exposure for .net solutions.
David Robbins
A: 

Thanks. Would you recommend Lua?

Please don't use answers to leave comments. Use the comment feature.
Brian Rasmussen
+2  A: 

Hi,

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;
    }
}
Nicolas Penin
Hi Nicholas. Thanks for the post. I havent actually worked with inetgrating scripts with a .net language before. Can u gimme an example of how i could manipulate 3d objects (created with c#) using jint? thanx
Hi,I surely can give it to you, but I think it would need a long discussion on how to make them available in Jint : The first possibility is to make the Game object available directly in Jint :JintEngine engine=new JintEngine().SetParameter("game",yourGameObject);The other possibility is to declare only the objects you need. Of course, this way is much less dynamic. It is up to you, and also depends on your needs. If you want to discuss more about it, please create a thread on http://jint.codeplex.com/Thread/List.aspx
Nicolas Penin
Thank you again!