Vehem's Python Ponderings (was "Off by one" python error)

Vehem

Modmod Monkey
Joined
Nov 22, 2005
Messages
3,219
I'm starting to get to grips with python, but have come up against an odd one. At the moment, my little "can I do it" project is to spawn a special warrior unit on the same square as a player's initial settler onGameStart, grant it a promotion (Combat5 for testing purposes) and rename it (the theory being that I can then create a new unit type to create and appropriate promotions as needed).

All of the above seems to work almost correctly with the following code...

Code:
	def onGameStart(self, argsList):
		'Called at the start of the game'
		if (gc.getGame().getGameTurnYear() == gc.getDefineINT("START_YEAR")):
			for iPlayer in range(gc.getMAX_PLAYERS()):
				player = gc.getPlayer(iPlayer)
				if (player.isAlive() and player.isHuman()):
					popupInfo = CyPopupInfo()
					popupInfo.setButtonPopupType(ButtonPopupTypes.BUTTONPOPUP_PYTHON_SCREEN)
					popupInfo.setText(u"showDawnOfMan")
					popupInfo.addPopup(iPlayer)

				py = PyPlayer(iPlayer)
				for pUnit in py.getUnitList():
					if pUnit.getUnitClassType() == gc.getInfoTypeForString('UNITCLASS_SETTLER'):
						pPlayer = gc.getPlayer(pUnit.getOwner())
						newUnit = pPlayer.initUnit(gc.getInfoTypeForString('UNITCLASS_WARRIOR'), pUnit.getX(), pUnit.getY(), UnitAITypes.NO_UNITAI)
						newUnit.setHasPromotion(gc.getInfoTypeForString('PROMOTION_COMBAT5'), true)
						newUnit.setName("Mr. King")

A unit is spawned, with the correct promotion and name, but the unit is a Taoist Missionary instead of a Warrior. Having checked the XML (which is unedited in my test mod), the missionary is defined immediately before the Warrior ("off by one").

I've removed all files from the mod directory except for the CvEventManager, cleared the cache and tried setting the UNITCLASS to actually be TAOIST_MISSIONARY (this spawns a Confuscian missionary instead). Interestingly, it does correctly identify the Settler unit and spawns in the correct place.

I'm going to slog through the unitclasses between Settler and Warrior to see which one is "off" - but not sure how that will help in the long run. Any suggestions welcomed.
 
Sorted that one - UNITCLASS_WARRIOR != UNIT_WARRIOR. It was looking up the correct int for the unit class, but because of the XML position of India's Fast workers, it doesn't line up properly. I assume the same will be true for other units throughout the file.

However, that brought me to a further pondering - if I were doing this with Workers, and playing as India - how would I go about spawning a FAST_WORKER for India? If I were spawning Riflemen - Redcoats for England?

Short version - is there a function to create a unit using the UNITCLASS, rather than an explicit UNIT?
 
Vehem said:
Short version - is there a function to create a unit using the UNITCLASS, rather than an explicit UNIT?

Shorter version - is there a way of doing it that is "tidier" than this (avoiding the need for 2 extra variables AND the need to have a "function-and-bracket-fest" as an argument for the initUnit function)?

Code:
civInfo = gc.getCivilizationInfo(player.getCivilizationType())
civUnit = civInfo.getCivilizationUnits(gc.getInfoTypeForString('UNITCLASS_WORKER'))
newUnit = pPlayer.initUnit(civUnit, pUnit.getX(), pUnit.getY(), UnitAITypes.NO_UNITAI)
 
Back
Top Bottom