Serialize LUA custom table

Announcements, support questions, and discussion for the Dialogue System.
Post Reply
little
Posts: 3
Joined: Sun Jan 24, 2016 4:47 am

Serialize LUA custom table

Post 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.
User avatar
Tony Li
Posts: 22104
Joined: Thu Jul 18, 2013 1:27 pm

Re: Serialize LUA custom table

Post 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.
little
Posts: 3
Joined: Sun Jan 24, 2016 4:47 am

Re: Serialize LUA custom table

Post 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.
User avatar
Tony Li
Posts: 22104
Joined: Thu Jul 18, 2013 1:27 pm

Re: Serialize LUA custom table

Post 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.
Post Reply