Calculating time passed/reading files?

So I’m new to Lua and programming in general, and in my current project I’m trying to get it to calculate the amount of time that has passed since the app was last closed/suspended. The problem is, I don’t know how to turn the saved (to a .txt) time when it was closed into a number. Any help would be greatly appreciated.

Are you using… 

os.time() 

as the stored data?

Yes.

The easiest solution would be to save data as .json

I would put time into table, open file and write encoded table ( using json.encode() on lua table first )

When reading from file, just use json.decode() on file content and you will get ready lua table :slight_smile:

For example

local json = require("json") local data = {} data.lastTime = os.time() local file = io.open( system.pathForFile("myData.json", system.DocumentsDirectory), "w" ) if file then local contents = json.encode(data) file:write(contents) io.close(file) else --handle error end -----now loading local fileToRead = io.open( system.pathForFile("myData.json", system.DocumentsDirectory), "r" ) local mySavedData = nil if fileToRead then local contents = fileToRead:read("\*a") mySavedData = json.decode(contents); io.close(fileToRead) end

While JSON would certainly work, if you’re just storing the time value then it’s actually just as efficient to write the data directly.

To get the time difference, take a look at http://docs.coronalabs.com/api/library/os/difftime.html

You would be doing something like:

[lua]local elapsed = os.difftime( os.time(), yourSavedTime )[/lua]

You then have the elapsed seconds which you can use in a variety of ways ( See: os.date() )

Hope that helps.

Cheers.

But remember, if you store it in .txt, number will be saved and later read as string. Json just allows you to get rid of converting headache :slight_smile:

Depends if you’re using a mod to handle your writes or doing it directly.  But I agree that the data size is negligible in this case so JSON can work just as well if you’re using a mod.  :slight_smile:

Cheers

Use tonumber(string) to convert a string into a number.

Also, instead of a text file or even JSON, GGData is a superb module for settings.
https://github.com/GlitchGames/GGData

@MochiBits

You are right of course, but I like to have as low overhead as possible :slight_smile: json just do it in the fly (no need to worry what was string, number or even table), but it’s just my personal preference :slight_smile:

Ok, what I’m trying to figure out is how many points to award someone based on how much time has passed since they left the app and how many of a certain object they have displayed. This object creates 10 points every so many seconds, so I want it to be figuring that out in the background. But I’ve spent the last few days trying to figure it out and haven’t yet. Any help?

When you open app, at the beginning you check if there is your file with saved data on device. If not, you assume user opens app first time and you save current time in file. Next time when user opens apps there will be file ob device so you will read it and check what time was stored there. Then you calculate time difference between saved time and current time, and praise user accordingly.

Are you using… 

os.time() 

as the stored data?

Yes.

The easiest solution would be to save data as .json

I would put time into table, open file and write encoded table ( using json.encode() on lua table first )

When reading from file, just use json.decode() on file content and you will get ready lua table :slight_smile:

For example

local json = require("json") local data = {} data.lastTime = os.time() local file = io.open( system.pathForFile("myData.json", system.DocumentsDirectory), "w" ) if file then local contents = json.encode(data) file:write(contents) io.close(file) else --handle error end -----now loading local fileToRead = io.open( system.pathForFile("myData.json", system.DocumentsDirectory), "r" ) local mySavedData = nil if fileToRead then local contents = fileToRead:read("\*a") mySavedData = json.decode(contents); io.close(fileToRead) end

While JSON would certainly work, if you’re just storing the time value then it’s actually just as efficient to write the data directly.

To get the time difference, take a look at http://docs.coronalabs.com/api/library/os/difftime.html

You would be doing something like:

[lua]local elapsed = os.difftime( os.time(), yourSavedTime )[/lua]

You then have the elapsed seconds which you can use in a variety of ways ( See: os.date() )

Hope that helps.

Cheers.

But remember, if you store it in .txt, number will be saved and later read as string. Json just allows you to get rid of converting headache :slight_smile:

Depends if you’re using a mod to handle your writes or doing it directly.  But I agree that the data size is negligible in this case so JSON can work just as well if you’re using a mod.  :slight_smile:

Cheers

Use tonumber(string) to convert a string into a number.

Also, instead of a text file or even JSON, GGData is a superb module for settings.
https://github.com/GlitchGames/GGData

@MochiBits

You are right of course, but I like to have as low overhead as possible :slight_smile: json just do it in the fly (no need to worry what was string, number or even table), but it’s just my personal preference :slight_smile:

Ok, what I’m trying to figure out is how many points to award someone based on how much time has passed since they left the app and how many of a certain object they have displayed. This object creates 10 points every so many seconds, so I want it to be figuring that out in the background. But I’ve spent the last few days trying to figure it out and haven’t yet. Any help?