[SOLVED] Changing count in Player Visibility object to remove improvement visibility outside territory

slushfund

Chieftain
Joined
Jan 17, 2025
Messages
3
Hello community,
I'm working on a modded improvement, it can be placed outside territory but I had an issue where the improvement provided vision when it should not have. Initally I asked how I could fix this, but I managed to figure it out on my own, so I will leave my code here as a solution for future modders.

station_placed.lua
-- Initialize the Event Listener for improvement placement
function OnImprovementPlaced(x, y, ImprovementType, playerID)

local pPlot = Map.GetPlot(x, y); --plot obj
local improvementIndex = GameInfo.Improvements["IMPROVEMENT_GAS_STATION"].Index;

if (pPlot:GetImprovementType() == improvementIndex) then
local plotIndex = Map.GetPlot(x, y):GetIndex(); --gets index of tile using x,y
local pv = PlayerVisibilityManager.GetPlayerVisibility(playerID); -- gets player visibility obj from the manager
local count = pv:GetVisibilityCount(plotIndex);
pv:ChangeVisibilityCount(plotIndex, -1); --uses function on pv obj to decrement count
local adjPlots = Map.GetAdjacentPlots(x, y); --generates array of adj tiles to current tile

for i = 1, 6, 1 do --always have 6 adj tiles
local adjIndex = adjPlots[i]:GetIndex(); --getting index of current adj tile
local pv = PlayerVisibilityManager.GetPlayerVisibility(playerID); --getting pv object
local count = pv:GetVisibilityCount(adjIndex);
pv:ChangeVisibilityCount(adjIndex, -1);
end
end
end

-- Hook into the improvement placement event
Events.ImprovementAddedToMap.Add(OnImprovementPlaced);

station_removed.lua
function OnImprovementRemoved(x, y, playerID)

local pPlot = Map.GetPlot(x, y); --plot obj

local plotIndex = Map.GetPlot(x, y):GetIndex(); --gets index of tile using x,y
local pv = PlayerVisibilityManager.GetPlayerVisibility(playerID); -- gets player visibility obj from the manager
local count = pv:GetVisibilityCount(plotIndex);

if(count < 1) then --if tile not visibile
pv:ChangeVisibilityCount(plotIndex, 1); --uses function on pv obj to increment count
end

local adjPlots = Map.GetAdjacentPlots(x, y); --generates array of adj tiles to current tile

for i = 1, 6, 1 do --always have 6 adj tiles
local adjIndex = adjPlots[i]:GetIndex(); --getting index of current adj tile
local pv = PlayerVisibilityManager.GetPlayerVisibility(playerID); --getting pv object
local count = pv:GetVisibilityCount(adjIndex);
if(count < 1) then --if tile not visibile
pv:ChangeVisibilityCount(adjIndex, 1);
end
end
end

-- Hook into the improvement placement event
Events.ImprovementRemovedFromMap.Add(OnImprovementRemoved);

These are some additional threads I was looking at in an attempt to solve it myself


Good day
 
Last edited:
Top Bottom