How to iterate through all columns in a table?

PawelS

Ancient Druid
Joined
Dec 11, 2003
Messages
2,811
Location
Poland
Iterating through all rows in a table is easy, and used in many places in the game's Lua code:
Code:
for row in GameInfo.TableName () do
  [some code here]
end

But would it be possible to iterate through all columns in a table, and get the names of these columns as text strings? It would be very useful for my resource placement code...
 
Code:
for col in DB.Query("PRAGMA table_info(Worlds)") do
  print(col.cid, col.name, col.type, col.notnull, col.dflt_value, col.pk)
end
 
Great, thanks :) If anyone is interested what I need it for - I'm creating a system of resource placement (when a map is generated) and creation (some great people in my mod can create new resources on the map) that uses a system of 22 "plot categories", depending on the terrain, plot type (flat/hills), and features present on the plot. Every resource has a probability of appearing on plots belonging to every category. And instead of the standard Civ5 approach of creating "secondary tables", which would look like this:
Code:
(...)
<Row>
	<ResourceType>RESOURCE_GOLD</ResourceType>
	<PlotCategory>Plains</PlotCategory>
	<Probability>4</Probability>
</Row>
<Row>
	<ResourceType>RESOURCE_GOLD</ResourceType>
	<PlotCategory>PlainsHill</PlotCategory>
	<Probability>8</Probability>
</Row>
(...)
I prefer to use a different approach, where column names are the same as the plot categories:
Code:
<Row>
	<ResourceType>RESOURCE_GOLD</ResourceType>
	(...)
	<Plains>4</Plains>
	<PlainsHill>8</PlainsHill>
	(...)
</Row>
This way the table is more compact, and is easier to edit using XML editing tools. But then, to avoid hardcoding these categories, the Lua code should read the column names to know where it should place the resources. There is another table that defines the terrain, plot type and feature for every category, so the system is totally adjustable by the database (as opposed to the standard resource placement system in AssignStartingPlots.lua). So, for example, after adding a new resource to the mod, or a new feature where resources can appear, it only needs database changes to make the resource placement use it.
 
Top Bottom