Is it possible to add a new XML row from within Lua?

General Tso

Panzer General
Joined
Oct 12, 2007
Messages
1,548
Location
U. S. of A.
I would like to add one or more rows to the following table. Here's how it's defined in Civ5Civilizations.xml.

Code:
	<Civilization_Start_Along_Ocean>
		<Row>
			<CivilizationType>CIVILIZATION_ENGLAND</CivilizationType>
			<StartAlongOcean>true</StartAlongOcean>
		</Row>
		<Row>
			<CivilizationType>CIVILIZATION_OTTOMAN</CivilizationType>
			<StartAlongOcean>true</StartAlongOcean>
		</Row>
	</Civilization_Start_Along_Ocean>

Is it possible to add a new row in Lua? I assume I would be using DB.Query() but everything I tried gives me a query error. What do I need to do?

I think I can get around the DB cache problem because I'm setting the values in the front end (Advanced Setup screen) and they are being used in PreGame when the map is being created. I've done something similar where I updated existing rows and it worked fine. I just can't figure out how to create new rows.
 
Never mind - I think I found the answer.

This works:
Code:
for row in GameInfo.Civilizations() do
  for _ in DB.Query(string.format("INSERT INTO Civilization_Start_Along_Ocean(CivilizationType, StartAlongOcean) VALUES(%q, %q)", row.Type, "true")) do
  end 
end

This doesn't work:
Code:
for row in GameInfo.Civilizations() do
  for _ in DB.Query(string.format("INSERT INTO Civilization_Start_Along_Ocean(CivilizationType, StartAlongOcean) VALUES(%s, %s)", row.Type, "true")) do
  end
end

The lua %s formatter was adding something (I think quotes).
 
While that will insert into the game database, the game core will not recognise the changes as it caches the data during start-up (ie before your Lua runs). This has been asked recently, there is another thread with examples of the situations you can expect it to work in (basically only from direct DB access within Lua) and those it won't (any that use GameInfo or GameInfoTypes)

Edit: In fact you asked the question! See post #12 of http://forums.civfanatics.com/showthread.php?t=486528
 
I didn't understand the responses in that thread back when it happened so I just worked around it.

Thanks for clearing it up - I now see how it works - DB access from Lua changes - GameInfo doesn't.

So it looks like the only way I can change a player's starting bias is by modifying the MapmakerUtilities.lua file and I'm not sure if that's a good idea. Maybe I'll try it and see what happens.
 
While that will insert into the game database, the game core will not recognise the changes as it caches the data during start-up (ie before your Lua runs). This has been asked recently, there is another thread with examples of the situations you can expect it to work in (basically only from direct DB access within Lua) and those it won't (any that use GameInfo or GameInfoTypes)

Edit: In fact you asked the question! See post #12 of http://forums.civfanatics.com/showthread.php?t=486528

I haven't tested all table, but I do know that some are not yet cached when you are in the setup screen (after the mod screen / before the game is launched), or if they are already cached, they are taken again from the DB when the game is launched.

I'm using this in YnAEMP to allow custom resource rules that are map dependent: I change the value in 2 or 3 Resources table in the DB when the launch button is pressed in the custom setup screen, and they are used when the game is loading (WB maps don't take resource placement from assignstartingplots)

I know it also works for MinorCivilization tables, and I was also planning to modify the Civilizations table to restrict the use of some civs for the random AI (AIPlayable tags) during setup.
 
Back
Top Bottom