I've just done a search for a tutorial on integrating mod components with BUG, and, actually, there
used to be one, plus extensive HTML documentation, but it all disappeared in the late 2010s. These are the only traces I can find through the Wayback Machine:
Tutorial |
References
I had always assumed that the BUG modders had just not gotten around to documentation beyond the technical comments throughout the scripts, i.e. that they implemented this carefully designed and neatly extensible framework, and failed to educate fellow modders in its use. It does seem that the documentation didn't receive much attention even when it was available. Maybe, even then, the problem was that the modders wishing to add a mod component to a BUG-based modpack were not the ones who originally wrote that component and had hardly any experience with Python themselves.
I can't research and write a new tutorial, but I'll try to demonstrate how this simple mod – WarNPeaceScript – can be integrated with BUG. Based on how it's done for some of the components that come with BUG. Apparently,
needs to be added to Config\init.xml; probably best to add it after the last of the existing
load
elements. Also in the Config folder, a corresponding XML file WarNPeace.xml needs to be added with contents such as:
I assume that many of those metadata attributes like
author
are optional. I think the
module
attribute needs to match the name of our main Python module. And the (empty)
init
element is probably necessary so that the
init
function in our module receives a call when the BUG core gets initialized. Then WarNPeace.py can be placed directly in the BUG-based mod's Python folder, I think, or in any subfolder. To that module, we'll add an
init
function that will register the event handlers of the WarNPeace mod, i.e. the WarNPeace changes in CvEventManager.py get moved into WarNPeace.py (and the modified CvEventManager.py gets discarded):
This chunk of code should go right above the first original function definition, i.e. above
def triggerEvents(iTurn):
. I've adopted the peculiar 7-space indents of the original code because indentation has to be consistent within a Python module. The mod name is contained in one of the paths here. This isn't nice, but it's already the case in the original mod. I've put "MyModPack" in the path, will need to be changed to whatever the BUG-based mod is named.
To elaborate: In the init function,
em
is defined as a local shorthand for the global BUG event manager – just for brevity. Then handlers for the three event types LoadGame, GameStart and BeginGameTurn are added. Since the original WarNPeace code (in CvEventManager.py) did the same thing at game start as upon loading a savegame, I've added a single handler function
loadCustomXML
for both. BeginGameTurn gets its own mini handler
onBeginGameTurn
. Both of my handler functions simply call pre-existing WarNPeace functions. One of those functions loads the custom XML file that specifies which wars are supposed to be declared and ended.
I've copied that file along with its containing "CustomXML" folder into the BUG-based mod.
Lastly, the game text file WarNPeace_GameText.xml can simply be copied to XML\Text. For a test with the WB file that comes with the mod, I had to explicitly assign France and England to AI civs on the Custom Scenario screen. Then, as intended, a DoW happened between those two at the start of turn 2 and peace was signed at the start of turn 5. That's all the testing I've done. I hope I haven't forgotten any step.
Edit: Linking to
this thread for another, very similar example.