OK - what's wrong with this machine?

Flying Pig

Utrinque Paratus
Retired Moderator
Joined
Jan 24, 2009
Messages
15,647
Location
Perfidious Albion
OK, here's the situation. I put this in my CvEventManager.py file at the bottom. The minute I do, none of my python works. All the XML references exist, I've checked that - what objection does my game have to this code?

Code:
# Units

iBritInf = gc.getInfoTypeForString("UNIT_BRITINF")
iSAS = gc.getInfoTypeForString("UNIT_SAS")
iAAC = gc.getInfoTypeForString("UNIT_AAC")
iBritArmour = gc.getInfoTypeForString("UNIT_SCIMITAR")

## Civs

iBritish = gc.getInfoTypeForString("CIVILIZATION_ENGLAND")
iArgentina = gc.getInfoTypeForString("CIVILIZATION_ARGENTINA")

## Cities

tStanley = (106, 38)
tTeal = (99, 45)
tPortHoward = (59, 44)
tGooseGreen = (71, 34)
tFitzroy = (92, 33)
tPebbleIsland = (53, 58)

## Objectives
tObjectivesList =    [    tStanley
                tTeal,
                tFitzroy,
                tGooseGreen,
                tPortHoward]

Can anyone help?
 
Here's a further twist. I just added the civilizations part and got the same error. Ditto for units. Does the game just not like me having these lists? I've seen them in other mods.
 
It is very difficult to debug python errors if you have disabled the routine to display errors. Please turn on python alerts and also look into PythonErr.log. Visually, it appears you are missing a comma at the end of the "tStanley" line near the bottom. But, you should not need to rely on civfanatics posters in order to find this kind of typo.
 
If you are adding these to the python file "naked" (not inside a class) then the problem is probably that you are calling gc.getInfoTypeForString("UNIT_BRITINF") before the XML has been loaded.

When the game starts Python is initialized before the XML is loaded. All code that is not part of a class is executed when the Python file is loaded. Therefore nothing that is from the XML is defined yet when this stuff is run. You are trying to look up things that don't exist yet.

Relatively Famous Example: This is why the various loop sizes at the end of the Final Frontier CvSolarSystem.py are hardcoded values. Since the XML hasn't been loaded yet at the time that code is run there is no way to check how many buildings and such are actually defined in the XML.

A solution is to add this stuff to a new function in the class and call that function from both onGameStart and onLoadGame. Or something like that.
 
It is very difficult to debug python errors if you have disabled the routine to display errors. Please turn on python alerts and also look into PythonErr.log. Visually, it appears you are missing a comma at the end of the "tStanley" line near the bottom. But, you should not need to rely on civfanatics posters in order to find this kind of typo.

That comma was, I'm afraid, my copying - it's there in the original

Sorry about this: as you people probably know I'm very new to Python, please be a little gentle with me - just say everything slowly and in words of two syllables or less and I'll probably get it. Can you go through how to turn on python alerts?

Also I pressed Ctrl-F for PythonErr.log and nothing came up. Does that need to have these alerts on as well?

When the game starts Python is initialized before the XML is loaded. All code that is not part of a class is executed when the Python file is loaded. Therefore nothing that is from the XML is defined yet when this stuff is run. You are trying to look up things that don't exist yet.

A solution is to add this stuff to a new function in the class and call that function from both onGameStart and onLoadGame. Or something like that.

I think I understand what you're saying - I have to put them into a fuction [how do I go about doing that? I thought a function was do-this-and-this-and-tell-me-what-you-got] and cut-paste it into the bits which trigger at the start of the game. In other words, it doesn't get triggered when the game reads the file, but when the game has read everything and decides what to do with it.
 
Hang on, you've lost me again. Does what God-Emperor said work? If so, how do I implement it?

Sorry about this; as I said I'm very new to Python
 
They don't have to be global variables if you make them fields in the event manager class.

Rather Handwavey Example:

In the CvEventManager class create a new member function (not just part of the file, but part of the class) to do the setup.
Code:
class CvEventManager:
# ... somewhere in the class definition... (perhaps right after the __init__ function)
	def initMyData(self):
		self.iBritInf = gc.getInfoTypeForString("UNIT_BRITINF")
# etc.
You'd call this function to set up the variables when a new game starts or a saved game loads via a line of code that just says "initMyData()".

You use the values via "self.iBritInf" and suchlike from other functions in the class.

If you need to use them from another class, you should be able to access these existing values via something like
Code:
		EM = CvEventInterface.getEventManager()
		if (something == EM.iBritInf):
			...whatever...

It might be possible to define the values in the event manager's __init__ function, but I'm not sure. The event manager must be initialized pretty early but I don't know if it is before the XML data is loaded or not. If you can, then it would be easier as you wouldn't have to add the function to the game start and load callbacks.
 
I get it - so that fixes the 'python loads before XML' stuff? I'll give it a shot.

EDIT: YES! Finally it lives! Thanks everyone!
 
Sorry about this: as you people probably know I'm very new to Python, please be a little gentle with me - just say everything slowly and in words of two syllables or less and I'll probably get it. Can you go through how to turn on python alerts?
This information is available in many places. Edit my games/beyond the sword/civilizationiv.ini to change the two following settings:
Code:
; Enable the logging system
LoggingEnabled = 1
; Set to 1 for no python exception popups
HidePythonExceptions = 0
If you do this, you will get a popup for every syntax error in your python. In many cases, hopefully, this will give you enough info to solve the problem. If not, you may prefer to find the complete text of the message in my games/beyond the sword/logs/pythonerr.log and post that as text, rather than taking screenshots of the popups.
 
I did that after posting (I found a tutorial on Python which had it in). It appeared to be showing me that the word 'else:' was invalid syntax. Anyway, when I did what GE said it gives a 'no errors' report
 
Back
Top Bottom