For any feature requests to either the Skyline Engine or the websites and forums please leave your suggestion here.

Save and Load game data

Save and Load game data

Postby SpiderMack » 25 Sep 2017, 22:36

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 ?
User avatar
SpiderMack
Skyline Expert
 
Posts: 441
Joined: 02 Dec 2016, 09:15
Skill: Concept artist
Skill: 3D Modeller
Skill: Level Designer
Skill: Scripter

Re: Save and Load game data

Postby Shando » 25 Sep 2017, 23:08

Hi Buddy,

There is a JSON api (http://www.chi-ad.com/Skyline/API/Lua/html/classjson.html) that works really well and is pretty easy to use as it works on Lua Tables:

Code: Select all
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


HTH

Shando
Ryzen 7 4800H 16GB GTX1650 Win 11 64
Love, Hope, Strength http://www.lovehopestrength.co.uk
User avatar
Shando
Skyline Moderator
Skyline Moderator
 
Posts: 560
Joined: 06 Mar 2013, 22:35
Location: Moffat Beach, Queensland
Skill: Programmer
Skill: Scripter
Skill: Level Designer

Re: Save and Load game data

Postby SpiderMack » 25 Sep 2017, 23:19

Nice :)
User avatar
SpiderMack
Skyline Expert
 
Posts: 441
Joined: 02 Dec 2016, 09:15
Skill: Concept artist
Skill: 3D Modeller
Skill: Level Designer
Skill: Scripter

Re: Save and Load game data

Postby SolarPortal » 26 Sep 2017, 09:58

Thanks for sharing that @Shando :)

@Spidermack, JSON is extremely quick to read and write, and we also are in the process of building a basic encryption for files to protect save files and the like :)
Skyline Game Engine - Lead Dev.
Please provide as much info as possible when asking for help.


Specs: OS: Win 10 64bit, CPU: Intel i7 4770 3.4ghz x 4 core(8 threads), GPU: Nvidia GTX 1060 6GB, Ram: 16gig DDR3, Windows on 250gb Samsung Evo 860

Twitter: @SolarPortal
Instagram: @SolarPortal
User avatar
SolarPortal
Skyline Founder
Skyline Founder
 
Posts: 3631
Joined: 29 Jul 2012, 15:56
Location: UK
Skill: 3D Modeller
Skill: 2D Artist
Skill: Programmer
Skill: Level Designer


Return to Feature Requests

Who is online

Users browsing this forum: No registered users and 1 guest

cron