question about detecting building methods

Craig_Sutter

Deity
Joined
Aug 13, 2002
Messages
2,773
Location
Calgary, Canada
Why would one use this:

Code:
local Buildingnum = city:GetNumRealBuilding(iBuilding)
if Buildingnum == 0 then
						
	city:SetNumRealBuilding(iBuilding, 1);

end

instead of this:

Code:
if not pCity:IsHasBuilding( iBuilding ) then

	pCity:SetNumRealBuilding(iBuilding, 1);

end

I found the former code to not consistently fire for some reason... am now going to try the latter in autoplay to see if I get better results. Hopefully, it will fire more reliably.
 
There's a difference, as I remember, between the way the game treats buildings that are given for free (such as from a wonder) and buildings that are added to a city in any other manner. As I recall, GetNumRealBuilding(iBuilding) does not take into account wonder-added buildings and may not take into account Trait-added buildings. As I recall the conversation DarkScythe and I had on this subject many moons ago, "GetNumBuildings()" will always give accurate results, as will "IsHasBuilding()", but "GetNumRealBuilding(iBuilding)" will give false negatives.
 
As I recall, GetNumRealBuilding(iBuilding) does not take into account wonder-added buildings ...

Correct, it doesn't ... for the reason that they are "free" (as in no associated maintenance costs)

pCity:GetNumBuilding(iBuilding) returns pCity:GetNumRealBuilding(iBuilding) + pCity:GetNumFreeBuilding(iBuilding)

pCity:IsHasBuilding(iBuilding) returns (pCity:GetNumBuilding(iBuilding) > 0)

Which is why the code differs.

You could use

Code:
local Buildingnum = city:GetNumBuilding(iBuilding)
if (Buildingnum == 0) then
	city:SetNumRealBuilding(iBuilding, 1)
end
for the same effect as using city:IsHasBuilding(iBuilding) if you really wanted.

On a final note, there is no pCity:SetNumFreeBuilding(iBuilding, 1) method, nor are there pCity:IsHasRealBuilding(iBuilding) or pCity:IsHasFreeBuilding(iBuilding) methods.
 
Craig, if a city will only ever be given 1 copy of "iBuilding", ie, there is no way your code is ever going to do anything like:
Code:
city:SetNumRealBuilding(iBuilding, 2)
Then you can just directly do:
Code:
city:SetNumRealBuilding(iBuilding, 1)
and you could dispense with the rest of:
Code:
local Buildingnum = city:GetNumBuilding(iBuilding)
if (Buildingnum == 0) then
	city:SetNumRealBuilding(iBuilding, 1)
end
Since that chunk of code as you have it is essentially forcing the city to have one copy of the building.

--------------------------------------------------------------------------------------

Then all you have to concern yourself with is any other logic that decides whether or not an individual city needs to have the building given to it. Something like, say, this:
Code:
for city in player:Cities() do
	if city:IsCoastal() then
		city:SetNumRealBuilding(iBuilding, 1)
	end
end
In that example I don't care whether the city already has the building, I am just arbitrarily putting one copy of the building into the city if the city is Coastal. Nothing will get 'broken' from repeating the same commands and re-placing the same building in the same city over multiple turns, for example. The only real question is whether the code can get to be some real inefficient processing when it is doing lots of stuff like that over and over every turn and in multiple cities with no actual changes made on successive run-throughs of the code.
 
Back
Top Bottom