clr obj <-> lua tables conversions

Jun 16, 2014 at 1:59 AM
Given this code:
namespace Luatest {

    class A {
        public string Name { get; set; }
    }

    class B {
        public Dictionary<string, A> Names { get; set; }
    }

    class Program {
        static void Main( string[ ] args ) {

            var bObj = new B( );
            bObj.Names = new Dictionary<string, A>( );
            bObj.Names.Add( "first", new A { Name = "Lua" } );

            var script = "l = obj.Names[ \"first\" ]; thisName = l:Name; l:Name = \"Neo Lua\"";

            using ( var lua = new Lua() ) {
                var environment = lua.CreateEnvironment( );
                var ret = environment.DoChunk( script, "lua script", new KeyValuePair<string,object>( "obj", bObj ) ); // define a new function in lua that calls the C# function

                Console.Write( bObj.Names[ "first" ].Name );
            }

        }
    }
}
It gives me an error of not converting to Lua table...

What am I missing?
Jun 16, 2014 at 9:27 AM
Why did you write l:Name? It should l.Name.

The exception is because the colon creates a this-member-set. And they are only allowed on Tables [a:b = c ==> ((LuaTable)a).SetMethod("b", (Delegate)c)].

What did you try to do with:
thisName = l:Name;
l:Name = "";
?

In c-lua thisName = l:Name returns also a parse exception.


But ou find a error in the parser error handling, the message you got is wrong :). That will be corrected in the next release.
Marked as answer by jorgeleo on 6/16/2014 at 5:59 AM
Jun 16, 2014 at 1:59 PM
Interesting... in your example in code project you use the colon ( : ) for calling methods, but the dot ( . ) for accessing properties... I missed that nuance
do (frm, cmd = Forms.Form(), Forms.Button()) -- create a from and button in a using block
    frm.Text = 'Hallo Welt!';
    cmd.Text = 'Click';
    cmd.Left = 16;
    cmd.Top = 16;
    cmd.Click:add( -- add a event the counter part is 'remove'
        function (sender, e) : void
          iClicked = iClicked + 1;
      Forms.MessageBox:Show(frm, clr.System.String:Format('Clicked {0:N0} times!', iClicked), 'Lua', Forms.MessageBoxButtons.OK, Forms.MessageBoxIcon.Information);
        end);
    frm.Controls:Add(cmd);
    Forms.Application:Run(frm);
end;
cool, now I have that part working