Triggering only for Capital, not all cities

Starrynite120

Prince
Joined
Jul 15, 2015
Messages
472
I have several scripts I'm running that I thought should be working for every city, but is only applying to the capital. For example, this script is supposed to give food in every city equal to half the number of policies you own, but instead it's applying to only the capital, and to the capital multiple times, equal to the number of cities you have. I'm assuming its a problem with how I'm acquiring the cityID, but I'm not sure what the problem is.

Code:
function FoodPerVirtue(playerID, cityID)
	local player = Players[playerID];
	local perk = GameInfo.PlayerPerks["PLAYERPERK_AFFINITY_BETTER_TERRASCAPES"].ID;
	local City = player:GetCityByID(cityID)

	if(player:HasPerk(perk)) then
		local bonus = (player:GetNumPolicies() / 2);

		if(bonus > 0) then
			for cityID in player:Cities() do
				City:ChangeFood(bonus);
			-- Send a notification to the player
			local text = Locale.ConvertTextKey("TXT_KEY_STARRYNITE_PURITY_8_NOTIFICATION", tostring(bonus));
			player:AddNotification(NotificationTypes.NOTIFICATION_GENERIC, text, text);

			end
		end
	end
end
GameEvents.PlayerDoTurn.Add(FoodPerVirtue);

Edit: Of course, just figured it out. It should be for City, not for cityID.
 
Code:
function FoodPerVirtue(playerID, [COLOR="Red"]cityID[/COLOR])
...
	local City = player:GetCityByID(cityID)
...
end
GameEvents.PlayerDoTurn.Add(FoodPerVirtue);
PlayerDoTurn doesn't provide a cityID as the second argument. It only provides the playerID as the first argument. This isn't the cause of your problem but it can mess you up in the future.
-------------
If you want to give food each turn to all cities it is likely easier to just put a dummy building in the player's cities. This will require less lua, less per turn processing and play nicely with the how-many-turns-until-growth city UI.

I believe your requirements are:

+1 F per-turn for every every city for every 2 policies

What you could do is create a bunch of perks that use PlayerPerks_CityYieldEffects to provide 1, 2, 3, 4, etc Food to cities (I'm going to refer to these perks as the "Food perks" from here on). Write some lua that fires when players adopt policies. When a player adopts a policy you calculate what Food perk they should have. Remove all the food perks they do have and give them the correct food perk.
 
That's a pretty good idea, I should give that a shot. Question though. Do you know if GetNumPolicies includes synergy and kicker policies?
 
Off hand I'm not sure. I would guess not (but that is a low confidence guess). But if you want to only count certain kinds of policies you can loop over them all and count whichever you want.
 
Top Bottom