Tuesday, November 24, 2015

Real Time Deltas

I am not certain what the best description is with what I want, but I quite liked this title.

Use a Time based on real time (can be UTC or Local). Not based on time since the game started.

Well I need to have my plants grow over hours, or possibly days. I also need quests to respawn daily, weekly, or perhaps even monthly.

What happens is that when you have completed the Daily quest for the first time. I record the time and Date this was completed on. Then I can test this in the NPC's update function against the Real Time NOW. If the right number of hours has passed, then I can once again respawn the Quest on this NPC. The same goes for objects collected in the game respawning.

I had no real idea quite how to do this at first, but research was easy to come by, even if my search terms were not great... apparently.

The Answer:
DateTime dt = new DateTime(2015, 1, 1, 0, 0, 0, 0, DateTimeKind.Local);
DateTime dtNow = DateTime.Now;
TimeSpan result = dtNow.Subtract(dt);
int seconds = Convert.ToInt32(result.TotalSeconds);

As you can see from the answer, it is Actual C# code that I used in Unity within my Very Bunny Haha game. I am very pleased to announce that this code is working fine. I made it into a function and I record the seconds in my Arrays for respawning stuff.

It is very easy to test the Current number of seconds (time now) against the completed and recorded seconds + how many seconds the respawn is supposed to take. Wham, it works

I hope you also get to write something like this in your game.
Da Voodoochief

No comments:

Post a Comment