CTD Help

Kael

Deity
Joined
May 6, 2002
Messages
17,403
Location
Paris, France
Any idea why the following code would cause my computer to CTD sometimes?

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.
 
I was going to suggest it was spawning your unit on the water... but you just said it wasn't. Hmmm... maybe it has issues spawning in cities? Or maybe there is a secret hardcoded limit on the number of barbarian units or something (I know it's unlikely but...)

Does the error log come up with anything?
 
The Great Apple said:
I was going to suggest it was spawning your unit on the water... but you just said it wasn't. Hmmm... maybe it has issues spawning in cities? Or maybe there is a secret hardcoded limit on the number of barbarian units or something (I know it's unlikely but...)

Does the error log come up with anything?

Nothing, and it isn't even the normal debug error that I would typically get, its a crash to the desktop. There is a bit of a pause before the CTD that makes me think the CTD may be caused by loop detection (spinlock) but I can't figure out why that would occur either.
 
Okay, this code seems to work. Forcing the computer only to lay down on another warrior. Maybe it CTD's when it tries to drop the warrior on an animal?

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())
			listUnits = []
			for iUnit in range(bPlayer.getNumUnits()):
				pUnit= bPlayer.getUnit(iUnit)
				if pUnit.getUnitType() == gc.getInfoTypeForString('UNIT_WARRIOR'):
					listUnits.append(iUnit)
			if (len(listUnits) >= 1):
				iRnd = CyGame().getSorenRandNum(len(listUnits), "Bob")
				pUnit = bPlayer.getUnit(listUnits[iRnd])
				newUnit = bPlayer.initUnit(gc.getInfoTypeForString('UNIT_WARRIOR'), pUnit.getX(), pUnit.getY(), UnitAITypes.NO_UNITAI)
 
The Great Apple said:
But surely you would have animals on turn 5, while on turn 75 they would have disappeared?

Actually the opposite, animals haven't spawned yet in turn 5 and turn 75 isnt as far into the game as it sounds, still plenty of black on the map, and animals.
 
Back
Top Bottom