Externalizing tables from Lua to XML

Thalassicus

Bytes and Nibblers
Joined
Nov 9, 2005
Messages
11,057
Location
Texas
I want to move a table from lua to xml, but I'm somewhat confused how to use loadstring to convert a string to lua code.

The lua table looks like this:
PHP:
for pBuildingInfo in GameInfo.Buildings() do
  stats.Buildings[pBuildingInfo.ID] = {
    ...
    {"SpecialistType"         , pBuildingInfo.SpecialistType },
    {"GreatGeneralRateChange" , pBuildingInfo.GreatGeneralRateChange },
    ...
  }

I moved it to xml like so:
Code:
<Table name="BuildingStats">
  <Column name="Order" type="integer" primarykey="true" autoincrement="true"/>
  <Column name="Type" type="text" notnull="true" unique="true"/>
  <Column name="Value" type="text" />
</Table>
<BuildingStats>
  ...
  <Row><Type>SpecialistType</Type>         <Value>pBuildingInfo.SpecialistType</Value></Row>
  <Row><Type>GreatGeneralRateChange</Type> <Value>pBuildingInfo.GreatGeneralRateChange</Value></Row>
  ...
</BuildingStats>
I know the basic algorithm is something like:
PHP:
for pBuildingInfo in GameInfo.Buildings() do
  for row in GameInfo.BuildingStats() do
    stats.Buildings[row.Order] = {row.Type, loadstring(row.value)}
  end
What's the proper syntax to do the loadstring part? From looking at the examples I can tell I'm missing something, but I'm not sure how exactly to structure it.
 
PHP:
assert(loadstring("return " .. row.Value))()

I managed to figure it out. The correct syntax is above, and global variable is needed for the table entries to access. Here it is in context:

PHP:
data_BuildingStats = nil

for pBuildingInfo in GameInfo.Buildings() do
  local iBuildingID = pBuildingInfo.ID
  data_BuildingStats = pBuildingInfo
  stats.Buildings[iBuildingID] = {}
  for row in GameInfo.BuildingStats() do
    stats.Buildings[iBuildingID][row.Order] = {row.Type, assert(loadstring("return " .. row.Value))()}
  end
  for k,v in ipairs(stats.Buildings[iBuildingID]) do
    if v == nil or v == 0 or v == "" then
      v = nil
    else
      if type(v[2]) == "function" then
        v[3] = Game.GetDefaultBuildingStatText
      else
        v[3], v[4] = Game.GetDefaultBuildingStatText(iBuildingID, v[1], v[2])
      end
    end
  end
end

Excerpt from the table:
Code:
<Table name="BuildingStats">
  <Column name="Order" type="integer" primarykey="true" autoincrement="true"/>
  <Column name="Type" type="text" notnull="true" unique="true"/>
  <Column name="Value" type="text" />
</Table>
<BuildingStats>
  ...other entries...
  <Row><Type>UnmoddedHappiness</Type>                  <Value>Game.GetDefaultBuildingStatData</Value></Row>
  <Row><Type>Happiness</Type>                          <Value>Game.GetDefaultBuildingStatData</Value></Row>
  <Row><Type>HappinessPerCity</Type>                   <Value>data_BuildingStats.HappinessPerCity</Value></Row>
  <Row><Type>HappinessPerXPolicies</Type>              <Value>data_BuildingStats.HappinessPerXPolicies</Value></Row>
  <Row><Type>UnhappinessModifier</Type>                <Value>data_BuildingStats.UnhappinessModifier</Value>        </Row>
  <Row><Type>NoOccupiedUnhappiness</Type>              <Value>data_BuildingStats.NoOccupiedUnhappinessFixed</Value></Row>
  <Row><Type>Experience</Type>                         <Value>data_BuildingStats.Experience</Value></Row>
  <Row><Type>ExperienceLand</Type>                     <Value>GetValue("Experience", {BuildingType=data_BuildingStats.Type, DomainType="DOMAIN_LAND"}, GameInfo.Building_DomainFreeExperiences)</Value></Row>
  <Row><Type>ExperienceSea</Type>                      <Value>GetValue("Experience", {BuildingType=data_BuildingStats.Type, DomainType="DOMAIN_SEA"}, GameInfo.Building_DomainFreeExperiences)</Value></Row>
  <Row><Type>ExperienceAir</Type>                      <Value>GetValue("Experience", {BuildingType=data_BuildingStats.Type, DomainType="DOMAIN_AIR"}, GameInfo.Building_DomainFreeExperiences)</Value></Row>
  <Row><Type>ExperienceCombat</Type>                   <Value>HasValue({BuildingType=data_BuildingStats.Type}, GameInfo.Building_UnitCombatFreeExperiences)</Value></Row>
  ...other entries...
</BuildingStats>
 
Back
Top Bottom