Updating Text Every Second - Efficiency help

Hiya folks.
                I’ve built a game where you have resources (Wood, Stone, Metal)
And you gain resources every second, like a resource-management game.

Now the game has a clock that runs every second, checks resources , adds and re-saves the resources and updates the text.:

local function updateTime() &nbsp;&nbsp;&nbsp;&nbsp;local time = os.date("\*t") &nbsp;&nbsp;&nbsp;&nbsp;local resourceData = ice:loadBox( "resourceData" )--loads saved resource numbers. &nbsp;&nbsp;&nbsp;&nbsp; --Increase the resource numbers. &nbsp;&nbsp;&nbsp;&nbsp;resourceData:increment( "Lumber", resourceData:retrieve("LumberIncome") + basicData:retrieve("BasicLumberIncome")) &nbsp;&nbsp;&nbsp;&nbsp;resourceData:increment( "Stone", resourceData:retrieve("StoneIncome") + basicData:retrieve("BasicStoneIncome")) &nbsp;&nbsp;&nbsp;&nbsp;resourceData:increment( "Gold", resourceData:retrieve("GoldIncome") + basicData:retrieve("BasicGoldIncome")) --Update the clock Text. &nbsp;&nbsp;&nbsp;&nbsp;local hourText = time.hour &nbsp;&nbsp;&nbsp;&nbsp;if (hourText \< 10) then hourText = "0" .. hourText end &nbsp;&nbsp;&nbsp;&nbsp;hourField.text = hourText &nbsp;&nbsp;&nbsp;&nbsp;local minuteText = time.min &nbsp;&nbsp;&nbsp;&nbsp;if (minuteText \< 10) then minuteText = "0" .. minuteText end &nbsp;&nbsp;&nbsp;&nbsp;minuteField.text = minuteText &nbsp;&nbsp;&nbsp;&nbsp; --Update the resource Text. &nbsp;&nbsp;&nbsp;&nbsp;local goldText = math.floor(resourceData:retrieve("Gold")) &nbsp;&nbsp;&nbsp;&nbsp;goldCounter.text = goldText &nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;local lumberText = math.floor(resourceData:retrieve("Lumber")) &nbsp;&nbsp;&nbsp;&nbsp;lumberCounter.text = lumberText &nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;local stoneText = math.floor(resourceData:retrieve("Stone")) &nbsp;&nbsp;&nbsp;&nbsp;stoneCounter.text = stoneText &nbsp;&nbsp;&nbsp;&nbsp; --Save the resource numbers.&nbsp; &nbsp; &nbsp;&nbsp;&nbsp;&nbsp;resourceData:save();&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; end -- Update the clock once per second local clockTimer = timer.performWithDelay( 1000, updateTime, -1 ) &nbsp;

I realise that’s a lot to take it but hopefully it’s simple enough.
The main issue is every second the frames drop from 25 to about 5 for a fraction tiniest jerk. 
(Tested on a SamSung Galaxy SIII Mini )

I understand that loading, adding, saving and updating will takes it toll.

I was wondering if you can think of a way to reduce the load.

My main concerns are the resources that are updated need to be up-to-date for when you make purchases.

  • If I didn’t load the previous data; making a purcahse would lower the number but then it would bump it back up when adding and save it like you never spend any money.
  • if I didn’t update the text, the user wouldn’t know how much they had.
  • if I didn’t save the resources, you wouldn’t be able to buy items because it wouldn’t register you’ve got the money.

So it seems necessary to load, add, save update every second

So any efficiency ideas would be useful.
PS: sorry for the long post.

*EDIT* Two Ideas:

**1) **I might be able to change every minute and *60 the resources added. It might cause other irregularities but I’ll try it.

**2) **I might be able to only update and save on scene changes? Sounds like I’d still need to update every second anyway which would likely still result in the spike.
 
 

Did you try to split the stuff up in different frames?

Like updating the resources after the first 10 frames, the clock after 20 frames and the text after 30 frames?

I haven’t tried that approach, how would I achieve that?
Basically create 3, 1 second timers? (Not sure if that would just increase overhead or not)
And then I still wouldn’t know how to alter it’s frames.

Cheers.

Why are you storing them using ice? I think ice writes a file every time you store a value, store the values in variables and only save them when you need to. 

I used ice because it seems like an easy way of using important variables over multiple scenes.
I considered what you just said and understand your concern.

I’ve taken to making them just variables and it seems to tick up just fine in my resourceTick.lua.
In order to look at them when I purchase something in a different .lua I had to change the variable from:

local lumber = …

to just:

lumber = …

then call them in other .lua files by using: (Couldn’t figure out how to call it while it was a local variable)

resourceTick.lumber

I then choose to save to the iceBox on every scene change. Hopefully this will help ‘safeguard’ battery cutouts and too greater data loss and stuff ya’know.

I’ve read loads about ’ Monster Global Variables’ in Corona, this wont be too bad will it?
I will be using them constantly. The whole game revolves around them.

Thanks for the help so far folks :slight_smile:
 

http://www.coronalabs.com/blog/2013/05/28/tutorial-goodbye-globals/

Thanks cublah.

What’s funny is I seem to be doing this already for Unit and Building information. 
Storing them in .lua files in a table, and calling them.

Although its refreshing to read that tutorial, which helps clarify some stuff. I’ll get doing that with my resource variables.

It seems to be good, as it stand on the device, still a little dip, every second, likely due to the updating of text.
Which is still done every second.
I might swap it so it updates on every scene change instead. And see how that works out 

I’ll keep you updated. Thanks again :slight_smile:

I think the idea here is to have the myData table in memory.  You don’t need to read from disk, update, save to disk every update.  That’s excessive and file output is costly.   You could do:

local myData = require(“mydata”)

myData.BasicLumberIncome = 10

… later on.

myData.LumberIncome = myData.LumberIncome + myData.BasicLumberIncome

lumberCounter.text = myData.LumberIncome

then maybe you could have a time that fires every minute or so that would save your data in case the app crashes (or potentially every update). 

Did you try to split the stuff up in different frames?

Like updating the resources after the first 10 frames, the clock after 20 frames and the text after 30 frames?

I haven’t tried that approach, how would I achieve that?
Basically create 3, 1 second timers? (Not sure if that would just increase overhead or not)
And then I still wouldn’t know how to alter it’s frames.

Cheers.

Why are you storing them using ice? I think ice writes a file every time you store a value, store the values in variables and only save them when you need to. 

I used ice because it seems like an easy way of using important variables over multiple scenes.
I considered what you just said and understand your concern.

I’ve taken to making them just variables and it seems to tick up just fine in my resourceTick.lua.
In order to look at them when I purchase something in a different .lua I had to change the variable from:

local lumber = …

to just:

lumber = …

then call them in other .lua files by using: (Couldn’t figure out how to call it while it was a local variable)

resourceTick.lumber

I then choose to save to the iceBox on every scene change. Hopefully this will help ‘safeguard’ battery cutouts and too greater data loss and stuff ya’know.

I’ve read loads about ’ Monster Global Variables’ in Corona, this wont be too bad will it?
I will be using them constantly. The whole game revolves around them.

Thanks for the help so far folks :slight_smile:
 

http://www.coronalabs.com/blog/2013/05/28/tutorial-goodbye-globals/

Thanks cublah.

What’s funny is I seem to be doing this already for Unit and Building information. 
Storing them in .lua files in a table, and calling them.

Although its refreshing to read that tutorial, which helps clarify some stuff. I’ll get doing that with my resource variables.

It seems to be good, as it stand on the device, still a little dip, every second, likely due to the updating of text.
Which is still done every second.
I might swap it so it updates on every scene change instead. And see how that works out 

I’ll keep you updated. Thanks again :slight_smile:

I think the idea here is to have the myData table in memory.  You don’t need to read from disk, update, save to disk every update.  That’s excessive and file output is costly.   You could do:

local myData = require(“mydata”)

myData.BasicLumberIncome = 10

… later on.

myData.LumberIncome = myData.LumberIncome + myData.BasicLumberIncome

lumberCounter.text = myData.LumberIncome

then maybe you could have a time that fires every minute or so that would save your data in case the app crashes (or potentially every update).