[TOT] [TOTPP] Unit ID Numbers

Prof. Garfield

Deity
Supporter
Joined
Mar 6, 2004
Messages
4,382
Location
Ontario
@SorFox, @Knighttime and I have been discussing the game's ID numbers for units recently. This is important for saving information about specific units in the state table. I had always assumed that the game might move around unit id numbers, and so saving them was unreliable (and so, not to be used for 'important' things). I never tested this.

Via lua, I created 2200 Roman warriors. I created a few Japanese legions to kill them, and printed the id numbers of all legions before the attack and after saving and loading the game. The game did not change the id number of the legions, despite such massive numbers of no longer needed unit spaces, so I would tentatively conclude that the game does not 'compact' unit id numbers when saving.

Code:
for unit in civ.iterateUnits() do if unit.type == civ.getUnitType(5) then print(unit.id) end end
112
213
714
715
1216
1717

Short of recording a list of all the units in the game every turn and seeing if something changed 'unexpectedly', I'm not sure what other kind of experiment could be run. If anyone has any ideas, I'd be happy to hear them. The trouble is that if the game messes with the unit ID numbers, and someone uses them as if they are fixed (as long as the unit stays alive), it could lead events to fail in ways that are not easy to debug.
 

Attachments

  • IDNumbers.zip
    17 KB · Views: 155
If you create new units do they take "new" numbers? I just wonder if the trigger for recycling comes later than when a game is saved/loaded.
 
If you create new units do they take "new" numbers? I just wonder if the trigger for recycling comes later than when a game is saved/loaded.

Based on what others have said, the game just looks for the first 'unused' id number (one or two tests suggested the same thing to me). So, you have to worry about if the unit in question has been killed/disbanded/etc and a new unit has subsequently been created and taken its place, but shouldn't have to worry about if the unit is still alive, but has been assigned a new id number.

If you're only trying to keep track of a small number of units, it might make sense to create 500-1000 'dummy' units, then create the units you want, and delete the dummy units after. Then, you can be pretty sure the unit hasn't been inadvertently lost and replaced.
 
BTW, I noticed the other day that cities seem to behave the same way as units, in that the game always assigns the lowest unused ID number when a new city is founded. Obviously cities don't get destroyed nearly as often as units do, so the IDs of new cities will mostly go up sequentially, but if a city is destroyed then the next one that's built will apparently take its ID. This caught my attention because the city roster on the F1 screen (and others) normally lists cities in the order they were founded, but one of my new cities suddenly showed up second in the list instead of at the bottom like I was expecting. I'm pretty sure the re-use of IDs is the explanation.
 
Top Bottom