Hello all, only recently arrived and have been posting a few answers. Finally my turn to ask a question I can’t find an answer for!
Bit of background, but feel free to skip to the paragraph before the code.
I’m up to the stage in my project where I’m implementing a global clock to run tasks that take time (such as smelting ingots and other crafting tasks) and other daily events, with a current time scale of one IRL second to one minute in game (making it 24 IRL minutes to one in game day, or 1440 seconds).
My first implementation was a Clock ScriptableObject, with a method that gets called repeatedly with InvokeRepeating() by a singleton clock object (currently the time display UI) that increments the minutes in the day by one every one second, which resets to zero when you’ve hit 1440 minutes.
Simple stuff. Anything that needs to access the current time can do so by referencing the SO and I even got a working day/night lighting set-up working with this. Of course, because of the incrementing, shadows slightly jump through the day rather than smoothly gliding. This is particularly noticeable in the early and late hours of the day.
So today I went about smoothing that out. The obvious solution is to just make my minutes a float/double (rather than an int as it currently was) and my clock singleton that can just Time.deltaTime the value.
But the crazy part of me wondered if I could make a clock that didn’t need monobehaviour/singletons? And it’s working in so far as I can set a starting time that will loop around once it passes amount of minutes in a day, with the following monstrous code:
[SerializeField, Range(0, 1439)]
private float startingTime;
[SerializeField, ReadOnly]
private float startTimeRef = 0;
private void OnEnable()
{
startTimeRef = startingTime;
}
[ShowInInspector, ReadOnly]
public double TimeMinutesDouble
{
get
{
if(Application.isPlaying)
{
float currentTime;
if (startTimeRef > Time.timeSinceLevelLoad)
{
currentTime = startTimeRef + Time.timeSinceLevelLoad;
if(currentTime >= minutesInDay)
{
startTimeRef = Time.timeSinceLevelLoad;
}
else
{
return currentTime;
}
}
else if (Time.timeSinceLevelLoad - startingTime > minutesInDay)
{
startTimeRef = Time.timeSinceLevelLoad;
}
currentTime = Time.timeSinceLevelLoad - startTimeRef;
return currentTime;
}
else
{
return 0f;
}
}
}
What I can’t do with this is set the time to a specific time, particularly if that time is less than the present time.
So two questions:
- Might anyone be able to suggest some logic to set the current time
- Or, is this just a silly idea and should I just use monobehaviours + singletons like a normal person?
Thanks in advance and apologies for waffling.