Having trouble making something fire only when none of a group is present in city lua

turingmachine

Emperor
Joined
May 4, 2008
Messages
1,438
Okay I think I've really confused myself with this.

I have an array of buildings.
I have a for loop.
I check the city for buildings in the array.
I need the event to fire only if none of the buildings in the array are present in the city.

Doing something like:

Code:
	for _, iBuilding in pairs(buildings) do
		if  not pCity:IsHasBuilding(iBuilding) then

Doesn't work. That would still fire even if one of the buildings was present in the city as the check would pass for all the other buildings. However I need it to only work if none of the buildings are present.

I tried to do a count, but I don't think I know how to do it properly.

Code:
	for _, iBuilding in pairs(buildings) do
		local count = 0
		if  pCity:IsHasBuilding(iBuilding) then
			count = count + 1
		end
		if count == 0 then

I thought that would check if the city had any of the buildings in the array, if there were any it would raise the count. Then it would only fire if none of the buildings were present in the city (i.e. the count was 0). However this didn't work and it still goes through if only one of the buildings in the array is present.

Can anyone give me some advice? Thanks.
 
gBuildings = {'BUILDING_BARRACKS', 'BUILDING_MARKET'}

for key, buildingType in ipairs (gBuildings) do

end

Try ipairs and see if that works if the values are as shown in my example.
 
(Based only on the incomplete snippet of code posted,) you need the initialisation of the count variable and the test to see if it is zero OUTSIDE the for loop

Code:
local count = 0
for _, iBuilding in pairs(buildings) do
  if  pCity:IsHasBuilding(iBuilding) then
    count = count + 1
  end
end
if count == 0 then
 
Back
Top Bottom