2

I use SharpLua with MonoDevelop. I created a class on C# side, which should be usable from Lua. That's works fine, I can access all fields from Lua. It's very easy.

public class Test {
  public string Name;
}

could be access from Lua with

print(test.Name)

Now, I want to create new fields by Lua. In Lua it should look like

test.abc = "A string"
print(test.abc)

But this didn't work. I get an error in the ObjectTranslator. So I couldn't extend the table from Lua. I didn't want to access this new entries from C#. It should only be possible to create them.

Is there an other way to achieve this? Could I create a class from LuaTable and insert this to Lua?

lua["NewLuaTable"] = new ClassFromLuaTable;

and than use in Lua

NewLuaTable.abc = "A string"
print(NewLuaTable.abc);

But than, how did I get notifications, that something I want to know is changed in the LuaTable (NewLuaTable.Name is changed)?

Thank you for your help.

kleopatra
  • 51,061
  • 28
  • 99
  • 211
user1829325
  • 108
  • 6

1 Answers1

0

Ok, I found it by myself.

You could extend C# classes from Lua with the functions get_Item() and set_Item(). These both functions are the same as __index and __newindex in Lua metatables. So you could create a Dictionary table in C# and fill it in the set_Item() function. If LuaInterface didn't find an entry in the class, it calls get_Item() to look, if it could get the value on this way. There you could look into your table, if it is a valid key-value-pair.

user1829325
  • 108
  • 6