0

I want to pass whole class from c# code to lua, so I can create in LUA a new object and use its methods, fields and so. When it's done, I'd like to know if it's possible to use objects in lua, which were created in a c# code and then passed somehow to lua.

Here's my code: atm, its not possible to create in my luainterface object of class Person1 and then use its function when I enter say() into lua scripter (without creating any object there), I'm getting output of Person2.say()

using System;
using LuaInterface;
using System.Reflection;

namespace ConsoleApplication
{


public class Person1
{
    public void say()
    {
        Console.WriteLine("person1 says: hehe");
    }
}


public class Person2
{
    public void say()
    {
        Console.WriteLine("person2 says: hihi");
    }
}

class Class1
{
    static void Main(string[] args)
    {
        Lua lua_compiler = new Lua();

        Person1 person1 = new Person1();
        Person2 person2 = new Person2();

        lua_compiler.RegisterFunction("say", person1, person1.GetType().GetMethod("say"));
        lua_compiler.RegisterFunction("say", person2, person2.GetType().GetMethod("say"));


        while (true)
        {
            string line = Console.ReadLine();
            try { lua_compiler.DoString(line); }
            catch { }
        }
    }
}
}
Patryk
  • 3,042
  • 11
  • 41
  • 83

1 Answers1

2
    Person1 person1 = new Person1();
    Person2 person2 = new Person2();

    lua_compiler["person1"] = person1;
    lua_compiler["person2"] = person2;


    while (true)
    {
        string line = Console.ReadLine();
        try { lua_compiler.DoString(line); }
        catch { }
    }

In the line you can use person1:say() or person2:say()

Passing the whole class is time consuming if your class have a lot of functions and properties.

Lucas Locatelli
  • 356
  • 5
  • 12