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