0

I'm coming from more OO languages (Java, C++, C#), so it's a bit hard for me to wrap my head around how lua handles objects.

I have a user object declared as follows in a User.lua file:

local User = {align="neutral", justness=50, sceneData=nil};

I attempt to convert it to JSON to save in a text file like this in the User.lua file

function User:save ()
    print ("Saving game state")
    local data = json.encode(self);
    local file = io.open (path, "w");
    file:write ( data );
    io.close( file );
    file = nil;
end

The function call is:

User:save();

And the program just saves [] in the text file. How do I reference the object in such a way that it saves the table data? (Edit:) I would like to be able to save the table and make it so that I am able to create a new object with the data in the table after the user closes and reopens the app.

Additionally, sceneData is an object of class Scene, will that encode the table data for that Scene object, or will I need to fiddle with that as well?

mtbyrd
  • 1
  • 1
  • that `file = nil` is totally unnecessary :D – DarkWiiPlayer May 14 '20 at 10:00
  • 2
    How are you calling the method? `User.save()` or `User:save()`? – DarkWiiPlayer May 14 '20 at 10:01
  • Not every object you can save in file using `json.encode()` Check out [`json.encode`](https://docs.coronalabs.com/api/library/json/encode.html) and [loadsave.lua](https://github.com/robmiracle/Simple-Table-Load-Save-Functions-for-Corona-SDK) Have a nice day:) – ldurniat Jun 16 '20 at 19:48

0 Answers0