Skyline Lua API  Version 1.0
Lua Script Reference for Skyline Game Engine.
json Class Reference

#include <LUA_API_JSON.h>

Public Member Functions

void rapidjson dump (jsonObj,"myfile.json",{pretty=true})
 Save a LUA table to file. More...
 

Public Attributes

 jsonObj = rapidjson.object(myTable)
 rapidjson.object(myTable) More...
 
 myTable = rapidjson.load("E:/myfile.json")
 rapidjson.load loads a file to a LUA table. More...
 

Detailed Description

This area covers json functions. Set or get various json propertie.
Use as json.function()
The JSON library inside skyline is used for reading and writing to files
This is a great library to use for saving and loading files as well as specific
in game information such as levels, inventory etc...

JSON is also very fast at being parsed and written too.

For more information on how these functions can be used please visit the User Manual - http://www.aurasoft-skyline.co.uk

obj = 0;
function onInit(objID)
   obj = objID;
end

function onKeyDown( key )

   -- | on key one, we will serialize the table to file.
   if(key == "1")then
      lprint("#&127,127,127:Saving table to JSON...")
   
      -- | First Lets 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";
      
      -- | Second: 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)
      
      -- | Third; Lets 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}); 
      
      lprint("#&127,255,127:Saving table to JSON Complete!");
   end
   
   -- | on key two, we will load the files data into a table and print the data.
   if(key == "2")then
      lprint("#&127,127,127:Loading table to JSON...")
   
      -- | Load the json file into a table.
      myData = rapidjson.load("E:/myfile.json");
      
      -- | Print the data to the in game screen.
      sprint("playername = "..myData["playername"]);
      sprint("lastName = "..myData["lastName"]);
      sprint("0 = "..myData[tostring(0)]["a"]);
      sprint("0 = "..myData[tostring(0)][tostring(3)]);
      sprint("1 = "..myData[tostring(1)]);
      
      lprint("#&127,255,127:Loading table to JSON Complete!");
   end
end

Member Function Documentation

void rapidjson json::dump ( jsonObj  ,
"myfile.json"  ,
{pretty=true}   
)

Save a LUA table to file.

Parameters
jsonObj: your Json table.
jsonfile: The name of the file written to.
pretty: True or false jsonObj is the stream of data. writes a file to disk and uses a human readable layout {pretty=true}
function saveGameData()
    data = {["highScore"]=math.floor(highScore), ["highDistance"]=math.floor(highDistance)};
    jsonObj = rapidjson.object(data);
    rapidjson.dump(jsonObj, "protoRunnerHighScore.json", {pretty=true}); 
end

Member Data Documentation

json::jsonObj = rapidjson.object(myTable)

rapidjson.object(myTable)

Parameters
myTable: your table Adds a lua table to json ready for writing to disk
function onInit(objID)
    jsonObj = rapidjson.object(myTable)
end
json::myTable = rapidjson.load("E:/myfile.json")

rapidjson.load loads a file to a LUA table.

Parameters
jsonObj: your Json table.
jsonfile: The name of the file written to.
pretty: True or false read back from a file to a table.
function loadGameData()
    data,n = rapidjson.load("protoRunnerHighScore.json");
    if(data==nil)then return; end -- check to see if data is valid or lua may misbehave
    highScore = data["highScore"];
    highDistance = data["highDistance"];
end

The documentation for this class was generated from the following file: