whoward69
DLL Minion
Tables that have both an ID and a Type column are treated specially by the game core. The contents of such tables are made available to Lua via the GameInfo and GameInfoTypes structures.
This enables us to write code such as
or any of the following, they are all the same, just depends on your preference
rather than a complex query
If we know the ID of the harbor (which I don't, but will use 30 as a guess), we can also write
to get the same value.
We can also iterate all buildings with
In most of our code we can use the Type name (eg BUILDING_HARBOR) for readability, but when working with the Lua API we almost always want the ID, eg
This is a common source of confusion, despite the API method ending with Type, it actually returns an ID
The game developers must have gotten bored with writing GameInfo.{tablename}.{type}.ID all the time as they also provide the GameInfoTypes structure, which returns the ID for any Type, eg
As modders, we can also create our own tables to take advantage of this, eg
(example in XML, but I could have used SQL)
and then use the standard GameInfo structure to access them
Note: The game core is not well behaved if you create a table with both ID and Type columns that are not of database types "integer" and "text" respectively - expect it to crash!
This enables us to write code such as
Code:
local iCost = GameInfo.Buildings.BUILDING_HARBOR.GoldMaintenance
or any of the following, they are all the same, just depends on your preference
Code:
local iCost = GameInfo.Buildings["BUILDING_HARBOR"].GoldMaintenance
local iCost = GameInfo.Buildings["BUILDING_HARBOR"]["GoldMaintenance"]
local iCost = GameInfo.Buildings.BUILDING_HARBOR["GoldMaintenance"]
rather than a complex query
Spoiler :
Code:
local iCost = 0
for row in DB.Query("SELECT GoldMaintenance FROM Buildings WHERE Type='BUILDING_HARBOR'") do
iCost = row.GoldMaintenance
end
If we know the ID of the harbor (which I don't, but will use 30 as a guess), we can also write
Code:
local iCost = GameInfo.Buildings[30].GoldMaintenance
to get the same value.
We can also iterate all buildings with
Code:
for building in GameInfo.Buildings() do
print(building.GoldMaintenance)
end
In most of our code we can use the Type name (eg BUILDING_HARBOR) for readability, but when working with the Lua API we almost always want the ID, eg
Code:
if (pPlot:GetFeatureType() == GameInfo.Features.FEATURE_FOREST.ID) then
-- Do something on a forest plot
end
This is a common source of confusion, despite the API method ending with Type, it actually returns an ID
The game developers must have gotten bored with writing GameInfo.{tablename}.{type}.ID all the time as they also provide the GameInfoTypes structure, which returns the ID for any Type, eg
Code:
if (GameInfo.Features.FEATURE_FOREST.ID == GameInfoTypes.FEATURE_FOREST) then
-- This is ALWAYS true
end
As modders, we can also create our own tables to take advantage of this, eg
Code:
<GameData>
<Table name="Animals">
<Column name="ID" type="integer" primarykey="true" autoincrement="true"/>
<Column name="Type" type="text" notnull="true" unique="true"/>
<Column name="Description" type="text"/>
<!-- Other stuff -->
</Table>
<Animals>
<Row>
<ID>0</ID>
<Type>ANIMAL_WORG</Type>
<Description>TXT_KEY_ANIMAL_WORG_DESC</Description>
<!-- Other stuff -->
</Row>
<Row>
<Type>ANIMAL_DRAGON</Type>
<Description>TXT_KEY_ANIMAL_DRAGON_DESC</Description>
<!-- Other stuff -->
</Row>
</Animals>
</GameData>
(example in XML, but I could have used SQL)
and then use the standard GameInfo structure to access them
Code:
GameInfo.Animals.ANIMAL_WORG.Desc
GameInfo.Animals[1].Desc -- this is the dragon description
GameInfo.Animals[GameInfoTypes.ANIMAL_DRAGON].Desc -- as is this
Note: The game core is not well behaved if you create a table with both ID and Type columns that are not of database types "integer" and "text" respectively - expect it to crash!