Player:GetNumPolicies() Question

washy

Chieftain
Joined
Feb 9, 2015
Messages
61
It´s some way of using Player:GetNumPolicies() to get the number of policies of one special branch or is always the total number of policies?
 
It's always the total number.

But you can do workarounds to find out how many policies a player has in a specific branch.

A rough draft would look something like this:

Code:
function CountPoliciesInBranch(iPlayer, policyBranchType)
 local player = Players[iPlayer]
 local count = 0
 -- Loop through all policies
 for row in GameInfo.Policies() do
  -- Test if it's the correct branch
  if row.PolicyBranchType == policyBranchType then
   -- If so, test if the player has the policy
   if player:HasPolicy(row.ID) then
    count = count + 1
   end
  end
 end
 return count
end

print( CountPoliciesInBranch(0, "POLICY_BRANCH_LIBERTY") )

Of course if you're looking to do that with only one specific branch, then it's probably better to put the policies in that branch into a table at the start of the game, because then you don't need to loop through ALL policies each time the function is called.

...this response was probably complete overkill.
 
Thank you, that was very good, it´s mainly x ideology branches, i think i'm gonna use your code
 
It's always the total number.
Note that (outside the VMC/CP modded DLL) this number also includes dummy policies! I.e. invisible and unobtainable policies that are generally used for a modded civ's UA.
Looping over all branches and counting the number of obtained policies in there (I.e. using @Ryika 's suggested "overkill method" for each policy branch) will not include these dummy policies (since those are not assigned to any policy branch).

While in your case this would not matter (since you only care about looping over specific branches), I'm just leaving this comment here for reference.
:)

-------

EDIT:
Furthermore, this method also exists which does exactly what @Ryika 's overkill method does too (but probably more efficiently).
Code:
local iBranchFreedom = GameInfoTypes.POLICY_BRANCH_FREEDOM
pPlayer:GetNumPoliciesInBranch(iBranchFreedom)
It also works for ideological branches (I've used it myself for that purpose too).
 
Back
Top Bottom