LeeS
Imperator
I made a couple of minor changes to the code here to tighten up and to add a couple of things so that the code only runs when the civ is actually part of a game, but otherwise there was nothing to debug really because there is nothing really wrong with code or the mod as posted on Post #5 (other than the couple of changes already mentioned, which I included into the code as posted here and which were active when I tested).
Both the mod and the lua code itself work just fine for me.
If you were expecting GameEvents.PlayerAdoptPolicy to fire when you open a policy branch, it does not. It only fires when you select the actual policies within a policy branch. Nor will it fire reliably when directly giving yourself policies via an lua method, via IGE, or via Live Tuner. You actually need to use the Pre-Provided Policy Selection Pop-up to get GameEvents.PlayerAdoptPolicy to fire reliably.
Relevent portions of my file Lua.log:
----------------------------------------------
There is a slight goof in the code for the player notification in that the argument position for the 1st "-1" is actually meant for the text string for the general description of the notification message, such as "Free Unit" or something.:
The trailing parameters aren't really required for a "NOTIFICATION_GENERIC" because for that particular type of notification the game makes no use of them.
------------------------------------------------
The process I use to test whether a PlayerAdoptPolicy listener is firing correctly is to give myself a bunch of culture in IGE, then close out IGE, then use the normal selection pop-up menu to select a policy.
-------------------------------------------------
Also, GameEvents.PlayerAdoptPolicyBranch(PlayerID, PolicyBranchID) fires when you adopt a policy branch, but it does not fire when you select an ideology in BNW.
Both the mod and the lua code itself work just fine for me.
Spoiler :
Code:
print("Started Mental script")
local iCiv = GameInfoTypes.CIVILIZATION_MENTAL
local iCargo = GameInfoTypes.UNIT_CARGO_SHIP
local iCaravan = GameInfoTypes.UNIT_CARAVAN
local iPietyPolicy1 = GameInfoTypes.POLICY_ORGANIZED_RELIGION
local iPietyPolicy2 = GameInfoTypes.POLICY_REFORMATION
local iPietyBranch = GameInfoTypes.POLICY_BRANCH_PIETY
local iMentalPromotion = GameInfoTypes.PROMOTION_MENTAL_HOARD
--This stuff was borrowed and modified from other Civ, in this case:
--Hakurei Shrine
-------------------------------------------------------------------------------------------------------------------------
-- GetStrongestMilitaryUnit
-------------------------------------------------------------------------------------------------------------------------
function GetStrongestMilitaryUnitStrengthM(pPlayer, bIgnoreResources, ...)
local tUnit = {["Combat"] = 0}
for iKey, sCombatType in pairs(arg) do
for row in GameInfo.Units("CombatClass = \'" .. sCombatType .. "\'") do
if pPlayer:CanTrain(row.ID, bIgnoreResources) and row.Combat > tUnit.Combat then
tUnit = row
end
end
end
return tUnit.Combat
end
print("Mentals Lua: GetStrongestMilitaryUnitStrengthM")
function MentalStrength(pPlayer,iPower)
if (pPlayer:IsAlive()) then
for pUnit in pPlayer:Units() do
if (pUnit:IsHasPromotion(iMentalPromotion)) then
local tUnitData = GameInfo.Units[pUnit:GetUnitType()]
local iBaseCombatStrength = tUnitData.Combat
local iBaseRangedCombatStrength = tUnitData.RangedCombat
local iModifiedCombatStrength = math.ceil(iBaseCombatStrength * (1+(iPower/10)))
local iModifiedRangedCombatStrength = math.ceil(iBaseRangedCombatStrength * (1+(iPower/10)))
pUnit:SetBaseCombatStrength(iModifiedCombatStrength)
end
end
if pPlayer:IsHuman() then
pPlayer:AddNotification(NotificationTypes.NOTIFICATION_GENERIC, "This is a test: Unit Power at: "..iPower, -1, -1);
end
end
end
print("Mentals Lua: MentalStrength")
function UpdateMentalPolicy(iPlayer)
print("Testing")
local pPlayer = Players[iPlayer]
if (pPlayer:IsAlive()) and (pPlayer:GetCivilizationType() == iCiv) then
print("Mental has "..pPlayer:GetNumPolicies().." policies")
MentalStrength(pPlayer,pPlayer:GetNumPolicies())
end
end
print("Mentals Lua: UpdateMentalPolicy")
------------------------------------------------------------
---- WilliamHoward's IsCivInPlay
------------------------------------------------------------
function IsCivInPlay(iCivType)
for iSlot = 0, GameDefines.MAX_MAJOR_CIVS-1, 1 do
local iSlotStatus = PreGame.GetSlotStatus(iSlot)
if (iSlotStatus == SlotStatus.SS_TAKEN or iSlotStatus == SlotStatus.SS_COMPUTER) then
if (PreGame.GetCivilization(iSlot) == iCivType) then
return true
end
end
end
return false
end
------------------------------------------------------------
---- GameEventHooks
------------------------------------------------------------
if IsCivInPlay(iCiv) then
GameEvents.PlayerAdoptPolicy.Add(UpdateMentalPolicy);
GameEvents.PlayerDoTurn.Add(UpdateMentalPolicy);
end
print("Mentals Lua: Hooks are loaded!")
print("Mentals Lua is loaded and ready!")
Relevent portions of my file Lua.log:
- From Game Loading:
Code:
[1343298.406] MentalLua: Started Mental script [1343298.406] MentalLua: Mentals Lua: GetStrongestMilitaryUnitStrengthM [1343298.421] MentalLua: Mentals Lua: MentalStrength [1343298.421] MentalLua: Mentals Lua: UpdateMentalPolicy [1343298.421] MentalLua: Mentals Lua: Hooks are loaded! [1343298.421] MentalLua: Mentals Lua is loaded and ready!
- After using IGE to give me enough culture to both open a policy-branch and select a policy from within the policy-branch:
Code:
[1343409.015] MentalLua: Testing [1343409.015] MentalLua: Mental has 2 policies
- The free floater unit had ist combat power changed to '5' from '4' (rouding up of 4 * 1.2 = 4.8)
----------------------------------------------
There is a slight goof in the code for the player notification in that the argument position for the 1st "-1" is actually meant for the text string for the general description of the notification message, such as "Free Unit" or something.:
Code:
local sLongMessage = " Message message message message to tell us [COLOR_NEGATIVE_TEXT]What Bad Thing Has Happened[ENDCOLOR]"
local sShortMessage = "[COLOR_POSITIVE_TEXT]We Have A Message![ENDCOLOR]"
pPlayer:AddNotification(NotificationTypes["NOTIFICATION_GENERIC"], sLongMessage, sShortMessage)
------------------------------------------------
The process I use to test whether a PlayerAdoptPolicy listener is firing correctly is to give myself a bunch of culture in IGE, then close out IGE, then use the normal selection pop-up menu to select a policy.
-------------------------------------------------
Also, GameEvents.PlayerAdoptPolicyBranch(PlayerID, PolicyBranchID) fires when you adopt a policy branch, but it does not fire when you select an ideology in BNW.