Hi,
I didn't find any Skyline examples about this important game feature, saving and loading game checkpoints or game data.
Is there some Api to load and save to a file game data ? I didn't find any class in Lua ?
Or must we use Lua - File I/O ?
obj = 0
function onInit ( objID )
obj = objID
end
function onKeyDown ( key )
-- | on key one, serialize the table to aJSON file.
if ( key == "1" ) then
sky.lprint ( "#&127,127,127:Saving table to JSON..." )
-- | Create a lua table and fill in some data to test with.
local tempTable = { }
tempTable["playername"] = "Joe"
tempTable["lastName"] = "Bloggs"
tempTable[0] = { ["a"]="Hello", [3]=200}
tempTable[1] = "World"
-- | Next, create a json object based on the table, this contains the correct
-- | metatable for the code to write the JSON data properly.
jsonObj = rapidjson.object ( tempTable )
-- | Finally, dump the new object into a json file.
-- | > The "pretty" at the end is to create more readable lines. Setting false will write into one line.
-- | > For a specific location on a Hard drive e.g. E:/myFile.json
-- | > You can name the extension to whatever you want ... *.tableData, *.player etc...
rapidjson.dump ( jsonObj, "myfile.json", {pretty=true} )
sky.lprint ( "#&127,255,127:Saving table to JSON Complete!" )
end
-- | on key two, load the json data into a table and print it.
if ( key == "2" )then
sky.lprint ( "#&127,127,127:Loading table from JSON..." )
-- | Load the json file back into a table.
testTable = rapidjson.load ( "E:/myfile.json" )
-- | Print the data.
sky.lprint ( "playername = " .. testTable["playername"] )
sky.lprint ( "lastName = " .. testTable["lastName"] )
sky.lprint ( "0 = " .. testTable[tostring ( 0 )]["a"] )
sky.lprint ( "0 = " .. testTable[tostring ( 0 )][tostring ( 3 )] )
sky.lprint ( "1 = ".. testTable[tostring ( 1 )] )
sky.lprint ( "#&127,255,127:Loading table from JSON Complete!" )
end
end
Users browsing this forum: No registered users and 1 guest