Page 1 of 1

Serialize LUA custom table

Posted: Sun Jan 24, 2016 4:55 am
by little
Hello, I would like to know if there is any build-in support to serialize a custom Lua table? That is to serialize to a string like this

Code: Select all

Inventory = { slot1 = {name="sword", level=1}, slot2 = {name="sword2", level=2}};
So that the result can be loaded directly by the PersistentDataManager.ApplySaveData?

Thank a lot.

Re: Serialize LUA custom table

Posted: Sun Jan 24, 2016 8:56 am
by Tony Li
Hi,

Assign a method to the PersistentDataManager.GetCustomSaveData delegate. See Saving Custom Data for an example with code. If you have any questions about it, just let me know.

Re: Serialize LUA custom table

Posted: Sun Jan 24, 2016 9:25 am
by little
Thanks, I have no question on the use of PersistentDataManager. I know it is easy to write a specific method to serialize the table. But is there a generic method that could serialize the Lua table recursively?

If I use Lua.Run ("return tablename"), it seems only basic type like string and number are returned. Does that mean it is necessary to query recursively if the attribute is a table? Thanks.

Re: Serialize LUA custom table

Posted: Sun Jan 24, 2016 9:54 am
by Tony Li

Code: Select all

Lua.Run("return tablename") 
returns a Lua.Result struct that you can "cast" as any supported data type using the AsXXX properties such as AsBool, AsFloat, and AsTable. The AsTable property returns a LuaTableWrapper. (The wrapper lets the Dialogue System work with different Lua implementations without having to know the specifics of how they implement tables.)

LuaTableWrapper has various properties such as Keys and Values.

Keys is the enumeration of the table keys ("slot1", "slot2", etc.)

Values is the enumeration of the table values. These are standard types: bool, float, string, etc. -- except for sub-tables. If the value is itself a table (such as {name="sword", level=1}), it will return another LuaTableWrapper.