- Home /
Help with reward player after 24 hours has passed (save time on exit, read on launch)
Hi all, I am trying to add some kind of daily reward to my players.
What I want to do is when a player plays the game then quits it saves the time on quit.
When the player re launches the game check if 24 hours has passed and if so activate my reward gameobject to reward the player 500 coins.
I currently have this script on a gameobject on my title screen:
using UnityEngine;
using System.Collections;
using System;
public class DailyReward : MonoBehaviour {
DateTime startTime;
TimeSpan currentTime;
//TimeSpan oneDay = new TimeSpan(24, 0, 0);
TimeSpan oneDay = new TimeSpan(0, 2, 0);
public GameObject reward;
void Start()
{
StartTime();
}
void Update()
{
currentTime = DateTime.UtcNow - startTime;
if (currentTime >= oneDay)
{
//set daily reward to active to give player 500 coins
reward.SetActive(true);
StartTime();
}
}
private void StartTime()
{
startTime = DateTime.UtcNow;
}
//Returns a string with currentTime in a 24:00:00 format
private string GetTime(TimeSpan time)
{
TimeSpan countdown = oneDay - time;
return countdown.Hours.ToString() + ":" + countdown.Minutes.ToString()
+ ":" + countdown.Seconds.ToString();
}
}
and a reward gameobject that has a script on to reward the player if its activated.
As you can see I have changed it to 2 minutes just to test for now, I don't need any cheat prevention eg user changing their device time, thats up to them if they want to cheat!
At the moment when I play and quit, then relaunch after 2 minutes have passed nothing happens.
Can anyone please help me?
Answer by Woltus · Jun 17, 2017 at 09:47 AM
First of all don't reset you'r start time at every game start. Now if player starts a game every hour he will never get that prize.
You need to store that time between game sessions. Try to use PlayerPrefs : https://docs.unity3d.com/ScriptReference/PlayerPrefs.html
And a little tip, 22 or 23 hours till reward should be better ;)
I am trying to save the current time to playerprefs
I have made a script on my quit button
using UnityEngine;
using System.Collections;
using System;
public class SaveTime : $$anonymous$$onoBehaviour {
void Start()
{
//currentTime = current system time
currentTime = DateTime.UtcNow;
//Save the current system time to player prefs
PlayerPrefs.SetInt("timesaved", currentTime);
}
}
Then on my daily reward script I am trying to read the saved playerpref saved time
using UnityEngine;
using System.Collections;
using System;
public class DailyRewardNew : $$anonymous$$onoBehaviour {
DateTime startTime;
TimeSpan currentTime;
//amount of time to reward Hrs, $$anonymous$$in, Secs
TimeSpan oneDay = new TimeSpan(23, 0, 0);
//the reward object
public GameObject reward;
void Start()
{
//NEED TO CHEC$$anonymous$$ PLAYER PREFS SET AT GA$$anonymous$$E QUIT AND SET THAT AS STARTTI$$anonymous$$E
//get saved time from last game from playerprefs
startTime = PlayerPrefs.GetInt("timesaved");
//currentTime $$anonymous$$us startTime
currentTime = DateTime.UtcNow - startTime;
//if currentTime is greater than oneDay(currently set at 23hrs)
if (currentTime >= oneDay)
{
//set daily reward to active to give player 500 coins
reward.SetActive(true);
}
}
}
I am getting this error: error CS0029: Cannot implicitly convert type int' to
System.DateTime'
do I need to save the playerpref as something else? eg float?
Answer by FantacyfilmsUS · Jun 21, 2017 at 11:13 AM
Instead of using an integer for your variables, use a string in whatever script in gives you CS0029. Find out where its trying to convert your time into an int and try things like .toString:
http://answers.unity3d.com/questions/201190/timetime-as-int-value-.html
this might be helpful...
Thankyou for your reply, I have tried this below, on my daily reward script:
using UnityEngine;
using System.Collections;
using System;
public class DailyRewardNew : $$anonymous$$onoBehaviour {
DateTime startTime;
TimeSpan currentTime;
//amount of time to reward Hrs, $$anonymous$$in, Secs
TimeSpan oneDay = new TimeSpan(23, 0, 0);
//the reward object
public GameObject reward;
void Start()
{
//NEED TO CHEC$$anonymous$$ PLAYER PREFS SET AT GA$$anonymous$$E QUIT AND SET THAT AS STARTTI$$anonymous$$E
//get saved time from last game from playerprefs
startTime = PlayerPrefs.GetString("timesaved");
//currentTime $$anonymous$$us startTime
currentTime = DateTime.UtcNow - startTime;
//if currentTime is greater than oneDay(currently set at 23hrs)
if (currentTime >= oneDay)
{
//set daily reward to active to give player 500 coins
reward.SetActive(true);
}
}
}
on my savetime script (on quit game):
using UnityEngine;
using System.Collections;
using System;
public class SaveTime : $$anonymous$$onoBehaviour {
void Start()
{
//Save the current system time to player prefs
PlayerPrefs.SetFloat("timesaved", DateTime.UtcNow).ToString("0");
}
}
getting this error: error CS0029: Cannot implicitly convert type string' to
System.DateTime'
what am I doing wrong? Can someone tell me the correct steps to saving and reading DateTime.UtcNow please? it's very confusing!
I've been looking all over trying to suss this out, I came across this video:
I have been trying to work it out to fit my code, I now have this below:
DailyRewardNew script:
using UnityEngine;
using System.Collections;
using System;
public class DailyRewardNew : $$anonymous$$onoBehaviour {
DateTime startTime;
TimeSpan currentTime;
//amount of time to reward Hrs, $$anonymous$$in, Secs
//TimeSpan oneDay = new TimeSpan(23, 0, 0);
//the reward object
public GameObject reward;
void Start()
{
//NEED TO CHEC$$anonymous$$ PLAYER PREFS SET AT GA$$anonymous$$E QUIT AND SET THAT AS STARTTI$$anonymous$$E
startTime = Convert.ToDateTime (PlayerPrefs.GetString("timesaved"));
//currentTime $$anonymous$$us startTime
currentTime = DateTime.Now - startTime;
Debug.Log ("timepassed:" +currentTime);
//if currentTime is greater than oneDay(currently set at 23hrs)
if (currentTime.Hours >= 23)
{
//set daily reward to active to give player 500 coins
reward.SetActive(true);
}
}
}
SaveTime script:
using UnityEngine;
using System.Collections;
using System;
public class SaveTime : $$anonymous$$onoBehaviour {
void Start()
{
//Save the current system time to player prefs
PlayerPrefs.SetString("timesaved",GetStringTime());
Debug.Log ("time" + GetStringTime());
}
string GetStringTime(){
DateTime now = Convert.ToDateTime(DateTime.Now);
return now.$$anonymous$$onth + "/" + now.Day +"/" + now.Year +"/" + now.Hour +":" + now.$$anonymous$$inute +":" + now.Second +":";
}
}
If I manually activate my savetime object I get a date eg 6/24 19:46:10 displayed in console.
If I start my game, therefore running the DailyRewardNew script, I get this error:
FormatException: String was not recognized as a valid DateTime. System.DateTime.Parse (System.String s, IFormatProvider provider, DateTimeStyles styles) (at /Users/builduser/buildslave/mono-runtime-and-classlibs/build/mcs/class/corlib/System/DateTime.cs:924) System.DateTime.Parse (System.String s, IFormatProvider provider) (at /Users/builduser/buildslave/mono-runtime-and-classlibs/build/mcs/class/corlib/System/DateTime.cs:912) System.DateTime.Parse (System.String s) (at /Users/builduser/buildslave/mono-runtime-and-classlibs/build/mcs/class/corlib/System/DateTime.cs:907) System.Convert.ToDateTime (System.String value) (at /Users/builduser/buildslave/mono-runtime-and-classlibs/build/mcs/class/corlib/System/Convert.cs:719) DailyRewardNew.Start () (at Assets/Scripts/cozscripts/DailyRewardNew.cs:21)
I think it's something to do with this https://stackoverflow.com/questions/44031297/string-was-not-recognized-as-a-valid-datetime-exception
but i'm stumped again :( can anyone please help me?
Answer by wtdunity · Jan 13, 2018 at 05:45 AM
public Text txt_timer; public GameObject RewardButton; void Start () { StartCoroutine(CheckTimer()); }
public void GetReward()
{
DateTime CurrentTime = DateTime.Now.AddMinutes(1440);
CurrentTime.SaveDate();
StopAllCoroutines();
StartCoroutine(CheckTimer());
}
public IEnumerator CheckTimer()
{
DateTime SavedTime = DateTimeExtension.GetSavedDate();
TimeSpan RemaningTime = SavedTime.Subtract(DateTime.Now);
if (RemaningTime.TotalMinutes > 0)
{
RewardButton.SetActive(false);
}
else
{
RewardButton.SetActive(true);
txt_timer.text ="Collect Reward!!";
}
while (RemaningTime.TotalMinutes>0)
{
RemaningTime =SavedTime.Subtract( DateTime.Now);
txt_timer.text ="Wait For Reward \n"+ RemaningTime.Hours + ":" + RemaningTime.Minutes + ":" + RemaningTime.Seconds;
yield return new WaitForSeconds(1f);
}
if (RemaningTime.TotalMinutes <= 0)
{
RewardButton.SetActive(true);
txt_timer.text ="Collect Reward!!";
}
Debug.Log("StopCoroutine");
}
}
public static class DateTimeExtension { public static void SaveDate(this DateTime _date,string Key="SavedDate") { string d = Convert.ToString(_date); PlayerPrefs.SetString(Key, d); } public static DateTime GetSavedDate(string key = "SavedDate") { if (PlayerPrefs.HasKey("SavedDate")) { string d = PlayerPrefs.GetString("SavedDate"); return Convert.ToDateTime(d); } else { return DateTime.Now; } }
}
Hey, I have a doubt, like where do call this get reward() function!! I mean on a button or else on some other script!! Thanks....
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Pause game while GUI instructions displayed? 1 Answer
activate other objects inside a different objects script 2 Answers
Can't set Time.timeScale back to 1. 2 Answers