Need help detecting change in game speed from other mod

TofuSojo

Chieftain
Joined
Sep 28, 2014
Messages
41
Location
Orlando, FL
I just made a mod that gives Nuclear Plants a chance to meltdown. The chance is based on the gamespeed's GrowthPercent variable, so if another mod changes that variable (such as the popular Superquick mod, which on Quick changes it from 66 to 33) my mod should use the updated number. Naturally, it ignores the other mod's change for some reason. I did make sure to enable the other mod first.

Here's my mod's lua:

Code:
--0 is Marathon 300%, 1 is Epic 150%, 2 is Standard 100%, and 3 is Quick 66%
local iGameSpeed = GameInfo.GameSpeeds[Game.GetGameSpeedType()].GrowthPercent / 100
local MeltdownChance = 1 / iGameSpeed --x1  0: 0.33%, 1: 0.66%, 2: 1.00%, 3: 1.50%
--print("MeltdownChance: " .. MeltdownChance)

function CheckNuclearPlants(PlayerID)
	local pPlayer = Players[PlayerID]
	local iNuclearPlant = GameInfoTypes["BUILDING_NUCLEAR_PLANT"]
	local iFallout = GameInfoTypes["FEATURE_FALLOUT"]
	local directions = {DirectionTypes.DIRECTION_NORTHEAST, DirectionTypes.DIRECTION_EAST, DirectionTypes.DIRECTION_SOUTHEAST, DirectionTypes.DIRECTION_SOUTHWEST, DirectionTypes.DIRECTION_WEST, DirectionTypes.DIRECTION_NORTHWEST}

	for pCity in pPlayer:Cities() do
		local sCityName = pCity:GetName()
		local pCityPlot = pCity:Plot()
		local iCityX = pCity:GetX()
		local iCityY = pCity:GetY()
		local sText = "Nuclear Plant in " .. sCityName .. " destroyed!"
		local sHeading = "Nuclear Plant Meltdown"

		if pCity:IsHasBuilding(iNuclearPlant) and (math.random(1,100)  <= MeltdownChance) then
			pCity:SetNumRealBuilding(iNuclearPlant, 0)
			--print("Nuclear Plant removed from: " .. sCityName)
			for loop, direction in ipairs(directions) do
				local pPlot = Map.PlotDirection(pCityPlot:GetX(), pCityPlot:GetY(), direction)

				if not pPlot:IsWater() and (math.random(1,100)  <= 50) then
					pPlot:SetFeatureType(iFallout)
					--print("Spawning Fallout near: " .. sCityName)
				end
			end
			--print("A Nuclear Plant has had a meltdown in: " .. sCityName)
			pPlayer:AddNotification(NotificationTypes.NOTIFICATION_GENERIC, sText, sHeading, iCityX, iCityY, false, false, false )
		end
	end
end
GameEvents.PlayerDoTurn.Add(CheckNuclearPlants)

Any help sorted this out would be appreciated. Thanks
 
Well, with whoward's help I added an lua variable modifier based on Game Speed using this statement in lua:
Code:
iTurnIncrement = math.ceil(iTurnIncrement * GameInfo.GameSpeeds[[COLOR="blue"]PreGame[/COLOR].GetGameSpeed()].TrainPercent / 100)
I was using TrainPercent because I was making a wonder pop a free unit every 10 turns on Standard, and needed to adjust that iTurnIncrement value of 10 based on a player's chosen game speed. The only essential difference between what whoward advised I use, and what you are using is that I used PreGame where you are using Game and that I was interested in TrainPercent instead of GrowthPercent.
 
Same results. Makes me wonder if InGameUIAddin Lua scripts run before the database is updated by xml mods, which is what the Superquick mod is...if that is the case, I don't think this is possible. I would have to detect the other mod's presense, assume what value it used for GrowthPercent, and have my mod use that value for Quick speed.

I assume a mod's ID stays the same even after being updated?
 
Well, I got it to work using a function that checks to see if the other mod is enabled, thus:

Code:
function IsSuperQuickEnabled() 
	--print("IsSuperQuickEnabled running...")
	local SuperQuickModID = "e3699836-4ca9-405b-8b20-232a05ea8bf6"

	for _, mod in pairs(Modding.GetActivatedMods()) do
		if (mod.ID == SuperQuickModID) then
			return true
		end
	end
	return false
end

--0 is Marathon 300%, 1 is Epic 150%, 2 is Standard 100%, and 3 is Quick 66%
local iGameSpeed = GameInfo.GameSpeeds[PreGame.GetGameSpeed()].GrowthPercent / 100
local MeltdownChance = 1 / iGameSpeed --x1  0: 0.33%, 1: 0.66%, 2: 1.00%, 3: 1.50%
                                      --x3  0: 1.00%, 1: 2.00%, 2: 3.00%, 3: 4.50%

--print("MeltdownChance before: " .. MeltdownChance)
if IsSuperQuickEnabled() then
	MeltdownChance = 3 -- Assumes SuperQuick's GrowthPercent is still 33 instead of 66
end
--print("MeltdownChance after: " .. MeltdownChance)

Learned the function from one of Pazyryk's posts I think. Hope it may be helpful to others.
 
I think the other difference in my usage and yours is that the line I am using is inside a PlayerDoTurn function, which means that all mods have loaded by the time the line I quoted in my post is executed in lua.

You might try writing a simple function like
Code:
 [COLOR="Blue"]local iGameSpeed = 0
local MeltdownChance = 0[/COLOR]

function GameSpeeds()
	iGameSpeed = GameInfo.GameSpeeds[Game.GetGameSpeedType()].GrowthPercent / 100
	MeltdownChance = 1 / iGameSpeed --x1  0: 0.33%, 1: 0.66%, 2: 1.00%, 3: 1.50%
	print("MeltdownChance: " .. MeltdownChance)
end
Events.LoadScreenClose.Add(GameSpeeds)
Where you simply initialize the two variables shown in blue at the loading of the lua, and the GameSpeeds() function updates the values after the player clicks the load screen close pushbutton. This method might get you past the mod loading order issues and the issues as to which lines in the lua are going to run before all the mods take effect, and which lines after.

There might also be a better event to hook into rather than Events.LoadScreenClose.
 
Top Bottom