spawnunit

globosud

Warlord
Joined
Jul 7, 2017
Messages
109
I found another interesting script

which unfortunately is for bug mod, but unfortunately I tried to adapt it, but I think I did something wrong.
I think I need to change something here, I added this part to the beginning of the file, as https://forums.civfanatics.com/members/f1rpo.258413/ advised me in the other request, but I don't think it's good for this

Import BugEventManager

def init():
em = BugEventManager.g_eventManager
em.addEventHandler('LoadGame', loadCustomXML)
em.addEventHandler('GameStart', loadCustomXML)
em.addEventHandler('BeginGameTurn', onBeginGameTurn)

def loadCustomXML(argsList):
loadEvents("Mods/Realism Invictus/Assets/XML/CustomXML/CIV4SpawnUnitInfos.xml")

def onBeginGameTurn(argsList):
iGameTurn = argsList[0]
AddUnits(iGameTurn)
 
Ah, the same author as WarNPeace. Really should work in a manner similar to your previous thread. One problem is probably that there is already a function named "init" in the original code, and Python doesn't support overloaded function names. I suspect that our function invoked during BUG initialization has to be named "init" – and cannot take any parameter – so I would suggest renaming the old init function. If the sModName parameter is removed (or maybe it needs to be replaced with argsList), then that function could actually simply become loadCustomXML. Instead of sModName, we might as well type in the mod's name directly. Or rather, let's do it more nicely and use BugPath.getModName() instead. (That would also be nicer in the WarNPeace mod for BUG.) Then we have, at the start of the former init function (and replacing your loadCustomXML function):
Code:
def loadCustomXML(argsList):    
    MyFile = open("Mods/"+BugPath.getModName()+"/Assets/XML/CustomXML/CIV4SpawnUnitInfos.xml")
    del SpawnUnitList[:] # (as before; etc. etc.)
Plus import BugPath after the other imports near the top of the file. And onBeginGameTurn will need to do what CvEventManager used to do, i.e.
Code:
def onBeginGameTurn(argsList):
    iGameTurn = argsList[0]
    process(iGameTurn+1)
This time 4 spaces are used for indentation. :twitch: Oh, sometimes 4, sometimes 8, so using 8 will work too, will be interpreted as double indentation.

And I suppose you'll need a separate SpawnUnits.xml file in Config. The one you already have for WarNPEace will only call the init function of that module. It's arguably also cleaner to have a separate <mod> definition in XML for each of these small mod components.

Testing this with Earth18Civs as Spain, I do receive extra units on turns 2 and 3 as intended.

Edit: readability
 
Last edited:
Putting that together, my mod component for BUG looks as in the attachment. The XML folder is as in the original mod. There was no need to rename SpawnUnitUtil to SpawnUnits; just seemed a bit strange to name it "Utils" when it's really the heart of the mod. And <load mod="SpawnUnits"/> needs to be added to Config\init.xml.
 

Attachments

  • SpawnUnits.zip
    5.2 KB · Views: 5
Thank you. Without you I would never have succeeded. I had imagined that the double .init function could not work.Very kind
 
Top Bottom