Hello,
Im trying to use the IPredicateEvaluator to add a “shop” dialogue during certain times, using a TimeManager script I wrote outside of this course.
Before diving into how the time manager works, I just wanted to share the predicate I set up, just to check if I understand this correctly.
Parameters in editor:
Evaluate function in TimeManager:
public bool? Evaluate(string predicate, string[] parameters)
{
if (predicate == "ShopTime")
{
if (Hour >= int.Parse(parameters[0]) && Hour <= int.Parse(parameters[1]))
{
return true;
}
return false;
}
return null;
}
Per my understanding, the way this should work is if the “hour” is greater than 8 but less than 10, the dialogue option to open the shopUI should appear. Currently, this dialogue option appears no matter what the in-game time is.
Ive honestly always struggled with this system, and have never really been able to make it work consistently. What am I doing wrong?
for reference, here is the entire timemanager script, but Im not sure if the problem is coming from there:
Summary
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
public class TimeManager : MonoBehaviour, IPredicateEvaluator
{
//Time
public static Action OnMinuteChanged;
public static Action OnHourChanged;
public static Action OnSleep;
public static int Minute { get; private set; }
public static int Hour { get; private set; }
public static int SeasonDate { get; private set; }
public static string CurrentSeason { get; private set; }
public static string CurrentDay { get; private set; }
[SerializeField] float minuteToRealTime = 0.5f;
[SerializeField] int startHour = 6;
private float timer;
//Day and Season
public Season season = 0;
public Day day = 0;
void Start()
{
Minute = 0;
Hour = startHour;
timer = minuteToRealTime;
//day count
SeasonDate = 0;
UpdateDate();
SetDate();
}
void Update()
{
timer -= Time.deltaTime;
if (timer <= 0)
{
Minute++;
OnMinuteChanged?.Invoke();
if(Minute >= 60)
{
Hour++;
Minute = 0;
if (Hour >= 24)
{
Hour = 0;
UpdateDate();
SetDate();
}
OnHourChanged?.Invoke();
}
timer = minuteToRealTime;
}
}
public void UpdateDate()
{
if (SeasonDate < 30)
{
SeasonDate++;
}
else
{
season++;
if (season > Season.Winter)
{
season = Season.Spring;
}
SeasonDate = 1;
}
if (day > Day.Saturday)
{
day = Day.Sunday;
}
else
{
day++;
}
}
public void SetDate()
{
CurrentDay = day.ToString();
CurrentSeason = season.ToString();
}
public void Sleep()
{
UIFade.Instance.FadeToBlack();
if(Hour >=4 && Hour <= 5)
{
Hour += 6; //not a full night sleep
Minute = 0;
}
else
{
Hour = 6;
Minute = 0;
UpdateDate();
SetDate();
PlayerController.Instance.GetComponent<PlayerHealth>().FullHealPlayer();
GetComponentInParent<ManagerResetter>().DailyReset();
}
OnSleep?.Invoke();
UIFade.Instance.FadeToClear();
}
public bool? Evaluate(string predicate, string[] parameters)
{
if (predicate == "ShopTime")
{
if (Hour >= int.Parse(parameters[0]) && Hour <= int.Parse(parameters[1]))
{
return true;
}
return false;
}
return null;
}
}