Kael
Deity
Any idea why the following code would cause my computer to CTD sometimes?
This code should just drop a barbarian Warrior on a random barbarian unit. If I comment out the last line I never get the crash. If I leave the last line in I get the CTD about 50-75% of the time.
If I control the environment (reduce the turn to the 5th turn when there are no other barbarians, drop a few barbarians in with the Worldbuilder) then the function never crashes and works correctly. If I run the function in a world without barbarian units then it correctly doesn't do anything.
The only reason I can think of as to why it CTD's inconsistently is that it has something to do with the pUnit it selects to drop the warrior on. But I can't figure out what the difference is.
I tried putting a check in to make sure it wasn't picking a naval unit (not that I expected any barbarian naval units) but that didn't help either.
Code:
def onBeginPlayerTurn(self, argsList):
'Called at the beginning of a players turn'
iGameTurn, iPlayer = argsList
pPlayer = gc.getPlayer(iPlayer)
if (iGameTurn == 75 and iPlayer == gc.getBARBARIAN_PLAYER()):
bPlayer = gc.getPlayer(gc.getBARBARIAN_PLAYER())
if (bPlayer.getNumUnits() >= 1):
pUnit = bPlayer.getUnit(CyGame().getSorenRandNum(bPlayer.getNumUnits(), "Bob"))
newUnit = bPlayer.initUnit(gc.getInfoTypeForString('UNIT_WARRIOR'), pUnit.getX(), pUnit.getY(), UnitAITypes.NO_UNITAI)
This code should just drop a barbarian Warrior on a random barbarian unit. If I comment out the last line I never get the crash. If I leave the last line in I get the CTD about 50-75% of the time.
If I control the environment (reduce the turn to the 5th turn when there are no other barbarians, drop a few barbarians in with the Worldbuilder) then the function never crashes and works correctly. If I run the function in a world without barbarian units then it correctly doesn't do anything.
The only reason I can think of as to why it CTD's inconsistently is that it has something to do with the pUnit it selects to drop the warrior on. But I can't figure out what the difference is.
I tried putting a check in to make sure it wasn't picking a naval unit (not that I expected any barbarian naval units) but that didn't help either.