Jint + XNA (C#)
Asked Answered
E

3

6

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

Effect answered 1/1, 2010 at 14:45 Comment(2)
An important question is this: do you want your app to be able to run on xbox? if so your choice is cut down quite a lotTenterhook
Not really. I only care about it running on a PC.Effect
T
4

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;
    }
}
Tuning answered 2/1, 2010 at 10:44 Comment(2)
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? thanxEffect
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 jint.codeplex.com/Thread/List.aspxTuning
N
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;
    }
}
Nette answered 1/1, 2010 at 15:14 Comment(1)
Nice - Lua needs more exposure for .net solutions.Ordinance
R
1

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.

Romany answered 1/1, 2010 at 15:4 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.