Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

How to lock game for fixed time like Cut the rope?

Discussion in 'Scripting' started by djoshi, Jul 21, 2014.

  1. djoshi

    djoshi

    Joined:
    Mar 28, 2014
    Posts:
    182
    Hi,
    How can we lock game for some period if he consumes given lives? Like done in Cut the rope.

    For Example If player consumes 10 lives, he can not play game(Locked) for next 30 minutes. After 30 min he will be given again 10 lives.

    I am developing for android so even if user clears game from "running apps" that timer still runs.(Game remain locked for 30 minutes).
     
  2. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    store a playerpref for the next time and check it in you main game manager scripting.... a lot of games like that have a "work around" where you can just change the time on the phone/device to get another life after all :)
     
  3. smitchell

    smitchell

    Joined:
    Mar 12, 2012
    Posts:
    702
    you can use System.DateTime.Now.Minute, store the start time when the app or level or what ever loads. then do a test to see if the time is half an hour later or something then do your logic with that.

    or just create a custom timer counting up to 30 minutes.

    The System.DateTime.Now.Minute is nice to use but can be problematic if the hour changes or day changes etc
     
  4. djoshi

    djoshi

    Joined:
    Mar 28, 2014
    Posts:
    182
    but what if player clears game from "running apps" list that timer still runs?
     
  5. smitchell

    smitchell

    Joined:
    Mar 12, 2012
    Posts:
    702
    which timer? the custom one or the one using System.DateTime.Now ?
     
  6. djoshi

    djoshi

    Joined:
    Mar 28, 2014
    Posts:
    182
    Custom one..
     
  7. smitchell

    smitchell

    Joined:
    Mar 12, 2012
    Posts:
    702
    I imagine it wouldn't run if the app was closed no.

    You could store what the time was using player prefs in the OnApplicationQuit () function then carry it on when Awake is called? Not sure how nicely that would work out.

    Personally I would just use the System.Date class, I've used it before, It's not to bad to use. Here's an example of how I'd do it:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class TimeCalc : MonoBehaviour {
    5.     void Start () {
    6.         //Calculate Time difference between 13:20 and 15:30
    7.         Debug.Log("Time Difference is : " + CalculateTimeDiff(13, 20, 15, 30) + " Mins");
    8.     }
    9.  
    10.     int CalculateTimeDiff (int hour1, int min1, int hour2, int min2) {
    11.         int newHour = Mathf.Abs(hour1 - hour2);
    12.         int newMin = Mathf.Abs(min1 - min2);
    13.    
    14.         int totalDiff = (newHour * 60) + newMin;
    15.  
    16.         return totalDiff;
    17.     }
    18. }
    19.  
    Sorry for the hideous code; I'll try clean it up later. I'm sure there is MUCH better way to calculate time
     
  8. smitchell

    smitchell

    Joined:
    Mar 12, 2012
    Posts:
    702
    Then if the output from the function was greater than 30 then the user could play again.
     
  9. smitchell

    smitchell

    Joined:
    Mar 12, 2012
    Posts:
    702
    Cleaned it up a little:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. [System.Serializable]
    5. public class TimeObject {
    6.     public int hour;
    7.     public int min;
    8. }
    9.  
    10. public class TimeCalc : MonoBehaviour {
    11.  
    12.     public TimeObject timeStart, timeEnd;
    13.  
    14.     void Start () {
    15.         Debug.Log("Time Difference: " + CalculateTimeDiff(timeStart, timeEnd) + " Minutes");
    16.     }
    17.  
    18.     int CalculateTimeDiff (TimeObject time1, TimeObject time2) {
    19.         return Mathf.Abs(Mathf.Abs(time1.hour - time2.hour)) * 60 + Mathf.Abs(time1.min - time2.min);
    20.     }
    21. }
    22.  
     
  10. djoshi

    djoshi

    Joined:
    Mar 28, 2014
    Posts:
    182
    I have attached this(TimeCalc) script to empty object & from player controller script

    void Update ()
    {

    if(lifeCounter==2)
    {


    timeCalc.enabled=true;

    }

    }

    But I get error like:
    NullReferenceException: Object reference not set to an instance of an object
    PlayerController.Start () (at Assets/Codes/Scripts/PlayerController.cs:76)
     
  11. djoshi

    djoshi

    Joined:
    Mar 28, 2014
    Posts:
    182
    Ok..I got rid of that error..

    I am getting Time Difference Debug msg..from game beginning..

    Actually I can not understand how to implement it.
     
  12. djoshi

    djoshi

    Joined:
    Mar 28, 2014
    Posts:
    182
    Smitchell can you please explain bit more on how to implement this exactly??
     
  13. smitchell

    smitchell

    Joined:
    Mar 12, 2012
    Posts:
    702
    Oh my bad; I forgot to check if you were a beginner.

    you'll probably want to add my code to your own class. paste the CalculateTimeDiff function into your class somewhere.
    And add this at the very top of your script, above your class definition like I did above.
    Code (CSharp):
    1. [System.Serializable]
    2. public class TimeObject {
    3.     public int hour;
    4.     public int min;
    5. }
     
  14. djoshi

    djoshi

    Joined:
    Mar 28, 2014
    Posts:
    182
    Sorry but still cant get through your method. Instead I have tried playerprefs this way but not being able to unlock game.

    How to get this method working please let me know.


    Here is my code written inside PlayerController.cs:

    private int CurrentTime = System.DateTime.Now.Minute;
    private int timeLockFrom;
    private int gameIsLoc_UnLoc;

    void Awake ()
    {
    if(timeLockFrom-CurrentTime==2)
    {
    PlayerPrefs.SetInt ("GameLocked",0);
    }
    }

    void Start ()
    {
    gameIsLoc_UnLoc=PlayerPrefs.GetInt("GameLocked");

    if(gameIsLoc_UnLoc==1)
    {
    gameDataRef.gameLocked=true; //This is Game Locking Splash Screen
    }

    if(gameIsLoc_UnLoc==0)
    {
    gameDataRef.gameLocked=false;
    }
    }

    void Update ()
    {

    if(lifeCounter==2)
    {
    gameDataRef.gameLocked=true;
    timeLockFrom=CurrentTime;
    PlayerPrefs.SetInt ("GameLocked",1);
    }

    }
     
  15. smitchell

    smitchell

    Joined:
    Mar 12, 2012
    Posts:
    702
    Sigh..

    I normally wouldn't just write code for someone but since I started I might aswel finish. you really need to go learn the basics again; it seems like that might be something you're struggling with?

    Here's a implementation using my way (Note I have not tested it, I've just wrote it in the text editor. There maybe errors)

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. [System.Serializable]
    5. public class TimerObject {
    6.     public int Hour;
    7.     public int Min;
    8.  
    9.     public TimerObject(int hour, int min) {
    10.         this.Hour = hour;
    11.         this.Min = min;
    12.     }
    13. }
    14.  
    15. public class PlayerController : MonoBehaviour {
    16.  
    17.     private int timeLockFrom;
    18.     private int gameIsLoc_UnLoc;
    19.  
    20.     public TimerObject timeStart, timeEnd;
    21.  
    22.     private bool isLocked = false;
    23.  
    24.     void Start ()  {
    25.         if(isLocked)
    26.             timeEnd = new TimerObject(System.DateTime.Now.Hour, System.DateTime.Now.Minute); //Set the starting time to the current time.
    27.  
    28.         //Check to see if we can unlock the timer
    29.         if(CalculateTimeDiff(timeStart, timeEnd) > 30)
    30.             isLocked = false;
    31.     }
    32.  
    33.     void Update () {
    34.         if(lifeCounter == 2 && !isLocked) {
    35.             timeStart = new TimerObject(System.DateTime.Now.Hour, System.DateTime.Now.Minute); //Set the starting time to the current time.
    36.             isLocked = true;
    37.         }
    38.     }
    39.  
    40.     int CalculateTimeDiff (TimerObject time1, TimerObject time2) {
    41.         return Mathf.Abs(Mathf.Abs(time1.Hour - time2.Hour)) * 60 + Mathf.Abs(time1.Min - time2.Min);
    42.     }
    43. }
    44.  
     
  16. djoshi

    djoshi

    Joined:
    Mar 28, 2014
    Posts:
    182
    Thanks..Smitchell for your valuable time & efforts!!