H everyone,
So first, what I’m trying to achieve:
In my game, a grand-strategy inspired by Civ and CK2, time passes as days go buy (appr. 1 sec = 1 day) and each day, certain things can happen, for example people age and characters are doing things.
So in a gameObject called GameManager I handle the general time passing (relevant snippet from the whole script):
public int daysPassed;
int currentDay;
int currentMonth;
int currentYear;
int currentYearDay; // this goes from 1 to 360
float gameSpeed; // This shows the number of secs between days, so the higher, the slower!
public delegate void myDelegate();
public Dictionary<int, myDelegate> yearDayEvents = new Dictionary<int, myDelegate> (); // for yearly recurring events
public Dictionary<int, myDelegate> totalDaysEvents = new Dictionary<int, myDelegate> (); // for only happening once events
IEnumerator TimePassing ()
{
while(true)
{
daysPassed++;
currentDay++;
currentYearDay++;
if (currentDay > 30)
{
currentMonth++;
if (currentMonth > 12)
{
currentYear++;
currentMonth = 1;
currentYearDay = 1;
}
currentDay = 1;
}
yearDayEvents[currentYearDay](); // here fire events that trigger once per year
if (totalDaysEvents.ContainsKey(daysPassed))
{
totalDaysEvents[daysPassed](); // all added events for that day fire here
totalDaysEvents.Remove(daysPassed);
}
timeLabel.text = currentDay.ToString("00") + "." + currentMonth.ToString("00") + "." + currentYear.ToString("0000");
yield return new WaitForSeconds(gameSpeed);
}
}
public void StartTime()
{
gameSpeed = 1f;
daysPassed = 0;
currentYear = startYear;
currentMonth = 1;
currentDay = 1;
currentYearDay = 1;
for (int i = 1; i <= 360; i++)
{
if(!yearDayEvents.ContainsKey(i))
{
yearDayEvents[i] = new myDelegate(FillerFunction);
}
}
StartCoroutine(TimePassing());
}
the relevant object here is the totalDaysEvents which is a dictionary having as a key an integer that is the number of the day since the game started and as value a delegate which SHOULD contain all events that will trigger on that day.
Here is the function from the character.cs script, in which I try to add functions to the delegates:
public void QueueDoSomething ()
{
int daysToNextDoing = 5; // will be replaced by a function
int nextDay = gameManager.daysPassed + daysToNextDoing;
if (gameManager.totalDaysEvents.ContainsKey(nextDay))
{
var myDelegate = gameManager.totalDaysEvents[nextDay];
myDelegate += this.DoSomething;
}
else
{
var myDelegate = new GameManager.myDelegate(this.DoSomething);
gameManager.totalDaysEvents.Add(nextDay, myDelegate);
}
}
public void DoSomething ()
{
if (verbose)
{
Debug.Log("I'm thinking about doing something :-)");
}
QueueDoSomething();
}
So, now to the thing that won’t work: My delegates seem always only to hold one function and never multiple. So I guess I must be doing something wrong when initializing the delegate?
Maybe I do this completely wrong and there is a much easier way to do this? (I’m new to Unity so feel free to give me advice :-))
Thanks in advance, I’m really lost here,
Cheers, Ray