This tutorial will describe how to manage consumables which replenish over time, even when the player is out of the game. We will use a coin system to illustrate the process.

APPFormative™ keeps track of the **total seconds lapsed**, since the app was opened for the first time after install.

Apart from making time calculations like these easier, using APPFormative™ to keep track of the users elapsed time, prevents possible user manipulation of the device system time to gain an unfair advantage.

First off, create a new game level attribute which will store the game time value at the start of the current play session.

**game.SessionStartTimer** of type **real**

Use a "Change Attribute" behavior to assign it the current **game.Time** value.

Define a new table as below. In this case, the game starts with a starting budget of 5 coins.

We already have the elapsed seconds since first launch ( **game.AF_SecondsAfterLaunch** ),

and can calculate the elapsed seconds of the current session ( **floor(game.Time - game.SessionStartTimer)** ).

Together these values give us the total number of seconds since the game was first lauched, up to the current point in the play session:

**self.TotalSeconds = game.AF_SecondsAfterLaunch + floor(game.Time - game.SessionStartTimer)**

We can now calculate the total possible coins the player may have gained since first launching the game (here we will assume a 2 coins every 5 minutes gain):

**self.TotalCoins = 2 * floor(self.TotalSeconds/(60*5))**

Recalculate and write this value to the 2nd column of the **T_Coins** table at regular intervals.

Note that this system is flexible and any kind of coins/second ratio can be represented.

Any time coins are used, update the **CoinsSpent** column in the **T_Coins** table. E.g. if it costs 1 coin to play a level, add 1 to the value stored.

Any time coins are accumulated through IAPs or bonuses, update the **CoinsBought** column in the **T_Coins** table. E.g. if the user bought 10 coins, add 10 to the value stored.

Always save the **T_Coins** table after changes to these two columns.

It is now possible to use a **Display Text** behavior to always show the correct number of available coins with the following calculation:

**tableCellValue(game.T_Coins,1,1) + tableCellValue(game.T_Coins,1,2) - tableCellValue(game.T_Coins,1,3) + tableCellValue(game.T_Coins,1,4)**

Copyright © 2013-2018 - All Rights Reserved - WWW.APPFORMATIVE.COM

Template by APPFORMATIVE™ (U1) & OS Templates