Recently I have been thinking about maintenance events and how the various mods handle them. I wanted to discuss an approach I have used in the past as well and hear what others have to say as well.
Maintenance Events
So in case you aren't sure what I am talking about, what I mean by maintenance events is the events that are run periodically to check the status of various aspects of a mod, modify and update variables used by the mod, etc. These kinds of events are often necessary when a mod aims to add new features to CK2.
How Maintenance Events are Usually Handled
The most common way of handling maintenance events - as far as I know - is to use a character that is always hanging around and fire an on_action pulse event (e.g. on_yearly_pulse). In early modding people often used the catholic pope, as there is always a pope no matter what happens and so he can be relied on to always be available for events. The problem with the pope though, is that when he dies in the middle of an event chain, it can cause many issues with the associated events.
To overcome these issues, many mods started using immortal characters that could not interact with the rest of the world. The Isis character in HIP? is an example of this method of handling maintenance events. The character can't interact with anyone in the game, but acts as an immortal titled character always available for maintenance events.
Using Provinces for Maintenance Events
Now with the discussion of how maintenance events are usually handled covered, I would like to briefly touch on how I have handled maintenance events in the modding I have done.
In the most complex mod that I have made, a large number of additional variables are added to every settlement in the game, and I needed to have a maintenance event to update the values yearly. At first I used the player character and a yearly pulse, but that had a host of issues that I am sure other modders have run into before as well.
I also didn't want to go through the trouble of adding an immortal character, and I wanted the event to fire again after exactly 365 days, so in the end I decided to try something new. On the first launch of the game, I used an on_startup event to then launch a recurring event with a delay of 365 days for province 1. Below is a simple outline of what happens:
On Startup : event mod.1 is fired
mod.1's immediate block is
immediate = {
1 = {
province_event = { id = mod.2 }
}
}
mod.2 recalls itself while also firing the actual maintenance event (mod.3(
immediate = {
ROOT = {
repeat_event = { id = mod.2 days = 365 }
province_event = { id = mod.3 }
}
}
The use of the repeat_event command ensures that the scope chain doesn't grow each time the event is run, and I believe that calling the repeat from mod.2 prevents any local variables created in mod.3 from persisting after each loop (I may be wrong about this though).
I have used this method for quite some time in my mod, and haven't noticed any serious issues as of yet. I've run observe games and played for nearly 300 years with this method being used, and haven't noticed any save game bloat or sudden failure of the maintenance event. As far as I can tell it seems to be working well.
Discussion
So, to my fellow modders, what is your opinion on this? Is the use of a province to call a recurring maintenance event safe? Am I being naive and there are some nuances of the way the CK2 engine works that makes this dangerous and unreliable? Or are there other reasons why the use of immortal characters is better for handling maintenance events?
If there are alternative ways of firing a maintenance event that you know of, please mention them as well. I don't know if this has been debated in the past, but I am interested to hear what others think and what they feel is the best way to handle maintenance events.
Maintenance Events
So in case you aren't sure what I am talking about, what I mean by maintenance events is the events that are run periodically to check the status of various aspects of a mod, modify and update variables used by the mod, etc. These kinds of events are often necessary when a mod aims to add new features to CK2.
How Maintenance Events are Usually Handled
The most common way of handling maintenance events - as far as I know - is to use a character that is always hanging around and fire an on_action pulse event (e.g. on_yearly_pulse). In early modding people often used the catholic pope, as there is always a pope no matter what happens and so he can be relied on to always be available for events. The problem with the pope though, is that when he dies in the middle of an event chain, it can cause many issues with the associated events.
To overcome these issues, many mods started using immortal characters that could not interact with the rest of the world. The Isis character in HIP? is an example of this method of handling maintenance events. The character can't interact with anyone in the game, but acts as an immortal titled character always available for maintenance events.
Using Provinces for Maintenance Events
Now with the discussion of how maintenance events are usually handled covered, I would like to briefly touch on how I have handled maintenance events in the modding I have done.
In the most complex mod that I have made, a large number of additional variables are added to every settlement in the game, and I needed to have a maintenance event to update the values yearly. At first I used the player character and a yearly pulse, but that had a host of issues that I am sure other modders have run into before as well.
I also didn't want to go through the trouble of adding an immortal character, and I wanted the event to fire again after exactly 365 days, so in the end I decided to try something new. On the first launch of the game, I used an on_startup event to then launch a recurring event with a delay of 365 days for province 1. Below is a simple outline of what happens:
On Startup : event mod.1 is fired
mod.1's immediate block is
immediate = {
1 = {
province_event = { id = mod.2 }
}
}
mod.2 recalls itself while also firing the actual maintenance event (mod.3(
immediate = {
ROOT = {
repeat_event = { id = mod.2 days = 365 }
province_event = { id = mod.3 }
}
}
The use of the repeat_event command ensures that the scope chain doesn't grow each time the event is run, and I believe that calling the repeat from mod.2 prevents any local variables created in mod.3 from persisting after each loop (I may be wrong about this though).
I have used this method for quite some time in my mod, and haven't noticed any serious issues as of yet. I've run observe games and played for nearly 300 years with this method being used, and haven't noticed any save game bloat or sudden failure of the maintenance event. As far as I can tell it seems to be working well.
Discussion
So, to my fellow modders, what is your opinion on this? Is the use of a province to call a recurring maintenance event safe? Am I being naive and there are some nuances of the way the CK2 engine works that makes this dangerous and unreliable? Or are there other reasons why the use of immortal characters is better for handling maintenance events?
If there are alternative ways of firing a maintenance event that you know of, please mention them as well. I don't know if this has been debated in the past, but I am interested to hear what others think and what they feel is the best way to handle maintenance events.