Persistent data problem again, I think

Craig_Sutter

Deity
Joined
Aug 13, 2002
Messages
2,773
Location
Calgary, Canada
I'm working on a trait in which having a city state ally gives bonuses to Golden Age progress or length.

The code works, sort of, but it seems to be firing off multiple times. The first turn, it fires off once, and then the following turn, runs many times.

I am using Modding.OpenSaveData() to save the results as true or false each turn... false (there is no current golden age) and a bonus is added to Golden Age progress. ...true, a Golden Age is in progress and a time bonus will be granted once.

The problem is that the function in red below, is firing multiple times... The print statement is firing and the bonus is being applied, but more than once in the turn. I have not even gotten around to testing the green part due to this problem.

The code below is a much modified version of "Not Another City States Mod" by Iceco.

Code:
-- Lua Script2
-- Author: Craig and Nancy
-- DateCreated: 11/19/2012 11:54:27 AM
--------------------------------------------------------------
-- Not Another City States Mod 
--   NewTraitsEffects.lua
--   Author: Iceco
--[[ Gives the bonuses granted by the new minor civ Traits. ]]
--------------------------------------------------------------
--------------------------------------------------------------
-- Main function determining what bonuses to apply and gathering the necessary info.
--------------------------------------------------------------

local modData = Modding.OpenSaveData()
local modGoldenAgeKey = "GoldenAge"
local haveGoldenAge = (modData.GetValue(modGoldenAgeKey) == 1)

-- Sets up Free Imperial Cities and Holy Roman Empire
-- Determines if FIC is alive
-- no Golden Age adds bonus to progress and sets mod data to false
-- if Golden Age sets mod data to true and increases Golden Age length

function MinorCivFriendshipEffects()

local FIC
local iFIC
local HRE
local iHRE

-- find FIC

	for iPlayer=GameDefines.MAX_MAJOR_CIVS, GameDefines.MAX_CIV_PLAYERS-1, 1 do  

	local pFIC = Players[iPlayer]

		if (GameInfo.MinorCivilizations.MINOR_CIV_FREE_IMPERIAL_CITIES.ID == pFIC:GetMinorCivType()) then
	
		FIC = pFIC
		iFIC = iPlayer
		
		end	
	end

--Set up HRE

	for iPlayer=0, GameDefines.MAX_MAJOR_CIVS-1 do

	local pHRE = Players[iPlayer]

   		if (GameInfo.Civilizations.CIVILIZATION_PERSIA.ID == pHRE:GetCivilizationType()) then
	
		HRE = pHRE
		iHRE = iPlayer

		end
	end
	
-- Set up bonus
	
	if ( FIC:IsAlive () ) then
		
		local CityBonus = FIC:GetNumCities()		
		local GoldenAgeProgressBonus = CityBonus + (HRE:GetCurrentEra() * CityBonus )
		local IsGoldenAge = HRE:IsGoldenAge()

		[COLOR="Red"]if not IsGoldenAge then

			HRE:ChangeGoldenAgeProgressMeter( GoldenAgeProgressBonus )
			haveGoldenAge = false
			modData.SetValue(modGoldenAgeKey, 1)
			print (HRE:GetName() ,"...is getting Golden Age bonuses from... ", FIC:GetName());
[/COLOR]
		[COLOR="Green"]elseif IsGoldenAge then

			if (haveGoldenAge == false) then

				HRE:ChangeGoldenAgeTurns( 1 )
				haveGoldenAge = true
				modData.SetValue(modGoldenAgeKey, 1)
				print (HRE:GetName() ,"...is has increased Golden Age length increased from... ", FIC:GetName());

			end[/COLOR]

		return
		end
	
	end
	
end

-- Determines that MinorCivFriendshipEffects() should be called at a player's (both human or AI) turn.
GameEvents.PlayerDoTurn.Add( MinorCivFriendshipEffects )

I always get stumped on this persistent data stuff because I am not certain what is really going on. I'm assuming it stores some value(s), in this case true/false, that can be called up and changed... perhaps many such, and these can be used as determinants in if statements and such, and changed so the if statements provide different results depending upon the variable stored.

If that's the case, then the function ought to fire once and set to false and end if no golden age, or fire once and reset to true and end if a golden age. But the function reiterates.

Any help would be useful.
 
GameEvents.PlayerDoTurn is called once for every player each turn, so your function will be called multiple times (36 times if you have 12 civs and 24 CS on the map).
 
Back
Top Bottom