Forum rules - please read before posting.

Check if real time passed?

Hey,
I just wanted to see if there was a way to check how much time has passed in real time? Basically the player buys something, and they're allowed to buy again after 8 real time hours. Would this be possible? Thanks! :smile:

Comments

  • Presumably they can close/re-open the game in between?

    You'd have to use a custom script that records the current system time in seconds, adds 28800 (the number of seconds in 8 hours), and then sets that to a Global Integer Variable.

    Create two Global Integer variables named BuyAgainTime and CurrentTime.

    Create a new C# script named SetTime.cs and paste in the code below:

    using UnityEngine;
    using System;
    
    public class SetTime : MonoBehaviour
    {
    
        public void OnBuy ()
        {
            AC.GlobalVariables.GetVariable ("BuyAgainTime").IntegerValue = CurrentTime () + 28800;
        }
    
        public void UpdateCurrent ()
        {
            AC.GlobalVariables.GetVariable ("CurrentTime").IntegerValue = CurrentTime ();
        }
    
        private int CurrentTime ()
        {
            DateTime epochStart = new DateTime (1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
            int currentEpochTime = (int)(DateTime.UtcNow - epochStart).TotalSeconds;
            return currentEpochTime;
        }
    
    }
    

    Add it to an empty GameObject, make it a prefab and remove from the scene. You can then use Object: Call event Actions to call the two functions OnBuy and UpdateCurrent within it.

    • When the player buys something, call OnBuy. This will update the BuyAgainTime variable to the time (in seconds) that they can buy again (28800 is 8 hours in seconds).
    • Before you let the player buy something, call UpdateCurrent. This will update CurrentTime with the current time (in seconds), that you can compare with OnBuy using a Variable: Check Action.
  • It works!! Thanks so much Chris you're awesome :) :)

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Welcome to the official forum for Adventure Creator.