How do I get the city a unit is in?

Joined
Jul 5, 2004
Messages
23,562
Location
Canberra, Australia
As the title says I have a unit but need to do something to the city the unit is in (Random Event manager). I have tried from both cyUnit and cyCity but end up with a bit of magic required or in other words a missing step. (Maybe I just need to take a walk round the block.)

Edit The only way I can see to do it is to get the X Y values of the unit then cycle through all the player's cities until I find the one with the same X and Y.
 
Since you know how to get the X and Y values, I believe you know how to get the Cyplot where the unit is?

From the plot itself, just use getPlotCity()
 
You can even skip the X, Y values entirely since a unit also has a .plot() function which returns the CyPlot directly.

pPlot = pUnit.plot()
pCity = pPlot.getPlotCity()
or, if you don't need the plot for anything else, you can skip the intermediate step like so:
pCity = pUnit.plot().getPlotCity()

It should return the None object if there isn't a city, generally tested via something like "if not pCity or pCity.isNone():" to see if it does not exist (which also covers the case where the "pCity" variable doesn't exist at all). Alternatively you can check for the presence of a city via "pUnit.plot().isCity()" to get a true/false value.
 
You can even skip the X, Y values entirely since a unit also has a .plot() function which returns the CyPlot directly.

pPlot = pUnit.plot()
pCity = pPlot.getPlotCity()
or, if you don't need the plot for anything else, you can skip the intermediate step like so:
pCity = pUnit.plot().getPlotCity()

It should return the None object if there isn't a city, generally tested via something like "if not pCity or pCity.isNone():" to see if it does not exist (which also covers the case where the "pCity" variable doesn't exist at all). Alternatively you can check for the presence of a city via "pUnit.plot().isCity()" to get a true/false value.

Typical! I looked through the list of unit methods three times expecting a getPlot or similar but never just plot!:blush:

Yes I almost always check that I do in fact get a city.

Thank you
 
Back
Top Bottom