object binding for lua
Posted: Mon Oct 05, 2020 9:05 pm
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:
Then in order to push your object as a table element for an Item then call like this:
Then you can script the object to access fields or methods within Dialogue System like this:
or
or if you want to pull it out again from somewhere else in your code:
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.
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; } }
Code: Select all
DialogueLua.SetTableField("Item", "itemName", "object", new Language.Lua.LuaUserdata(myObject, Language.Lua.LuaBinding.GetMetaTable()));
Code: Select all
print[lua(Item["itemName"].object.propertyName)])
or
Code: Select all
lua(Item["itemName"].object.MethodName())]
Code: Select all
Lua.Result res = DialogueLua.GetVariable(itemVarName);
if (res.isObject)
{
item = res.luaValue.Value as MyItem;
}
Anyway I hope this is interesting to someone out there, any feedback or suggestions just let me know.