Python Question

Topkapi Palace works, sometimes. Haven't been able to figure out any reason why it doesn't work - it isn't something simple such as having Wonder "x" also and then Topkapi fails.

It's probably related to the fact that the Topkapi code is reusing the iPlayer variable (which was causing your original issues in this post).

Change these lines of code.

Code:
					for iPlayer in range(gc.getMAX_PLAYERS()):
						ppPlayer = gc.getPlayer(iPlayer)

Replace iPlayer with iPlayer2 in both lines and see what happens
 
It's probably related to the fact that the Topkapi code is reusing the iPlayer variable (which was causing your original issues in this post).

Change these lines of code.

Code:
					for iPlayer in range(gc.getMAX_PLAYERS()):
						ppPlayer = gc.getPlayer(iPlayer)

Replace iPlayer with iPlayer2 in both lines and see what happens

Tried, that still doesn't work. After changing just what you suggested, a Python exception popped up (see attached pic). It pointed me to line 819, which was:

Code:
							civ_type = gc.getPlayer(iPlayer).getCivilizationType()

If I replaced (iPlayer) with (iPlayer2), the Python exception no longer occurs, but I still don't receive my vassal's UU. :confused:
 

Attachments

  • error.jpg
    error.jpg
    54.2 KB · Views: 120
That code must be somewhere else. As far as I can tell, all that the posted code is doing is sucking gold from vassals.

Topkapi Palace has three separate entries in the CvEventManager.py file. There is a section that modifies gold as you mentioned, a section that gives a Civ with a vassal a 20% chance of training their vassal's UU, and a third section at the bottom that I'm not sure what is for. This is the section of code that is flaky:

Code:
## Topkapi Palace Start ##

		pCity = argsList[0]
		pUnit = argsList[1]
		pPlayer = gc.getPlayer(pUnit.getOwner())
		iUnitType = pUnit.getUnitType()

		b_BUILDING_TOPKAPI = gc.getInfoTypeForString("BUILDING_TOPKAPI_PALACE")
		obsoleteTech = gc.getBuildingInfo(b_BUILDING_TOPKAPI).getObsoleteTech()
		if ( gc.getTeam(pPlayer.getTeam()).isHasTech(obsoleteTech) == false or obsoleteTech == -1 ):
			topkapi = false
			for iCity in range(pPlayer.getNumCities()):
				if topkapi == false:
					ppCity = pPlayer.getCity(iCity)
					if ppCity.getNumActiveBuilding(b_BUILDING_TOPKAPI) == true:
						topkapi = true
			if ( topkapi == true ):
				iTeam = pPlayer.getTeam()
				pTeam = gc.getTeam(iTeam)
				l_vassalUB = []
				for iPlayer in range(gc.getMAX_PLAYERS()):
					ppPlayer = gc.getPlayer(iPlayer)
					if ( (ppPlayer.isAlive()==true) and (ppPlayer.isBarbarian()==false) ):
						if ( gc.getTeam(ppPlayer.getTeam()).isVassal(iTeam) == true ):
							civ_type = gc.getPlayer(iPlayer).getCivilizationType()
							for iUnit in range(gc.getNumUnitClassInfos()):
								iUniqueUnit = gc.getCivilizationInfo(civ_type).getCivilizationUnits(iUnit);
								iDefaultUnit = gc.getUnitClassInfo(iUnit).getDefaultUnitIndex();
								if (iDefaultUnit > -1 and iUniqueUnit > -1 and iDefaultUnit != iUniqueUnit):
									if ( iUnitType == iDefaultUnit ):
										l_vassalUB.append(iUniqueUnit)
				if ( len(l_vassalUB) >= 1 ):
					self.iVassalUUChance = self.getRandomNumber( 4 )
					if self.iVassalUUChance == 0:
						chance = CyGame().getSorenRandNum(len(l_vassalUB), "Random for UB")
						iX = pUnit.getX()
						iY = pUnit.getY()
						pNewUnit = pPlayer.initUnit( l_vassalUB[chance], iX, iY, UnitAITypes.NO_UNITAI, DirectionTypes.NO_DIRECTION )
						pNewUnit.convert(pUnit)


## Topkapi Palace End ##

I'm positive the error is in the python somewhere, as the XML is so simple that even an ignoramus such as myself understands it. I've attached the entire file in case someone wants to take a peek at it (the one posted before is now out of date).
 
Oops, here is the CvEventManager.py file. :)
 

Attachments

Just as I predicted. Didn't you read this post?

I read it, and I changed what you suggested (lines 396-7). If I change those lines in the second section of Topkapi code (lines 806 and 808) I get Python errors (see attached). I didn't realize until now that the second section had that code also, stupid oversight on my part. :wallbash: I thought perhaps that was in relation to what Tholal suggested, so I tried the suggestions he made again, but that made even more exceptions pop up.

I feel we are getting close to making this right. I really like the Topkapi wonder and would hate to have to remove it. Thanks again for all the help guys! :goodjob:
 

Attachments

  • errormessage.jpg
    errormessage.jpg
    56.3 KB · Views: 54
Post your full Event Manager module and someone will be able to sort this out for you. Package it or save it as a TXT file before uploading.

Also, if you have loggin enabled in the INI file, then you can find all the exceptions in \Documents\Games\Beyond the Sword\Logs\PythonErr.log - no need to post screen dumps.
 
Post your full Event Manager module and someone will be able to sort this out for you. Package it or save it as a TXT file before uploading.

Also, if you have loggin enabled in the INI file, then you can find all the exceptions in \Documents\Games\Beyond the Sword\Logs\PythonErr.log - no need to post screen dumps.

Fast response! :) Here is the file.
 

Attachments

This should clear that exception right up. (Edit marked with red.)
Code:
def onUnitBuilt(self, argsList):
		'Unit Completed'
		city = argsList[0]
		unit = argsList[1]
		player = PyPlayer(city.getOwner())
## Topkapi Palace Start ##

		pCity = argsList[0]
		pUnit = argsList[1]
		pPlayer = gc.getPlayer(pUnit.getOwner())
		iUnitType = pUnit.getUnitType()

		b_BUILDING_TOPKAPI = gc.getInfoTypeForString("BUILDING_TOPKAPI_PALACE")
		obsoleteTech = gc.getBuildingInfo(b_BUILDING_TOPKAPI).getObsoleteTech()
		if ( gc.getTeam(pPlayer.getTeam()).isHasTech(obsoleteTech) == false or obsoleteTech == -1 ):
			topkapi = false
			for city in [COLOR="Red"][B]player[/B][/COLOR].getCityList():
I'm not at home so I can't try anything out myself, but please post you entire Python Error Log if you get any more exceptions.
 
This should clear that exception right up. (Edit marked with red.)
I'm not at home so I can't try anything out myself, but please post you entire Python Error Log if you get any more exceptions.

That did clear up the exception, thank you! However, Topkapi Palace is still picky about when it wants to give me a vassal's UU. :(

While fooling around in WB testing stuff out, I did notice something else though.. the Seafaring Trait (marked as SEA TRAIT in CvEventManager) causes Python exceptions, but only when starting a game on an era later than Ancient. :confused: I am 100% sure I added the code as tsentom1 intended, so I guess there is an error somewhere in the seafaring code - I am also using three other traits he created with no problems. I tend to doubt it, but the problems with the Seafaring trait wouldn't be interfering with Topkapi, right?

BTW - in the limited free time I've had lately, I have started reading some Python tutorials. I've worked in tech support and I know how annoying it is to have people like me constantly pester you. ;) I really do hate asking for so much, at this point I really want to just get my mod released before my real life completely devours all of my free time. :lol:

Python error log:
Code:
Traceback (most recent call last):

  File "CvEventInterface", line 23, in onEvent

  File "CvEventManager", line 187, in handleEvent

  File "CvEventManager", line 362, in onGameStart

UnboundLocalError: local variable 'iPlayer' referenced before assignment
ERR: Python function onEvent failed, module CvEventInterface
 
You do know that it's a 20% chance, right?

Yes, of course I know that. When I go into World Builder and build two dozen units and none of them are my vassal's UU, I think that covers 20% odds. The problem is that some games it will work perfectly, and some games it doesn't work at all - no middle ground. You're more than welcome to try it out for yourself if you don't believe me, I posted a link to the beta version of my mod.
 
Python error log:
Code:
Traceback (most recent call last):

  File "CvEventInterface", line 23, in onEvent

  File "CvEventManager", line 187, in handleEvent

  File "CvEventManager", line 362, in onGameStart

UnboundLocalError: local variable 'iPlayer' referenced before assignment
ERR: Python function onEvent failed, module CvEventInterface

That error does not affect the topkapi palace, but screws the seafaring trait.
Change:
PHP:
pTeam.setHasTech(iTechSailing, true, iPlayer, false, false)

to:
PHP:
pTeam.setHasTech(iTechSailing, true, iPlayerLoop, false, false)


Can't see anything for the topkapi :/.
 
I would like to start fresh and rewrite the whole Topkapi script for you, but unfortunately that is not an option for me right now. You could however make this change (line #829) and see whether or not the wonder is doing what it is supposed to 100% of the time...
Code:
					if [COLOR="Red"][B]true[/B][/COLOR]:
If it doesn't fire every single time (with all prerequisite conditions met), then you know that the script isn't working.
 
That error does not affect the topkapi palace, but screws the seafaring trait.

Worked like a charm. Thanks again!
 
I would like to start fresh and rewrite the whole Topkapi script for you, but unfortunately that is not an option for me right now.

That would be incredibly generous of you, and believe me, I understand about not having time. If at some point you want to do that, I would be very grateful.

I do have one more (I promise this is the last! ;)) Python question however. Lines 1181-1254 of the CvEventManager.py file contain my attempt to use both Tech by Conquest and Flavian Amphitheater in the same Mod. Both of them work just fine with just one installed, they just don't play nice together. With the way I have the code written now, Tech by Conquest still works but the Flavian Amphitheater does not. No Python errors occur. So I'm guessing a minor rewrite of the code needs to be done?

I looked at the code myself, and recruited a friend who is fairly skilled at Python, but neither of us could come up with a solution... we just can't find any obvious reasons why the two won't work together in the same Mod.

MINOR GRIPE - A message saying "The vanquished have no useful scientific knowledge" pops up on the screen when you capture an enemy city but they have no technologies you don't already have - which is exactly what it is supposed to do. However, the same message also shows up when another civ captures a city - can this be fixed as well?

Many, many thanks to everyone in advance!
 
Yeah, it makes no sense. This needs to be debugged.

About the gripe, this should do it:
Code:
				[COLOR="Red"]elif gc.getGame().getActivePlayer() == iNewOwner[/COLOR]:
					CyInterface().addImmediateMessage("The vanquished have no useful scientific knowledge.", "")
(You replace the "else:" line.)
 
Yeah, it makes no sense. This needs to be debugged.

Cool, what can I do to help get this resolved?

Here is the code for easy viewing:

Code:
	def onCityAcquired(self, argsList):
		'City Acquired'
		iPreviousOwner,iNewOwner,pCity,bConquest,bTrade = argsList
# Tech By Conquest Mod
		# The Loser
		pPlayer1 = gc.getPlayer(iPreviousOwner)
		pTeam1 = gc.getTeam(pPlayer1.getTeam())
		# The Winner
		pPlayer2 = gc.getPlayer(iNewOwner)
		pTeam2 = gc.getTeam(pPlayer2.getTeam())
		FoundTransferTech = False
		
		if bConquest:
			if not pPlayer1.isNone() and not pPlayer1.isBarbarian():
			
				for iTech in range(gc.getNumTechInfos()):
					# Does Player1 have a Tech that Player2 does not have?
					if pTeam1.isHasTech(iTech):
						if not pTeam2.isHasTech(iTech):
							iTechToTransfer = iTech
							FoundTransferTech = True
							break
							
				if FoundTransferTech:
					TechDescription = PyInfo.TechnologyInfo(iTech).getDescription()
					CyInterface().addImmediateMessage("Your scientists have discovered the knowledge of " + str(TechDescription), "")
					pTeam2.setHasTech(iTech,True,iNewOwner,False,False)
					
					# Show tech splash 
					if (iNewOwner > -1 and not CyInterface().noTechSplash()):
						if (gc.getGame().isFinalInitialized() and not gc.getGame().GetWorldBuilderMode()):
							if ((not gc.getGame().isNetworkMultiPlayer()) and (iNewOwner == gc.getGame().getActivePlayer())):
								popupInfo = CyPopupInfo()
								popupInfo.setButtonPopupType(ButtonPopupTypes.BUTTONPOPUP_PYTHON_SCREEN)
								popupInfo.setData1(iTech)
								popupInfo.setText(u"showTechSplash")
								popupInfo.addPopup(iNewOwner)
					
				elif gc.getGame().getActivePlayer() == iNewOwner:
					CyInterface().addImmediateMessage("The vanquished have no useful scientific knowledge.", "")
# End Tech By Conquest Mod

		CvUtil.pyPrint('City Acquired Event: %s' %(pCity.getName()))

# Flavian Start #

		pNewOwner = gc.getPlayer(iNewOwner)
		b_BUILDING_FLAVIAN = gc.getInfoTypeForString("BUILDING_FLAVIAN")
		obsoleteTech = gc.getBuildingInfo(b_BUILDING_FLAVIAN).getObsoleteTech()
		if ( gc.getTeam(pNewOwner.getTeam()).isHasTech(obsoleteTech) == false or obsoleteTech == -1 ):

			flavian = false
			for iCity in range(pNewOwner.getNumCities()):
				if flavian == false:
					ppCity = pNewOwner.getCity(iCity)
					if ( ppCity.getNumActiveBuilding(b_BUILDING_FLAVIAN)==true ):
						flavian = true

			if flavian == true:
				civ_type = gc.getPlayer(iPreviousOwner).getCivilizationType()
				for iBuilding in range(gc.getNumBuildingClassInfos()):
					iUniqueBuilding = gc.getCivilizationInfo(civ_type).getCivilizationBuildings(iBuilding);
					iDefaultBuilding = gc.getBuildingClassInfo(iBuilding).getDefaultBuildingIndex();
					if (iDefaultBuilding > -1 and iUniqueBuilding > -1 and iDefaultBuilding != iUniqueBuilding):

						if ( pCity.getNumActiveBuilding(iDefaultBuilding)==true ):

							pCity.setNumRealBuilding(iDefaultBuilding,0)
							pCity.setNumRealBuilding(iUniqueBuilding,1)

							CyInterface().addMessage(iNewOwner,false,15,CyTranslator().getText("TXT_KEY_BUILDING_FLAVIAN_GAMETXT",( pCity.getName(), PyHelpers.PyInfo.BuildingInfo(iUniqueBuilding).getDescription() , PyHelpers.PyInfo.BuildingInfo(iDefaultBuilding).getDescription() )),'',0,gc.getBuildingInfo(iUniqueBuilding).getButton(),ColorTypes(11),pCity.getX(),pCity.getY(),True,True)


## Flavian End ##
 
I was thinking that some helpful community member with some insight would be able to help you out. Because I'm not even sure whether or not you know about the Python Debug Log?
 
Back
Top Bottom