object binding for lua

Announcements, support questions, and discussion for the Dialogue System.
Post Reply
diccon
Posts: 27
Joined: Wed Sep 23, 2020 2:20 pm

object binding for lua

Post 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.
Attachments
LuaBinding.7z
(1.3 KiB) Downloaded 35 times
User avatar
Tony Li
Posts: 22051
Joined: Thu Jul 18, 2013 1:27 pm

Re: object binding for lua

Post by Tony Li »

Thanks for sharing!
Post Reply