So i have gpgs working in my current game and everything is fine i can save data to the server and when i check to make sure it’s there it is… but my problem is reading the data back. I opened the snapshot and then when i try to read the snapshot objects contents with object:read() i get nothing? What format is it in? I’ve tried everything i can think of and it’s not nil so i have no idea where the data is. It should be as simple as
local data = object:read()
Right? Or am I missing something? I even tried decoding it like it was a json.
Before you can call the .read() method, it’s expected that you would have already called the .write() method at some point. The API documentation says that .write() expects a string value. No numbers, no tables, no booleans. Obviously you would like to store things other than strings like a table of data and that’s where the JSON library comes into play.
json.encode( someTable) returns a string that represents that table in a standard format. You can use the json.* library to convert the table to a string and then when reading convert it back to a table:
I am a bit late to this topic. My question is that if I already have a gameInfo.json can I directly use event.snapshot.contents.write( gameInfo.json ) ?
I am using localloadsave= require(" loadsave") by Rob Miracle to save my game data as gameInfo.json.
The same JSON will work. The loadsave functionality hides your need to have access to the JSON file. You give it a table, you get a table. But I suppose you could either
read the saved file back in like load does and instead of converting it to a Lua table, just save the read data in it’s JSON form. JSON is just a big string that represents your table.
Just use json.encode( yourTable ) as the value to GPGS.
Modify your loadsave functions to also write the JSON string to GPGS at the same time it saves it to a local file.
@Rob. Thank you for the insight. I am going with option 2. Can you please have a look at this post (GPGS new plugin implementation basics) regarding the GPGS new plugin. I have implemented option 2 if you could please check whether it is correct or not.
I haven’t tried testing it yet since I am having other issues with login. I have posted the code in another postsince it was not related to this post. If you could please have a look at there it would be a great help to me.
Before you can call the .read() method, it’s expected that you would have already called the .write() method at some point. The API documentation says that .write() expects a string value. No numbers, no tables, no booleans. Obviously you would like to store things other than strings like a table of data and that’s where the JSON library comes into play.
json.encode( someTable) returns a string that represents that table in a standard format. You can use the json.* library to convert the table to a string and then when reading convert it back to a table:
I am a bit late to this topic. My question is that if I already have a gameInfo.json can I directly use event.snapshot.contents.write( gameInfo.json ) ?
I am using localloadsave= require(" loadsave") by Rob Miracle to save my game data as gameInfo.json.
The same JSON will work. The loadsave functionality hides your need to have access to the JSON file. You give it a table, you get a table. But I suppose you could either
read the saved file back in like load does and instead of converting it to a Lua table, just save the read data in it’s JSON form. JSON is just a big string that represents your table.
Just use json.encode( yourTable ) as the value to GPGS.
Modify your loadsave functions to also write the JSON string to GPGS at the same time it saves it to a local file.