Why exactly do you need to track, store, and reset
every single plot on the map every turn?
My Holo Civ makes extensive use of storing and getting plot data, but this sounds overkill, even for me. What are you checking for, and what can possibly change over every single plot?
Here's the logic I use for Holo:
Plots can be acquired via several specific means - Founding a City, Capturing a City, City Expansion, or plopping a Citadel.
This means that, rather than scanning every single plot, I just fire off a function to scan the plots acquired via the aforementioned methods. The owner of these plots shouldn't change unless a City is captured and/or razed, or a Citadel is plopped.
Having said that, and with regard to your specific inquiry about your table issue:
This is because that particular table you're trying to insert stuff into isn't actually a table.
Let's investigate the process Lua has to follow using your current code:
We now have a table referenced as '
tPlayerPlots' as evidenced by the table constructor: {}
Code:
function GetPlayerPlots(iPlayer)
-- reset plots:
tPlayerPlots[iPlayer] = nil;
Okay, so when this function runs, it will "reset" the data within the subtable whose
index value matches
iPlayer. Let's set aside the fact that the very first time this function runs, such a subtable doesn't exist to begin with, because all we've defined so far is this in its entirety:
{}
So, assume we
had data there from a previous turn. We now set that subtable to
nil -- it's now nothing at all, not even a value; It's dead, Jim. However, there is a secondary consequence here that you've overlooked, and is what is causing your error. We'll return to this point in a minute.
Code:
if (pEveryPlot:GetOwner() == iPlayer) then
table.insert(tPlayerPlots[iPlayer], pEveryPlot)
end
Now we get to the meat of the function -- trying to add plots into the table. Unfortunately...
Runtime Error: C:\Users\AW\Documents\My Games\Sid Meier's Civilization 5\MODS\AW's [REDACTED] Mod (v 2)\LUA\PlotOwner_Functions.lua:20: bad argument #1 to 'insert' (table expected, got nil)
As you see, you ran into this error. I've bolded the section that we're interested in.
Why is it saying nil? I thought we defined
tPlayerPlots as a table...
But of course, recall where I stated an issue was overlooked earlier -- You're not trying to insert into
just tPlayerPlots, you want to put stuff into
tPlayerPlots[iPlayer]! Why isn't it working?
Let's look at the above code again..
Code:
tPlayerPlots[iPlayer] = nil;
You used this to reset the data in this table,
but then you forgot to re-initialize it as a new table!
Because of this,
tPlayerPlots[iPlayer] has the value of
nil -- no value at all! This is why Lua complained. You were effectively trying to do
table.insert(nil, data) -- how can Lua put data into nothing?
Here's a quick and dirty example:
Go here to the Lua demo page -- I love this site, as it lets me quickly test isolated chunks of standard Lua code without having to fire up the game, which is excellent for bits of "logic" code such as this.
In the box, paste this:
Code:
t = {}
function a()
t[1] = nil
--t[1] = {}
table.insert(t[1], "hi")
end
a()
print(t[1][1])
This is a simplified version of your function, complete with the error. You should see a similar error output when you hit "Run."
Afterward, uncomment the line I've commented out (this is the line you need to re-initialize the subtable as a table) and hit "Run" again.
Edit:
I should note that there is another method of clearing the table while retaining the
iPlayer index. In addition to simply doing
tPlayerPlots[iPlayer] = {}, you can also iterate through the subtable itself.
I use this method in my TSL Serializer component, but you can iterate through that subtable and assign a value of 'nil' to every result.
Code:
for data in pairs(tPlayerPlots[iPlayer]) do
tPlayerPlots[iPlayer][data] = nil
end
This would clear all the data inside the
tPlayerPlots[iPlayer] table, while leaving that particular reference as a table intact.
However, in your case, the easiest solution I think is to simply replace the assignment of '
nil' with that of a new table constructor. It will break the reference to the "old" data, and Lua will garbage collect it in due time.