In games, and especially mobile games like Candy Crush or Archero, it’s not uncommon to introduce a mechanic that gates the player to prevent them from running through content too fast. Mechanics like this could for example be:
These mechanics often use the device’s time to check if a certain amount of time has passed. So on a mobile device, that would be the local clock and time zone. A problem with this is that players can just set their time manually instead, circumventing any time-gated systems. This allows players to time cheat to for example get more lives, instant energy refills, or big idle earnings.
Thankfully with LootLocker, preventing this type of cheating is very simple. This can be achieved in 4 steps:
That’s it.
Now, this will not make it completely impossible to time cheat. We’ll store the time on the client's side, so it would be possible for someone to hack the game and change it, but average users will at least not be able to use their device time to do so.
This guide will teach you how to set up a simple lives system using LootLocker using the server's time to check if the user should get a new life or not. For this guide, we will be using Unity Engine 2021.2.12f, but the implementation should work in any recent Unity version.
This implementation is made to be built upon, so if you want to use this implementation for an energy system or offline earning system, or any other type of time-based earning system, there’s not much that needs to change:
To make it into an energy system: Instead of removing a life when a player loses, have your game remove a life whenever the player performs an action, such as starting a level or crafting an item.
To make it into an offline earning system: Use the “timeOffline” variable to calculate how long the player was away and reward them using that variable.
To make it into a cooldown system: Use the “timeOffline” variable to check if it has gone above a certain threshold, if so remove the cooldown.
At the end of this guide, you’ll find the full source code for an example with a lives system, an offline earnings system, and an energy system, as seen in the example gif above. You can also test out our implementation by following the link below:
Now let’s get started.
First, let’s create a LootLocker account and set up the SDK using the links below. These guides will show you how to set up LootLocker with Unity and configure everything so that you’re good to go.
We are using Guest Login for this guide, so make sure you enable Guest Login in Platforms on your LootLocker dashboard.
The great thing about this implementation is that it’s not very complex as it just needs a few things to work:
As you can see the implementation of this mechanic is not complex. You can download the script and the examples that are shown in the gif from the link below.
Note: If you want to make this into an offline system, the only thing that needs to change is to get the time from the device instead of the server. But we don’t want that, since that would make it possible for our players to easily get more lives or energy by adjusting their clock.
I’ll now walk you through the script for the lives system step by step and explain what happens.
We now have all the information from the server that we need to check the status of the gameTime which is what we’ll use to see if the player should get a new life or not.
In Update() we’ll check if a new life should be added every second. There is no need for us to check more often than that
Now we have the correct time while we are playing the game, but what do we do if the player doesn’t quit the game, but instead just puts the app out of focus? If this happens, we just need to update the time again from the server as we did in Start(). Unity has a built-in function to check if the app lost focus or not, which makes it easy for us:
When the app loses or gains focus;
The only thing we have left now is what we do when the player loses a life. Since we don’t count the time when the player’s lives are full (5/5 hearts in our implementation) we don’t know if the server time has been recently checked. Therefore, we need to update the gameTime when removing a life if the player’s lives are full.
So we need to update the gameTime when removing a life if the life amount is at the max:
And with that, we have a working lives system that is a lot harder to cheat than just turning your device clock backward or forwards in time. As I stated at the beginning of the guide, changing the lives system into another type of energy system is simple. Here are some different use cases:
An energy system: Instead of removing a life when the player loses, change it to remove a life when the player does an action; starting a level, performing crafting, or similar.
Offline earning system: Use the “timeOffline” variable to calculate how long the player was away and reward them using that variable.
Cooldown system: Use the “timeOffline” variable to check if it has gone above a certain threshold, if so remove the cooldown.
Now, these are just a few of the implementations that this system can be used for. For example, by using the gameTime variable you could reward the player for staying with your game for a long time or reward your players with Loot Boxes or DLC when they have played for a certain amount of time.
Hopefully, you found this guide quick, simple, and easy. All LootLocker features are quick, simple, and easy to set up and implement.
As usual, if you have any questions, please don't hesitate to get in touch via Twitter or Discord.
If you like these guides, please let me know by tweeting me directly!