Help with OnPolicyAdopted

JFD

Kathigitarkh
Joined
Oct 19, 2010
Messages
9,132
Location
The Kingdom of New Zealand
I'm trying to get this code to work:

Spoiler :
function OnPolicyAdopted(playerID, policyID)
local pPlayer = Players[playerID];​
local iGoldenAgeBoost = 50;​
-- Adjust the size of the bonus based on the game speed​
local speedGoldenAgePercent = GameInfo.GameSpeeds[Game:GetGameSpeedType()].GoldenAgePercent;​
local adjustedBonusGoldenAgePoints = (iGoldenAgeBoost * speedGoldenAgePercent) / 100;​
if (pPlayer:HasPolicy(GameInfo.Policies["POLICY_TRADITION"].ID)) then​
pPlayer:ChangeGoldenAgeProgressMeter(iGoldenAgeBoost);​
end​
end

GameEvents.PlayerAdoptPolicy.Add(OnPolicyAdopted);


but am not having much luck. It is simply having no effect. There are no errors coming up in the log file either and I'm still slowly figuring out lua. I'd appreciate any help and some explanation as to what I've done wrong. I've managed to learn a few new things on my own, but when there's nothing indicated in the log it makes it difficult. Thanks.
 
is there a space also in your actual code for 'iGoldenAgeBoo st' on line 8 ?
 
oh, yes.

you can add a code tag in your spoiler tag to make it appears correctly and show the indentation, like that:

Spoiler :
Code:
function OnPolicyAdopted(playerID, policyID)
	local pPlayer = Players[playerID];
	local iGoldenAgeBoost = 50;
	-- Adjust the size of the bonus based on the game speed
	local speedGoldenAgePercent = GameInfo.GameSpeeds[Game:GetGameSpeedType()].GoldenAgePercent;
	local adjustedBonusGoldenAgePoints = (iGoldenAgeBoost * speedGoldenAgePercent) / 100;
	if (pPlayer:HasPolicy(GameInfo.Policies["POLICY_TRADITION"].ID)) then
		pPlayer:ChangeGoldenAgeProgressMeter(iGoldenAgeBoost);
	end
end

GameEvents.PlayerAdoptPolicy.Add(OnPolicyAdopted);


Now if you want to change the size of the golden age, you should use ChangeGoldenAgeTurns.

IIRC ChangeGoldenAgeProgressMeter is to change the value of the progression to the next golden age.
 
Yes, ChangeGoldenAgeProgressMeter does contribute to your Golden Age progression. I tested it successfully with OnCityCaptureComplete. This is the one I want, rather than changing the length of Golden Ages or giving a Golden Age. It's just that OnPolicyAdopted isn't working. I tested it with giving a free unit when a policy is adopted, rather than the Golden Age thing, but this also didn't work.

It's possible that this only works if a specific policy is defined, but I'll have to test this later.
 
No error messages can mean that the code is never being executed in the first place. Do you have an InGameUIAddin entry for that file (or if it is included from another file, is this file set to VFS=true and the other file has an InGameUIAddin entry)

At the top of every Lua file that is meant to run as the game starts-up, place a
print("This is the Xyz script file")
line. It'll show in the lua.log and tell you that the file is at least loading and executing.

Other than that, without creating a mod to add that code to, the only thing I can see wrong is it should be Game[dot]GetGameSpeedType() not Game[colon]GetGameSpeedType() - but for a method without parameters it shouldn't actually make a difference.
 
If I remember correctly, there are two events, PlayerAdoptPolicy and PlayerAdoptPolicyBranch. The second only fires when adopting the opener, while the first doesn't fire on the opener but does fire on the other policies. If you were testing the code by simply adopting the Liberty or Tradition opener nothing would happen. To work correctly the function would need to be added to both events.

An extremely minor optimization note -- the "if(playerHasPolicy Tradition)" check can be done before the goldenAge = 50 line. That way no unnecessary calculations are made if the player doesn't have Tradition.
 
If I remember correctly, there are two events, PlayerAdoptPolicy and PlayerAdoptPolicyBranch. The second only fires when adopting the opener, while the first doesn't fire on the opener but does fire on the other policies. If you were testing the code by simply adopting the Liberty or Tradition opener nothing would happen. To work correctly the function would need to be added to both events.

An extremely minor optimization note -- the "if(playerHasPolicy Tradition)" check can be done before the goldenAge = 50 line. That way no unnecessary calculations are made if the player doesn't have Tradition.

Ah, okay. Thanks. I appreciate the response.
 
Back
Top Bottom