Ok, lets break down the code. Then we can see if you can figure out how to put everything together into your own function(s). Please, do not copy-paste more than single expressions, try to write your own code for this. You'll learn way more and you will end up with something that you understand - that is customized for your needs.
This method takes a string (like "2001:2") and returns a integer date - expressed in months:
Code:
def getStringDate(self, date):
for iLetter in range(len(date)):
if date[iLetter] == ":":
return int(date[:iLetter]) * 12 + max(0, int(date[iLetter + 1:]) - 1)
if iStartYear <= float(date) <= 2020:
return int(float(date) * 12)
The for-in loop (lines 2-5) determines whether or not there is a colon anywhere in the string. In that case the string needs to be parsed into a year and and a month. The return line holds all the math and is also indexing both parts of the string.
The *12 is important, as the game actually counts all dates in months - not years. Thus the year needs to be multiplied by 12. The month portion of the string is - as said - enumerated 1-12. This of course doesn't compute, so we have to subtract 1 (turning it to a 0-11 interval) before adding to the year * 12 value.
The conditional statement (lines 6-7) is a backup in case the string is only a year without any month (and thus no colon - or nothing beyond the colon). The condition for returning the date is that it is valid (within scenario calendar) and the return statement simply multiplies the date with 12 to get the month date.
Since you're not making a scripting tool (like I was) you could well simplify your version of the method/function.
This is where the actual date check is done:
Code:
def checkDate(self, iTurn, iMonth1, iMonth2):
return ( iTurn == 0 and iMonth1 == -36000 == iMonth2
or getDateForTurn(iTurn + 1) >= iMonth1 and getDateForTurn(iTurn) < iMonth2 )
The iTurn value can be fetched with CyGame.getGameTurn() and the iMonth1 and iMonth2 values are the two values making up the date interval. (If only one date is used, then iMonth2 = iMonth1. Again, if you only need single dates, you could simplify things.)
The first or-clause in the return statement basically checks if all the values are valid - the method returns False for invalid values - thus such a Trigger will never fire. So you clearly don't need this, since you're only gonna use valid dates, right?
The second or-clause calls the getDateForTurn() helper function for both dates and checks whether or not current game date is within this interval. With only one date you would use something similar - look what I did with the isDate() function.
Finally, this is the helper that returns the month for the current game turn:
Code:
def getDateForTurn(iTurn):
return getTurnMonthForGame(iTurn, Game.getStartYear(), Game.getCalendar(), Game.getGameSpeedType())
Note that getTurnMonthForGame() is a built-in SDK function not listed in the Python API (because it doesn't belong to any
class). My version is tailored to work with any possible game setup - you on the other hand already know these settings for your mod. So there are opportunities for simplification.
Now, can you make something helpful out of this for your mod?
