I am working on a rough prototype Object Creation Engine that is setup up dynamically by Lua script. Initially, I thought this would be easy. But I soon found out different. While enjoyable overall, it has been a challenge trying to convert C snippets to equivalent C#(based on fairly limited "accurate" sources). Besides, many examples only include what the author considers advanced. Here's why this is so drawn out - I want others to understand and possibly implement my procedures. If I'm on the right track, lemme know...and if I'm wrong, I'll expect explanations :). This is a kind of pseudo code I do:
Given:
// language = C#
// lua = new LuaInterface
// event Object_OnCreate
// event Button_OnClick
// txtArray = Control.TextBox {multiline = true}
Object_OnCreate(sender, e)
{
lua.RegisterObject("event.sender", sender);
lua.RegisterObject("event.evkey", e.Key);
lua.RegisterObject("event.evobj", e.Object);
}
Button_OnClick(sender, e)
{
LuaInterface.LuaTable _LTable = (LuaInterface.LuaTable)lua[path];
string[] arrTable = new string[_LTable.Keys.Count];
int ndx = 0;
foreach (DictionaryEntry element in _LTable)
{
string sVal;
sVal = element.Key.ToString() + " = " + element.Value.ToString() + '\n';
arrTable.SetValue(sVal, ndx++);
}
txtArray.Clear();
foreach (object element in arrArray)
{
txtArray.AppendText(element.ToString());
}
}
// LuaSnippet
event = {}
event.sender = ""
event.evkey = ""
event.evobj = ""
So, the Object is created and fires off a delegate event I wrote. As you can see, I register the object with a Lua table named "event". This was to ensure that my Lua function would receive all the correct information when I call it from C#:
Given:
// language C#
lua.DoString("event:Engine_OnObjectCreated()");
// LuaSnippet
omni = {}
function event:Engine_OnObjectCreated()
sKey = { event.evkey}
omni[sKey] = {}
omni[sKey].Obj = event.evobj
end
With "omni" being a global table, I am assuming that I can add a new Key [sKey] to the "omni" table, and even a new Key ["Obj"] with the value referencing the newly created object. This process has evolved from fruitless searches on finding an example of getting a Lua function to trigger as a direct result of an event. I know there has got to be an easier way to call this On_Event_Function in Lua. Here is the pseudo code equivalent to I'm looking for:
Given:
// language C#
Button.Click += new OnButtonClickEventHandler(
Lua.DoScript("event:Engine_OnObjectCreated()"));
// LuaSnippet
function event:Engine_OnObjectCreated(sender, e)
//c#
{
txtArray.AppendText(e.Key + "= " e.Value + '\n');
}
end
Obviously, the C# inside the LuaSnippet is just my way of capturing a message through Lua calling a C# function. Amazing to me, I was able to trace four things in the text results, therefore tracing the object creation, verifying proper reference, and even the type of object through my C# debugger. In my quest to achieve 1 thing, I believe I have succeeded in achieving two things.
First, I succeeded in my mission of firing an event in Lua with all the necessary parts -- albeit, not the most efficient method, I am sure.
Second, I have a quality testing widget to understand how Lua works and that the table has the correct references. And this, my friends, is what I wanted to share with you all (err, um, y'all for those of us in Texas).
After all that, my question really comes down to "Is there a better way for an event to hook up with a Lua function?"
Later (and anxiously awaiting replies)...
[Edited by - ddboarm on September 11, 2009 4:59:45 PM]