- Home /
How can i make a life system like candy crush?
Hello every one, i'm a french developper so forgive my english please! I'm working on an app for android in unity and i need help with my code. I want to make a life system like candy crush, a system that give you a pack of life after 12 hours in real life but sometimes my code works and sometimes not and i don't know why. There is my code (i give life every 3 minutes in this code just for testing):
using UnityEngine; using System.Collections; using System;
public class GameHelperScript : MonoBehaviour {
public int Playerlife;
DateTime oldDate;
DateTime currentDate;
int hours;
void Start()
{
if (PlayerPrefs.GetInt("FirstStart") == 0)
{
PlayerPrefs.SetInt("FirstStart", 1);
PlayerPrefs.SetInt("PlayerLife", 5);
PlayerPrefs.SetString("sysString", System.DateTime.Now.ToString());
}
Playerlife = PlayerPrefs.GetInt("PlayerLife");
}
void Update ()
{
DateTime.TryParse (PlayerPrefs.GetString ("sysString"), out oldDate);
currentDate = System.DateTime.Now;
hours = currentDate.Minute - oldDate.Minute;
if (hours >= 3)
{
PlayerPrefs.SetInt("PlayerLife", 5);
Playerlife = PlayerPrefs.GetInt("PlayerLife");
PlayerPrefs.SetString("sysString", System.DateTime.Now.ToString());
}
}
}
Answer by NoseKills · May 23, 2014 at 11:54 PM
Your problem probably is here
hours = currentDate.Minute - oldDate.Minute;
DateTime.Minute means "minutes in this hour". So if the timestamp "sysString" was saved at 12:59 and player continued at 13:03, hours would be -56.
You need to use TimeSpan class to keep track and compare passed time periods. Something like this (just to give you an idea, not directly applicable to your game):
using UnityEngine;
using System;
public class test : MonoBehaviour
{
private const int MAX_LIVES = 5;
// 5 seconds for testing
private static TimeSpan newLifeInterval = new TimeSpan(0,0,5);
private DateTime lostLifeTimeStamp;
private int livesLeft = MAX_LIVES;
private int amountOfIntervalsPassed;
// Update is called once per frame
void Update ()
{
if (livesLeft < MAX_LIVES)
{
TimeSpan t = DateTime.Now - lostLifeTimeStamp;
// in theory if you wait for many many years, amountOfIntervalsPassed might be bigger than fits in an int
try
{
// round down or we get a new life whenever over half of interval has passed
double intervalD = System.Math.Floor(t.TotalSeconds / newLifeInterval.TotalSeconds);
amountOfIntervalsPassed = Convert.ToInt32(intervalD);
}
catch (OverflowException)
{
// something has probably gone wrong. give full lives. normalize the situation
livesleft = MAX_LIVES;
}
if (amountOfIntervalsPassed + livesLeft >= MAX_LIVES)
{
livesLeft = MAX_LIVES;
}
Debug.Log("it's been " + t + " since lives dropped from full (now "+livesLeft+"). " + amountOfIntervalsPassed + " reloads passed. Lives Left: " + getAmountOfLives() );
}
}
[ContextMenu ("Lose Life")]
void lostLife()
{
if (livesLeft-- == MAX_LIVES)
{
// mark the timestamp only when lives drop from MAX to MAX -1
lostLifeTimeStamp = DateTime.Now;
///////////////////////////////////////////////////////////////////////////////////
// SAVE livesLeft AND lostLifeTimeStamp HERE AND RESTORE WHEN STARTING THE GAME ///
///////////////////////////////////////////////////////////////////////////////////
}
}
int getAmountOfLives()
{
return Mathf.Min(livesLeft + amountOfIntervalsPassed, MAX_LIVES);
}
}
If you add the script to a GameObject and run the Scene, you can test losing a life by right clicking on the script in the Inspector view.
If this answer was correct, please tick it as answered by clicking the checkmark under the thumbs next to the answer.
$$anonymous$$ay you send me this full scripts please thanx in advance i am new in field of Unity
There is no "full scripts". This is a made up example i wrote to answer this question.
can anyone assure about correctness of this script? Also I am interested in making a timer as well.
You can assure it works yourself. Use it, try it, see if it works. if it doesnt modify it to make it work. If you don't know how to code to change it, then it's probably time to learn, or use a prebuilt system like one exa games has on the asset store.
Could someone please help me. I am new to unity3d. I implemented this code Nose$$anonymous$$ills. After I run out of lives, the lives I want to return one by one. but the lives progresibamente not return but return all together after having done all the time.
my goal is that every ten $$anonymous$$utes the player get a life. and the count is at COUNTDOWN. This is the modification of code:
using UnityEngine; using System.Collections; using UnityEngine.UI; using System;
public class SystemLifes : $$anonymous$$onoBehaviour { private const int Life$$anonymous$$ax = 5; public int LifeCount = Life$$anonymous$$ax; //for reasons of testing only add 1 second. private static TimeSpan newLifeTime = new TimeSpan (0,0,1);
private DateTime lostLifeTimeStamp;
private int TimeInterval; //Each time you pass an interval you should get a life.
public Text chronometerLives;
public Text Lifes; //var numeric counter lives
void Update () {
Lifes.text = LifeCount.ToString(); // numeric counter lives.
//This code is only for testing. to increase or decrease lives.
if (Input.Get$$anonymous$$eyDown ($$anonymous$$eyCode.DownArrow)) {
lostLife();
}if (Input.Get$$anonymous$$eyDown ($$anonymous$$eyCode.UpArrow)) {
GetLife();
}
if (LifeCount < Life$$anonymous$$ax){
TimeSpan t = DateTime.Now - lostLifeTimeStamp;
// in theory if you wait for many many years, amountOfIntervalsPassed might be bigger than fits in an int
try {
// round down or we get a new life whenever over half of interval has passed
double intervalD = System.$$anonymous$$ath.Floor(t.TotalSeconds / newLifeTime.TotalSeconds);
TimeInterval = Convert.ToInt32(intervalD);
}
catch (OverflowException) {
// something has probably gone wrong. give full lives. normalize the situation
LifeCount = Life$$anonymous$$ax;
}
if (TimeInterval + LifeCount >= Life$$anonymous$$ax){
LifeCount = Life$$anonymous$$ax;
}
chronometerLives.text = t.ToString ();
Debug.Log( "Has been " + t + " the time until complete lives. (lives today "+LifeCount+")
. " + TimeInterval + " recharged lives. lives: " + getAmountOfLives() );
}
}
public void GetLife(){
LifeCount++;
}
[Context$$anonymous$$enu ("Perder Vida")]
void lostLife() {
if (LifeCount-- == Life$$anonymous$$ax){
// mark the timestamp only when lives drop from $$anonymous$$AX to $$anonymous$$AX -1
lostLifeTimeStamp = DateTime.Now;
// SAVE livesLeft AND lostLifeTimeStamp HERE AND RESTORE WHEN STARTING THE GA$$anonymous$$$$anonymous$$
}
}
int getAmountOfLives(){
return $$anonymous$$athf.$$anonymous$$in(LifeCount + TimeInterval, Life$$anonymous$$ax);
}
}
//This image is the result of this code. https://www.dropbox.com/s/iyt4p47b33462rn/LifeSystem.gif?dl=0