Storing Plots in Lua

turingmachine

Emperor
Joined
May 4, 2008
Messages
1,438
So I thought I knew how to do this but my attempts have failed miserably.

Very simply I want to store a list of plots of every city I capture. I can very easily get the plot at the end of the city capture event, but I want to keep a list of these plots so I can check them again easily later without checking a whole bunch of other plots.

Any help would be appreciated.

(On a harder note I need to figure out how to tie this to a Save Utility but once step at a time).
 
You need a "global" array of plot (X, Y) pairs (or the index of the plot) do NOT attempt to cache the actual plot object (the thing you get back from Map.GetPlot(iPlotX, iPlotY)) - see http://forums.civfanatics.com/showthread.php?t=508206

While that warning is for city objects, there is nothing to indicate that this is not a general possibility for in-memory Lua objects
 
Interesting, thanks for the help.

Would you know of any mods that do this so I can see an example (I'm still having some trouble)?
 
Here are some examples I use:

Code:
g_Towns = {
-- USSR
--	Moscow Defence Ring
{X = 53, Y = 47, Name = "Klin", Size = 3 },
{X = 51, Y = 45, Name = "Volokolamsk", Size = 2 },
{X = 53, Y = 44, Name = "Mozhaysk", Size = 2 },
{X = 56, Y = 44, Name = "Podolsk", Size = 5 },
{X = 54, Y = 43, Name = "Naro-Fominsk", Size = 3 },
{X = 58, Y = 44, Name = "Kolomna", Size = 7 },
{X = 54, Y = 45, Name = "Odintsovo", Size = 7 },
}

This is an example of the code that uses it:
Code:
function AddTownNames()
	local townStr = ""
	townData = g_Towns
	
	if townData then
		for i, data in ipairs(townData) do
			local townPlot = GetPlot(data.X, data.Y)
			townStr = data.Name
			local instance = g_FlagManager:GetInstance()
			instance.Flag:SetText(" [COLOR_YELLOW] "..townStr.."[ENDCOLOR]")
			instance.Flag:SetFontByName("TwCenMT16")
			instance.Flag:SetToolTipString(townStr)
			PlaceInWorld(instance.Anchor, GetWorldPos(townPlot))
		end
	end
end
Note that this code is triggered within a modified UI game file so requires a few more steps to get g_Town to work.


Second example:
Code:
LOUKHI_PLOT = GetPlot(42,79)
BELOMORSK_PLOT = GetPlot(45,74)
MEDVEZHYEGORSK_PLOT = GetPlot(45,69)

g_Louhki_Medvezhyegorsk_VolkhovRailBranch = {
	[1] = LOUKHI_PLOT,
	[2] = BELOMORSK_PLOT,
	[3] = MEDVEZHYEGORSK_PLOT,

This is an example of the code that uses it:
Code:
function IsRailBranchAvailable(playerID, branchArray)

	local player = Players[playerID]
	local currentPlot
	local nextPlot
	local railRange = CITY_RAIL_LEND_LEASE_PLOT_RANGE

	local bAvailable = true
    
	for n, plot in ipairs(branchArray) do
	    currentPlot = plot
        nextPlot = branchArray[n+1]
        if nextPlot ~= nil then
			if currentPlot:GetOwner() == playerID then
				if not isPlotConnected(player, currentPlot, nextPlot, "Railroad", true, true , PathBlocked, railRange) then
					bAvailable = false
					return bAvailable
				end
			else
				bAvailable = false
				return bAvailable
			end
        end
    end
	
	return bAvailable
end

GetPlot function was created by Gedemon
Code:
function GetPlot (x,y)
--	Here (x,y) = (0,0) is bottom left of map in Worldbuilder.

	local plot = Map:GetPlotXY(y,x)
	if plot then
		return plot
	else
		Dprint("GetPlot: Plot object could not be retrieved for plot coordinates: "..x..","..y)
	end
end
 
do NOT attempt to cache the actual plot object
I have also changed my code following this advice, but it's probably overkill since the game itself stores Lua objects (e.g. the pedia callbacks). The Firaxis warning for not storing the cities is probably because they can have a dynamic life cycle... which plots do not.
 
Back
Top Bottom