davidlallen
Deity
I have used a similar loop many times. I want to find all units which are within cultural borders to give them a promotion, and take away the promotion from all units which are not within cultural borders. Here is the code:
I have put in "if pUnit.isNone()" already as a defensive measure. But I am getting the following alert, and then a CTD:
The offending line is "iType = pUnit.getUnitCombatType()".
What can be going wrong? This happens very early in the game, not on turn 1, but always within the first 100 turns. Of course I cannot print any statistics about the unit which is failing, since the object seems to be an invalid one. With earlier versions of the same routine, some units were coming up at location -1,-1 or were matching isNone(), both of which indicate invalid data somehow.
Is there some other defensive programming I should do? Or have I overlooked something silly?
Code:
def HomeGround(self):
for iPlay in range(gc.getMAX_CIV_PLAYERS()):
pPlay = gc.getPlayer(iPlay)
if not pPlay.isAlive(): continue
for iUnit in range(pPlay.getNumUnits()):
pUnit = pPlay.getUnit(iUnit)
if pUnit.isNone(): continue # defensive
iType = pUnit.getUnitCombatType()
self.DebugPrint("%d,%d cbt %d" % \
(pUnit.getX(), pUnit.getY(), iType))
I have put in "if pUnit.isNone()" already as a defensive measure. But I am getting the following alert, and then a CTD:
Code:
Traceback (most recent call last):
File "DuneWars", line 150, in onEndGameTurn
File "DuneWars", line 783, in HomeGround
RuntimeError: unidentifiable C++ exception
ERR: Python function onEvent failed, module CvEventInterface
The offending line is "iType = pUnit.getUnitCombatType()".
What can be going wrong? This happens very early in the game, not on turn 1, but always within the first 100 turns. Of course I cannot print any statistics about the unit which is failing, since the object seems to be an invalid one. With earlier versions of the same routine, some units were coming up at location -1,-1 or were matching isNone(), both of which indicate invalid data somehow.
Is there some other defensive programming I should do? Or have I overlooked something silly?