unit home city

carloscodex

Chieftain
Joined
Sep 14, 2012
Messages
66
Location
Pleasanton (San Francisco), Ca
I hope I am not repeating a question here, but searching for "unit home city" and various other combinations didn't return what I wanted. The API seemed to have nothing on this, either.

I am trying to find a unit's home city -- the city from which it was build. Seems like it should be straightforward or something build-in, right?

I am looking to do two things:

1. find home city of unit
2. determine all units ALREADY built by city


Any suggestions? Thanks all for the help. I really appreciate it.
 
It's not recorded by the base code (C++) so you'll have to detect the unit creation via Lua (there are numerous mods out there that do this that you can borrow the code from) and track it yourself - persisting the data across saves.

But bear in mind that not all units have a "home city" - they can spawn from goody huts, prize ships, city states, etc, etc, etc. You'll also have to cope with upgrades - as a unit doesn't actually upgrade but spawns the next unit, copies all the data from the itself (the now defunct unit) to the new one and then deletes the defunct unit.
 
I've got the unit creation done using:

Spoiler :

function SerialEvenUnitCreated_Pre(playerID, unitID, hexVec, unitType, cultureType, civID, primaryColor, secondaryColor, unitFlagIndex, fogState, selected, military, notInvisible)

...

local pUnit = Players[playerID]:GetUnitByID(unitID)

local pCity = { *** FIND UNIT HOME CITY HERE *** }

if unitType == "UNIT_BUILDING_LOOKOUT_ROCK" then
pPromotion = GameInfo.UnitPromotions["PROMOTION_CLIMB_AND_LOOK"].ID;

{ *** DO CODE BASED ON HOME CITY; FIND ALL OTHER UNITS CREATED BY THIS CITY *** }

...



but I need to find the home city from which the unit was created. Am I missing something here?
 
From pUnit you can get the plot it is standing on, from the plot you can get the city (if any) on that plot
 
Back
Top Bottom