Python Code Only Works Second Time Round

Lplate

Esus (Allegedly)
Joined
Feb 8, 2011
Messages
578
Location
Ireland
Hi,

In the spoiler is some of the code from one of my python events.

Spoiler :

for movingBuilding in mobBuilding:
if pOnCity.getNumBuilding(movingBuilding)>0:
pOnCity.setNumRealBuilding((movingBuilding), 0)
pOffCity.setNumRealBuilding((movingBuilding), 1)
if movingBuilding == iSlums:
iRadius = 3
# Moves buildings - on first use some buildings get left behind? Test catapult in code below only appears after first use
newUnit = pPlayer.initUnit(iCata, iOnX, iOnY, UnitAITypes.NO_UNITAI, DirectionTypes.DIRECTION_SOUTH)

iXLow = iOffX - iRadius
iXHigh = iOffX + iRadius + 1
iXLimit = iOffX +iRadius
iYLow = iOffY - iRadius
iYHigh = iOffY + iRadius + 1
iYLimit = iOffY + iRadius

for iiX in range(iXLow, iXHigh, 1):
for iiY in range(iYLow, iYHigh, 1):
if not(iiX == iXLow and iiY == iYLow) and not (iiX == iXLow and iiY == iYLimit) and not (iiX == iXLimit and iiY == iYLow) and not (iiX == iXLimit and iiY == iYLimit):
pPlot = CyMap().plot(iiX,iiY)
iImp = pPlot.getImprovementType()
if iImp == iDormant:
pPlot.setImprovementType(iExp1)



The first time I trigger this event on a particular OffCity, the python seems to stop within the for movingbuilding loop. The second time its triggered, the loop completes as expected and the improvements are changed around OffCity.

Even if I remove all buildings from the OnCity before the event is triggered, the movingbuilding loop seems to fail on the first pass. Any ideas what would cause this failure and allow it to work the second time?

Will the onBuildingBuilt code in CvEventManager be triggered during this event if a wonder is built and if so will this interrupt the for movingbuilding loop? Will the onBuildingBuilt check only be performed after completion of all of the code in the event loop?

thanks,
LPlate
 
Hint: Repost your code with
Code:
 tags and the indentation will show. Because that is what determines flow in Python.
 
Hi,

Here it is with the indentation showing

Spoiler :

Code:
	for movingBuilding in mobBuilding:
		if pOnCity.getNumBuilding(movingBuilding)>0:
			pOnCity.setNumRealBuilding((movingBuilding), 0)
			pOffCity.setNumRealBuilding((movingBuilding), 1)
			if movingBuilding == iSlums:
				iRadius = 3
# Moves buildings - on first use some buildings get left behind?  Test archers in code below only appear after first use
	newUnit = pPlayer.initUnit(iCata, iOnX, iOnY, UnitAITypes.NO_UNITAI, DirectionTypes.DIRECTION_SOUTH)
			
	iXLow = iOffX - iRadius
	iXHigh = iOffX + iRadius + 1
	iXLimit = iOffX +iRadius
	iYLow = iOffY - iRadius
	iYHigh = iOffY + iRadius + 1
	iYLimit = iOffY + iRadius

	for iiX in range(iXLow, iXHigh, 1):
		for iiY in range(iYLow, iYHigh, 1):
			if not(iiX == iXLow and iiY == iYLow) and not (iiX == iXLow and iiY == iYLimit) and not (iiX == iXLimit and iiY == iYLow) and not (iiX == iXLimit and iiY == iYLimit):
				pPlot = CyMap().plot(iiX,iiY)
				iImp = pPlot.getImprovementType()
				if iImp == iDormant:
					pPlot.setImprovementType(iExp1)
 
If it is not executing the unit creation line, then you have a Python exception that is causing the function to fail before it gets to that line (assuming nothing in the code before this is exiting the function).

You have not shown how "mobBuilding" is created. My guess is that it is either invalid in general (undefined) or, if some buildings are being moved, sometimes includes an invalid building ID which causes "pOnCity.getNumBuilding(movingBuilding)" to fail, at which point the function exits.

Enable exception logging (in Civilization4.ini in the My Games\Beyond the Sword folder set LoggingEnabled = 1) and check the Python error log. You can also enable the Python exception pop-ups by setting ShowPythonDebugMsgs = 1, which will give you immediate feedback when a Python error happens.
 
Hi,

That was useful. Tidied up some separate stuff with the popups. The error message I get is, unidentifiable C++ exception on line 197, which is the for movingBuilding in mobBuilding: line. I assume this means that one of the mobile buildings has a typo in it.
 
This bug hunt is over. Turns out I've been mispronouncing and spelling Syliven as Sylvien ever since I started playing FFH.

Thanks for all your help
 
Back
Top Bottom