Nlua broken in the newest version and LuaTableWrapper question

Announcements, support questions, and discussion for the Dialogue System.
Post Reply
patchfoot
Posts: 6
Joined: Thu Sep 18, 2014 12:02 pm

Nlua broken in the newest version and LuaTableWrapper question

Post by patchfoot »

I noticed importing the Nlua package and deleting lua interpreter seems to break the lua environment. Is that a known issue?



Also how do you work with LuaTableWrapper  values and how do you check for subtables?  Like how do you get both a list of the array values and all the values assigned to a key? I've been trying to various ways to access info from a LuaTableWrapper and I've confirmed it is valid and with the console I can see the table is populated but I can't manage to pry any data out of it.



Example code



Lua.Run("blah = {55,66,12,3,5,7}");



LuaTableWrapper blah = Lua.Run("return blah").AsTable;



Debug.Log(blah .IsValid + " " + blah .luaTable.Count); // returns true and 0 iterating over values or keys with foreach finds nothing



but return blah[1] in the console shows 55.
User avatar
Tony Li
Posts: 22100
Joined: Thu Jul 18, 2013 1:27 pm

Nlua broken in the newest version and LuaTableWrapper question

Post by Tony Li »

Hi,



What version of Unity are you using? (Version number and Pro/Free please)



I'll post some examples for LuaTableWrapper (link is to the API reference). I'll also include an accurate Count property in the upcoming v1.3.9.
User avatar
Tony Li
Posts: 22100
Joined: Thu Jul 18, 2013 1:27 pm

Nlua broken in the newest version and LuaTableWrapper question

Post by Tony Li »

I tested NLua with Unity 4.3.4f1 and 5.0b13, and it seems to work fine in both versions. Could you please provide details to reproduce the problem?



Regarding LuaTableWrapper, for the upcoming v1.3.9 I improved the API documentation for LuaTableWrapper and added a Count property. You'll be able to work with tables like this:

var result = Lua.Run("return blah");

if (result.IsTable) {

LuaTableWrapper blah = result.AsTable;

Debug.Log("IsValid=" + blah.IsValid + " Count=" + blah.Count);



// Iterate over keys:

foreach (var key in blah.Keys) {

...

}



// Iterate over values:

foreach (var value in blah.Values) {

if (value is LuaTableWrapper) {

// this is a subtable

}

...

}

patchfoot
Posts: 6
Joined: Thu Sep 18, 2014 12:02 pm

Nlua broken in the newest version and LuaTableWrapper question

Post by patchfoot »

I'll check nlua again, but how do you pull the value in the table? Or is the Count required so that foreach runs?



LuaTableWrapper blah = result.AsTable;

foreach (var value in blah.Values) {

Debug.Log(value) // this never runs since it doesn't think there are any values even though I can access them via console

}



The intent is to be able to insert my own tables into the lua environment and pull them out so I can serialize them to xml. So I need to be able to grab all the keys and values (whether just values or sub tables) of my inserted tables.
User avatar
Tony Li
Posts: 22100
Joined: Thu Jul 18, 2013 1:27 pm

Nlua broken in the newest version and LuaTableWrapper question

Post by Tony Li »

The Count property isn't required to use foreach.



The Dialogue System only uses hashes, so LuaTableWrapper was originally designed only for hashes:

blah = { a=55, b=66, c=12 };

print(blah[a])

In version 1.3.9, which will be available tomorrow, it also supports regular, one-dimensional arrays:

blah = { 55, 66, 12 };

print blah[1]

So this is really just a lack of functionality in v1.3.8, nothing you're doing wrong. The expanded API documentation coming in v1.3.9 should help, too.
patchfoot
Posts: 6
Joined: Thu Sep 18, 2014 12:02 pm

Nlua broken in the newest version and LuaTableWrapper question

Post by patchfoot »

Upgraded to the newest version and it's still not quite what I expected but I'm probably doing something wrong.



Lua.Run(@" blah = {5,1}");

var blah = Lua.Run("return blah").AsTable;

foreach (var key in blah.Keys) {

Debug.Log("key " + key);

Debug.Log(blah[key]);

} // prints key 1, NULL, key 2, NULL

foreach (var value in blah.Values) {

Debug.Log("value " + value);

} //value 5, value 1

Lua.Run(@" blah2 = {a = 5, b = 1, 5}");

var blah2 = Lua.Run("return blah2").AsTable;

foreach (var key in blah2.Keys) {

Debug.Log("key " + key);

Debug.Log(blah[key]);

}// key 1, key a, NULL, key b, NULL

foreach (var value in blah2.Values) {

Debug.Log("value " + value);

}// only prints value 5











I wouldn't be able to serialize this either as I don't have complete key/value pairs for everything in a table.
User avatar
Tony Li
Posts: 22100
Joined: Thu Jul 18, 2013 1:27 pm

Nlua broken in the newest version and LuaTableWrapper question

Post by Tony Li »

I think the issue is due to the way LuaInterpreter implements tables. If it's a one-dimensional array, it uses a List<LuaValue>. If it's a hash, it uses Dictionary<LuaValue,LuaValue>. It may be easier to access LuaInterpreter's LuaTable directly through the LuaTableWrapper.luaTable field. LuaTable.ListValues will enumerate the List<LuaValue> values.  LuaTable.KeyValuePairs will enumerate the KeyValuePair<LuaValue,LuaValue> values.



(Sorry about the code formatting issue. I'll look into why bbPress is having a problem with it.)
Post Reply