Limiting Policy Adoption

Starrynite120

Prince
Joined
Jul 15, 2015
Messages
472
I'm trying to limit whether a player is allowed to adopt certain policies based on if they have other policies via lua. I can't see any errors in my script, but for some reason it is blocking adoption of any policies (and no errors on firetuner). Any ideas why?

Code:
function CanAdoptVirtue(playerID, policyID)
	local policyInfo = GameInfo.Policies["POLICY_INDUSTRY_2"]
	local virtueID = policyInfo.ID
	local requirement = GameInfo.Policies["POLICY_MIGHT_1"]
	local requirementID = policyInfo.ID
	local Player = Players[playerID];

	if policyID == virtueID and Player:HasPolicy(requirementID) then
		return true
	elseif policyID == virtueID then
		return false
	end
end
GameEvents.PlayerCanAdoptPolicy.Add(CanAdoptVirtue);
 
So now I'm trying this

Code:
function CanAdoptVirtue(playerID, policyID)
	local policyInfo = GameInfo.Policies["POLICY_INDUSTRY_2"]
	local virtueID = policyInfo.ID
	local requirement = GameInfo.Policies["POLICY_MIGHT_1"]
	local requirementID = policyInfo.ID
	local Player = Players[playerID];

	if policyID == virtueID and Player:HasPolicy(requirementID) then
		return true
	elseif policyID == virtueID then
		return false
	elseif policyID ~= virtueID then
		return true
	end
end
GameEvents.PlayerCanAdoptPolicy.Add(CanAdoptVirtue);

and it is blocking Industry correctly, and allowing others policies to be adopted, but it never allows Industry 2 to be adopted.
 
So I got something working. The script was not recognizing if the requirement policy was adopted. I changed it to a Player:HasPerk and now it is functioning. Weird, but I'll take it.
 
and it is blocking Industry correctly, and allowing others policies to be adopted, but it never allows Industry 2 to be adopted.

Code:
function CanAdoptVirtue(playerID, policyID)
	local policyInfo = GameInfo.Policies["POLICY_INDUSTRY_2"]
	local virtueID = policyInfo.ID
	[COLOR="Red"]local requirement = GameInfo.Policies["POLICY_MIGHT_1"]
	local requirementID = policyInfo.ID[/COLOR]
	local Player = Players[playerID];

	if policyID == virtueID and Player:HasPolicy(requirementID) then
		return true
	elseif policyID == virtueID then
		return false
	[COLOR="Red"]elseif policyID ~= virtueID then
		return true
	end[/COLOR]
end
GameEvents.PlayerCanAdoptPolicy.Add(CanAdoptVirtue);
You're setting requirementID = policyInfo.ID where you intended to use requiredment.ID. As a result the required ID is Industry2 not Might1. The naming you're using here is needlessly confusing. It is unwise to use both policy and virtue as they are essentially two ways to say the same thing. You want to pick names that help you remember the difference between the two things.

Your original issue is caused by (and partially fixed) the lower red section. With the "CanDoX" events you must return true or false. Your first attempt was not guaranteed to return a value. Presumably the lack of a return values was interpreted as returning false. CanDoX checks happen for everything so usually the first thing you want to do when added a restriction for Blah is to return True for everything that isn't Blah.
 
So I have a script mostly working for this, with one small error that I can't seem to figure out. I have attached a screenshot so you can see what the virtue trees look like after my editing. Might has been split into 3 separate trees, one for each affinity. Once you finish one of the trees it unlocks 2 of the more advanced trees (Prosperity, Knowledge, and Industry), but until you finish one of the Might trees you cannot adopt any of the other trees.

The first tree of Might is for Supremacy, second Purity, and third Harmony.

Finishing Supremacy unlocks Knowledge and Industry.

Purity unlocks Prosperity and Industry.

Harmony unlocks Prosperity and Knowledge.

So everything about this is working, but with a small error in that Purity is unlocking Knowledge when it is not supposed to, and I cannot figure out why. Everything else I have described is working, except that. Can anyone see what I'm missing?

Code:
function CanAdoptVirtue(playerID, policyID)
	local policyInfo1 = GameInfo.Policies["POLICY_INDUSTRY_1"]
	local virtue1ID = policyInfo1.ID
	local policyInfo2 = GameInfo.Policies["POLICY_KNOWLEDGE_1"]
	local virtue2ID = policyInfo2.ID
	local policyInfo3 = GameInfo.Policies["POLICY_PROSPERITY_1"]
	local virtue3ID = policyInfo3.ID

		local perk1 = GameInfo.PlayerPerks["PLAYERPERK_MIGHT_5"].ID;
	local perk2 = GameInfo.PlayerPerks["PLAYERPERK_MIGHT_6"].ID;
	local perk3 = GameInfo.PlayerPerks["PLAYERPERK_MIGHT_7"].ID;
		local perk4 = GameInfo.PlayerPerks["PLAYERPERK_MIGHT_12"].ID;
	local perk5 = GameInfo.PlayerPerks["PLAYERPERK_MIGHT_13"].ID;
	local perk6 = GameInfo.PlayerPerks["PLAYERPERK_MIGHT_14"].ID;
		local perk7 = GameInfo.PlayerPerks["PLAYERPERK_MIGHT_19"].ID;
	local perk8 = GameInfo.PlayerPerks["PLAYERPERK_MIGHT_20"].ID;
	local perk9 = GameInfo.PlayerPerks["PLAYERPERK_MIGHT_21"].ID;

	local Player = Players[playerID];

	if policyID == virtue1ID and (Player:HasPerk(perk1) and Player:HasPerk(perk2) and Player:HasPerk(perk3)) or (Player:HasPerk(perk4) and Player:HasPerk(perk5) and Player:HasPerk(perk6)) then
		return true
	elseif policyID == virtue1ID then
		return false
	elseif policyID ~= virtue1ID and policyID ~= virtue2ID and policyID ~= virtue3ID then
		return true
	end

	if policyID == virtue2ID and (Player:HasPerk(perk1) and Player:HasPerk(perk2) and Player:HasPerk(perk3)) or (Player:HasPerk(perk7) and Player:HasPerk(perk8) and Player:HasPerk(perk9)) then
		return true
	elseif policyID == virtue2ID then
		return false
	elseif policyID ~= virtue1ID and policyID ~= virtue2ID and policyID ~= virtue3ID then
		return true
	end

	if policyID == virtue3ID and (Player:HasPerk(perk4) and Player:HasPerk(perk5) and Player:HasPerk(perk6)) or (Player:HasPerk(perk7) and Player:HasPerk(perk8) and Player:HasPerk(perk9)) then
		return true
	elseif policyID == virtue3ID then
		return false
	elseif policyID ~= virtue1ID and policyID ~= virtue2ID and policyID ~= virtue3ID then
		return true
	end
end
GameEvents.PlayerCanAdoptPolicy.Add(CanAdoptVirtue);

For reference, virtue1ID is Industry, 2 is Knowledge, and 3 is Prosperity. Perk 1-3 is Industry, 4-6 is Purity, and 7-9 is Harmony.

(Note: I have set the FreePolicy on the Policy branches to nothing, so the first policy in each tree is fired in lua as a normal policy and not by policy branch. As far as I have tested this is working).
 

Attachments

  • Screenshot 2016-04-13 22.43.42.jpg
    Screenshot 2016-04-13 22.43.42.jpg
    285.4 KB · Views: 126
Back
Top Bottom