Adopting a Policy Branch

Shamrocalypse

Chieftain
Joined
Jul 15, 2012
Messages
3
I can't seem to find a way to adopt a policy branch from a lua script. For some reason Player:CanAdoptPolicy(i) and Player:DoAdoptPolicy(i) don't seem to work for the policy branches (always return false and do nothing, respectively), but they work for the sub-policies. The branches are listed along the sub-policies in the Policies table, so I figure it should use the same function. Also Player:HasPolicy(i) reports the correct state for the branches as well as sub-policies. Player:SetPolicyBranchUnlocked() also seems to have no effect. Anyone had luck doing this action programmatically?
 
Anyone had luck doing this action programmatically?

All the time (modified code from my Policies LiveTuner Panel mod)

Code:
function grantPlayerPolicy(pPlayer, sPolicy)
  privateGrantPlayerPolicy(pPlayer, sPolicy)
end

function privateGrantPlayerPolicy(pPlayer, sPolicy)
  local iPolicy = GameInfo.Policies[sPolicy].ID

  if (not pPlayer:HasPolicy(iPolicy)) then
    for i, sRequiredPolicy in ipairs(requiredPolicies(sPolicy)) do
      privateGrantPlayerPolicy(pPlayer, sRequiredPolicy)
    end

     -- Need to adopt the branch?
    if (GameInfo.Policies[iPolicy].PolicyBranchType == nil) then
      for pBranch in GameInfo.PolicyBranchTypes{FreePolicy = sPolicy} do
        pPlayer:SetPolicyBranchUnlocked(pBranch.ID, true)
      end
    end

    pPlayer:SetHasPolicy(iPolicy, true)
  end
end

Probably more complex than you need, in that if you gramt a 2nd tier policy it will (recursively) grant any required 1st tier policies as well and also adopt the branch if necessary.
 
Thanks man! Apparently I just wasn't passing the second bool arg to SetPolicyBranchUnlocked. That function doesn't consume the available policy (and I'm trying to simulate the user selecting this), but I can workaround it by doing a DoAdoptPolicy for the first policy in the branch (which consumers the policy choice), then SetHasPolicy(i, false) to retract the extra one. Not elegant, but functional.
 
Back
Top Bottom