Units with the same ID?

Tholal

Emperor
Joined
May 19, 2009
Messages
1,676
I noticed in a save game from my mod that there are three units (each from a different civ) that all have the same ID. This seems incorrect. Any idea how this could happen?
 
What do you mean by ID? Because each player has its own array of units, all enumerated from zero and up. So every player can totally have a unit with the ID 1 and a unit with the ID 2.

The point is that you need both a PlayerType (the ID of the Player) and a unit ID to figure out what CyUnit instance you need to reference.
 
Every unit has a unique CvUnit (CyUnit in Python) instance, with a unique address in the memory. But you don't need to worry about that. Instead you identify units based on what player they belong to.

It should also be noted that there are regularly gaps in the arrays of cities or units. Like if the first city or unit (ID zero) is destroyed, then that slot may be vacant for some time before the game manages to reorganize the order.
 
Oh, and also: Each map plot has its own array of units. So the first unit on any given plot has the ID zero, the second one has the ID 1 and so forth. This enumeration is independent of the player unit ID.
 
ID's are basically only used for save games because it is useless to save pointers of Unit Objects.
 
ID's are basically only used for save games because it is useless to save pointers of Unit Objects.

Since this may have some effect on my mod, I have a question:

If save games hold only the ID of a unit and the PlayerTypes of the unit's owner, how does the game reconstruct the unit upon loading? How does it get the other information that comes with a CvUnit object?
 
That stuff is directly taken from the XML files.

Easy experiment to see it: Create a game, make a savegame, and then move in the UnitInfos.xml the first unit to the end or something similar. Then take again a look at your savegame, you'll have other units, because the game doesn't really store anything unit related there.
 
I'm still confused. I thought the ID of a unit was determined by when it was built by that civ (first unit's ID would be 0, next would be 1, and so on). And even if the ID is determined by the unit's place in the XML file, how does it store in-game changes to the unit, like promotions?
 
Back
Top Bottom