- Home /
Trigger events on specific time in datetime
My game contains a datetime to represent the in-game date, and it will forward the time automatically. And I have a series of events that will only occurs on a specific date.
Question is what is the best way so that when the time comes it will trigger these events?
Now the best I can think of is setting a date for each of these events, so everytime when the game time is updated, I check if (game time >= event time). But it seems to drain resources quite much because the time is updated very frequently per second. Is there any better way to do this?
Here is my DateManager:
public class DateTimeManager : MonoBehaviour
{
//Singleton
public static DateTimeManager dateManager;
//Get other objects
private GameStatManager gameStatManager;
//Local var
private Text displayTime;
private Text displayDate;
private Image timeScaleImage;
float counter;
int dayCounter;
//Get speed icon
public Sprite slow;
public Sprite medium;
public Sprite fast;
//Output var
public DateTime dateTime;
void Start(){
//Singleton
dateManager = this;
//Get other objects
gameStatManager = GameObject.Find("GameStatManager").GetComponent<GameStatManager>();
displayDate = transform.Find("Date").gameObject.GetComponent<Text>();
displayTime = transform.Find("Time").gameObject.GetComponent<Text>();
timeScaleImage = transform.Find("TimeScale").gameObject.GetComponent<Image>();
//Initialize
dateTime = gameStatManager.GetDateTime();
//Set default
Time.timeScale = 1;
timeScaleImage.sprite = slow;
counter = 0f;
dayCounter = 0;
}
void Update()
{
counter += Time.deltaTime;
if (counter >= 1f / Time.timeScale){
counter -= 1f / Time.timeScale;
//Add 10 muinutes
dateTime = dateTime.AddMinutes(10);
//Add dayCounter
dayCounter++;
if(dayCounter >= 144){
dayCounter = 0;
gameStatManager.AddTotalDay();
}
//Display datetime
displayDate.text = dateTime.ToString("dd MMM, yyyy");
displayTime.text = dateTime.ToString("h:mmtt");
//To GSM
gameStatManager.SetDateTime(dateTime);
}
}
where are you doing the (game time >= event time) part? can you share that part of code? is imposible that an comparison is taking so long
@xxmariofer I have not implemented it yet. But if i have a series of events (like 30 events), then I have to check these events for each datetime update. Is the method that I mentioned practical?
Well, i imagine each event are consecutive right? You can have each event saved in an array, have an index for knowing the current event and just compare the next event in the array
Your answer
Follow this Question
Related Questions
C# DateTime inspector 2 Answers
Help, getting ambiguous reference error 1 Answer
How to save DateTime in a list 1 Answer
Check How Many Minutes Have Passed 1 Answer
How do I return the correct number of days between TimeSpans? 0 Answers