Page 1 of 1

object binding for lua

Posted: Mon Oct 05, 2020 9:05 pm
by diccon
I brought this up as an idea in another thread and spent a bit of time getting it to work so I thought I'd post the results up here. If you want to be able access your objects properties and methods from the lua script in Dialogue System then it might be interesting to you.

You will need to drop the attached file somewhere in your project, and then make this small modification to LuaInterpreter/Lua.cs:

Code: Select all

  
public object asObject
{
	get
        {
        	if (hasReturnValue)
          	{
                 	return ((Language.Lua.LuaUserdata)luaValue).Value;
                }
                return null;
        }
}
public bool isObject { get { return hasReturnValue && luaValue is Language.Lua.LuaUserdata; } }
Then in order to push your object as a table element for an Item then call like this:

Code: Select all

DialogueLua.SetTableField("Item", "itemName", "object", new Language.Lua.LuaUserdata(myObject, Language.Lua.LuaBinding.GetMetaTable()));
Then you can script the object to access fields or methods within Dialogue System like this:

Code: Select all

print[lua(Item["itemName"].object.propertyName)])

or

Code: Select all

lua(Item["itemName"].object.MethodName())]
or if you want to pull it out again from somewhere else in your code:

Code: Select all

 
 Lua.Result res = DialogueLua.GetVariable(itemVarName);
 if (res.isObject)
 {
  	item = res.luaValue.Value as MyItem;
 }
 
Note that this implementation does not include any editor support and is only designed to support my own use case of dynamically pushing data items into the Dialogue System, but I'm sure its not too much extra work to get that going.

Anyway I hope this is interesting to someone out there, any feedback or suggestions just let me know.

Re: object binding for lua

Posted: Mon Oct 05, 2020 9:26 pm
by Tony Li
Thanks for sharing!