City IDs...any existing way to get a unique ID?

ww2commander

Emperor
Joined
Aug 23, 2003
Messages
1,243
Location
Australia
Is there a way to obtain a unique ID for cities?

I noticed that city IDs are not unique and can be reused with the player ID distinguishing them.

I gather I could create an ID by combining playerID and cityID, but wanted to confirm if the system has something already.
 
I did not try it for cities, but you should test if the ID is not reused after a city has been destroyed. Or what happens when the city is captured/liberated.
 
At present I am storing cityIDs in an array using g_CityData[playerID][cityID]

I had to do this after I noticed that using g_CityData[cityID] would iterate the same data for a Russian and German city on my map! Only after adding playerID did I get unique info as opposed to the array overriding the same cityID entry!
 
playerID+cityID is unique for every current city on the map. What is not guarenteed is that it is unique for the duration of the game (which probably won't affect your particular circumstances), eg found Portsmouth (cityID=3456), found other cities, Portsmouth is captured and razed, found Plymouth (cityID could be 3456)

However, even that's not a problem, just hook the CityCaptureComplete() and/or SetPopulation() events and clear out any cached data
 
A simple 'for' loop print of the above array using [playerID][cityID] confirmed that player 0, 1, 2 and so on all utilized the same city ID's but in different order.

This explains (I assume) why we have Player:GetCityByID and Player.GetUnitByID as opposed to City:GetCityByID and Unit:GetUnitByID as the game uses its own unique key system.

In case your not aware, Gedemon uses a 'unitkey' approach where he constructs a unit identifier for each unit on the map by using playerID + unit creation turn + unitID.
 
Yes, I know about the non-uniqueness for these. I use my own identifier. But whoward69's playerID+cityID would be easier to use than my system, so I was questioning that. (Well... I shouldn't question whoward69 I guess.)

...constructs a unit identifier for each unit on the map by using playerID + unit creation turn + unitID.

OK, that one too. Are you all just counting on cityID and unitID being big numbers so collision is unlikely? Or do you know something about ID generation that I don't?

Edit: OK, let me make my point more explicit. To my knowledge (which is incomplete) these IDs appear to be large arbitrary integers, with the only rule being that the same number isn't handed out to different cities (or units) from the same player. Let's say player 0 gets a cityID = 33452. Then player 6 gets a cityID = 33446. In this case, whoward69's system fails. Ergo: whoward69 either 1) is counting on that unlikely event not happening or 2) knows something about ID generation that I don't. Same for Gedemon's unit identifier. (I'm trying to provoke them into telling me that #2 is the case.)
 
To clarify, the key is not a single integer.

Gedemon's code:
Code:
function GetUnitKey(unit)
	if unit then
		local ownerID = unit:GetOwner()
		local unitID = unit:GetID()
		local turn = unit:GetGameTurnCreated()
		local unitKey = unitID..","..ownerID..","..turn
		return unitKey
	else
		Dprint("- WARNING: unit is nil for GetUnitKey()")
	end
end
 
I use a string for the unique key :D

Code:
	local ownerID = unit:GetOwner()
	local unitID = unit:GetID()
	local turn = unit:GetGameTurnCreated()
	local unitKey = unitID..","..ownerID..","..turn

edit: beaten to quote my own code :o
 
OK, so "playerID + unit creation turn + unitID" was really referring to string concatenation. Should I be reading whoward69's playerID+cityID the same way? (if not, my question still stands)
 
Back
Top Bottom