Give a bonus to a player every 24 hrs

Good evening,

What is the best way to control a daily bonus?

1 day of play time or just 1 day in total?

@Browdaddy96 ,

The player will get a bonus every 24 hrs. Independently if he played or not. I just want to check if the last time he got a bonus is greater than 24 hrs. I store in disk the date an time of the last given bonus.

Thanks

I’ve got an answer. If result >= 1440 (1440 is 24 hrs in minutes)
then I give the bonus.

I used AddHours just to check. In my script I’ll use the date of last time the bonus was given.

public class Test_Script : MonoBehaviour
{

    private DateTime date1, date2;
    private TimeSpan cycle;
;
    private double result;

    void Start()
    {
        date1 = System.DateTime.Now;
        date2 = System.DateTime.Now.AddHours (24);
        cycle = date2.Subtract (date1);
        result = cycle.TotalMinutes;
        Debug.Log (result);


    }
}

wait but… did you use playerprefs for this? if so then how did you convert from string back to date time… im so lost at that part …

This post is 4 years old, so they probably won’t respond.

Here’s Microsoft’s documentation on how to parse this.

https://docs.microsoft.com/en-us/dotnet/standard/base-types/parsing-datetime

so what i ended up doing was

date1 = DateTime.Parse(PlayerPrefs.GetString("adRewardTime"));
                date2 = DateTime.Parse(PlayerPrefs.GetString("adRewardTime")).AddHours (24);
                cycle = date2.Subtract (date1);
                result = cycle.TotalMinutes;

                if(result >= 1440)
                {
                   // reset ad availability
                }

and i saved my date string to be parsed like this

PlayerPrefs.SetString(“adRewardTime”, System.DateTime.UtcNow.ToString(“yyyy-MM-ddTHH:mm:sszzz”));

but it no work :frowning: result always = 1440

Well, in your code all you did was get the same date twice, add 24 hours to one of them, and then subtracted the two. Of course you’d always get 1440 minutes, that’s 24 hours. What are you trying to do?

lmao i woke up this morning and thats the first thing i thought xD. i have to put DateTime.UtcNow as date1 and then subtract it from my playerpref date plus 24 hours (of when i claimed the reward) and then check if result is <= to 0(if a day has passed) then make the reward available again… right? i think so lol testing now. Thank you so much for posting that parse thing i was so lost… still am but hey im slowly learning lol

edit: OR actually i can just subtract date1(current time) from date2(saved playerpref time from when i last claimed) and check if result is >= 1440 :slight_smile: