Python Help - Can't make head nor tail of this:

Flying Pig

Utrinque Paratus
Retired Moderator
Joined
Jan 24, 2009
Messages
15,651
Location
Perfidious Albion
Code:
################
### Globals ###
##############

gc = CyGlobalContext()
PyPlayer = PyHelpers.PyPlayer
localText = CyTranslator()

# initialise misc variables
iX = 0
iY = 1
iNoFocus = -1
iFocusBoth = -2
bForceCoastal = True
bUsePythagoras = True
#tAlAqabahOilField = (51, 7)

# initialise team variables
iAllies = 0
iAxis = 1

# initialise player variables to player IDs from WBS
iBritish = 0
iAllied = 1
iFreeFrench = 2
iVichyFrench = 3
iGermans = 4
iItalians = 5
iSpanish = 6
iTurkish = 7

# initialise unit variables to unit indices from XML
iAlliedInfantry = 0
iBritishInfantry = 1
iVichyFrenchInfantry = 2
iFreeFrenchInfantry = 3
iGermanInfantry = 4
iTurkishInfantry = 5
iItalianInfantry = 6
iSpanishInfantry = 7
iVickers = 8        # Allied light tank
iCrusader = 9        # British light tank
iRenault = 10        # French light tank
iPanzerIII = 11        # German light tank
iL3tank = 12        # Italian light tank
iVerdeja = 13        # Spanish light tank
iMatilda = 16        # British heavy tank
iPanzerIV = 17        # German heavy tank
iTiger = 18        # German very heavy tank
iTransport = 21
iATransport = 23    # Allied transport
iDestroyer = 24
iBattleship = 25
iSubmarine = 26
iCarrier = 27
iACarrier = 28        # Allied carrier
iMesserschmidt = 32    # German fighter
iB25 = 45        # Allied tactical bomber
iStuka = 47        # German tactical bomber
iRommel = 59        # first German Great Commander

# units the British can't build if they lose the Middle East
forbiddenUnitList =    [    iCrusader,
                iMatilda,
                iTransport,
                iDestroyer,
                iBattleship,
                iSubmarine,
                iCarrier    ]

# initialise promotion variables to promotion indices from XML
iAmbush = 10
iMedicI = 12
iMedicII = 13
iGuerillaI = 14
iRepair = 39

# initialise tech variables to tech indices from XML
iDesertFox = 11        # Desert Fox
iBarbarossa = 12    # Operation Barbarossa
iPearlHarbor = 13    # Pearl Harbor
iTorch = 14        # Operation Torch
iArmySupport = 15    # Army Support


# initialise civic variables to civic indices from XML
iGuerrilla = 2

# initialises city variables to city coords from WBS
tMarrakech = (1, 6)
tRabat = (2, 12)
tValladolid = (2, 29)
tTangiers = (3, 16)
tCadiz = (3, 21)
tMadrid = (5, 25)
tBechar = (6, 6)
tGibraltar = (6, 18)
tOran = (7, 13)
tCartagena = (9, 22)
tBarcelona = (9, 28)
tAlgiers = (11, 14)
tPalma = (13, 24)
tGabes = (16, 10)
tTunis = (16, 14)
tAlghero = (17, 28)
tCagliari = (18, 24)
tTripoli = (20, 8)
tPalermo = (21, 16)
tValletta = (22, 12)
tSurt = (24, 5)
tMessina = (24, 16)
tNaples = (24, 26)
tFoggia = (25, 29)
tCatanzaro = (28, 20)
tBari = (28, 25)
tAjdabiya = (30, 3)
tBenghazi = (31, 8)
tIgoumenitsa = (32, 29)
tPreveza = (33, 23)
tTobruk = (35, 10)
tYithion = (36, 17)
tAthens = (37, 20)
tLarisa = (37, 29)
tVolos = (38, 25)
tElAlamein = (39, 8)
tIraklion = (39, 15)
tAlBawiti = (40, 5)
tAlMinya = (42, 1)
tAlexandria = (42, 9)
tIzmir = (43, 24)
tIstanbul = (43, 29)
tCairo = (44, 5)
tNicosia = (45, 15)
tAntalya = (45, 19)
tSuez = (46, 9)
tKonya = (47, 23)
tAnkarra = (47, 29)
tRasGharib = (48, 2)
tBeirut = (49, 14)
tAlAqabah = (50, 8)
tLatakia = (50, 20)
tDamascus = (51, 11)
tHoms = (51, 17)
tKaysen = (51, 25)

Comes from the Desert War scenario. My querstion is: where do all these numbers refer to? How does it know that iBarbarossa = 12 and iMedic1 = 12 point to different things?
 
IMHO, this is a very poor coding technique. It only works if the author of the python code counts carefully to find the positions of objects in the xml file, and then it breaks if somebody edits the xml file and moves objects around.

The integer numbers effectively refer to the position of the object in the xml file. So if building ABC is the 12th building in its xml file, and unit XYZ is the 12th unit in its xml file, both of them use the value 12. The python interface does not protect you from accidentally passing an integer for a building object into a call which expects an integer from a unit object.

A much better way to write the code is to use the XML names and the gc.getInfoTypeForString function:

gc = CyGlobalContext()
iVickers = gc.getInfoTypeForString("UNIT_VICKERS")

You need to read the xml to find out the exact name of the unit; but once you have done that, then the python will still be correct even if the xml is rearranged.
 
gc = CyGlobalContext()
iVickers = gc.getInfoTypeForString("UNIT_VICKERS")

So this just says 'search everything for anything called UNIT_VICKERS and call that iVickers, yes? So it will work with promotions, civs, anything, with no extra work?
 
Lists of predefined constants do have an advantage- they're fast. In Python-heavy scenarios, that's a good thing.
 
So this just says 'search everything for anything called UNIT_VICKERS and call that iVickers, yes? So it will work with promotions, civs, anything, with no extra work?

and replace it with the number of them in the XML file would be correct, but yes, if i understand you right, this is how it works.

Lists of predefined constants do have an advantage- they're fast. In Python-heavy scenarios, that's a good thing.

:yup:
 
So this just says 'search everything for anything called UNIT_VICKERS and call that iVickers, yes? So it will work with promotions, civs, anything, with no extra work?

Yes. Of course, if you have a promotion called UNIT_VICKERS, you are in for a world of confusion. Most modders follow naming conventions in the xml.

jmerry said:
]Lists of predefined constants do have an advantage- they're fast. In Python-heavy scenarios, that's a good thing.

That is a bit of an oversimplification. In well written python, the gc.getInfoTypeForString is called once, at the start of the program, to set the constant. Thereafter, nobody cares how the constant was set. The few microseconds of runtime to lookup these constants once is much better than the hours/days of debugging time to figure out that some bug is caused because another modder added a unit to the xml in the "wrong" place.
 
Top Bottom