Can you mod the DB from a lua file?

Gedrin

Chieftain
Joined
Mar 27, 2008
Messages
52
XML and SQL are great if you know the value to put in the DB at boot time.

What if you want to update it at runtime?
I asked this over in http://forums.2kgames.com/forums/showthread.php?p=1199212#post1199212 too.

I am trying to make a mod to provide incremental ranges to the city range attacks based on known techs. I have implemented this by setting:

<Defines>
<Update>
<Where Name="CITY_ATTACK_RANGE" />
<Set Value="4" />
</Update>
</Defines>

and then place further restrict it to range 1-4 based on known techs.

I have it working great... for the player. The AI however does not seem to run though the same code [in WorlView.lua InterfaceModeMessageHandler[InterfaceModeTypes.INTERFACEMODE_CITY_RANGE_ATTACK][MouseEvents.LButtonUp] to be very specifc]

The AI's restriction is only CITY_ATTACK_RANGE == 4;

So I am back to wishing I could alter the DB at runtime... specifically at the beginning of everyone's turn, human and AI player alike.

I have the will, is there a way?
 
Yeah, from lua you can call: DB.Query( <your sql query string> );
However, I believe that the parts of the DB that are meant to be static may be reloaded from XML without notice... at least, people have found their changes to files reflected in games without reloading...
 
Bah well if inspecting the DebugDB at runtime is to be believed:

DB.Query("UPDATE Defines SET Value = 4 WHERE Name = 'CITY_ATTACK_RANGE'");

does nothing... either that or NewTurn.lua function OnTurnStart () is never fired.
But even if it did... I altered the DB at runtime... unsurprisingly the core DLL seems to cache this seemingly constant value. In fact it seems to cache it when the mods are all loaded since manually altering the DB and then loading the game did not pick up the value.

I think I am humped until DLL modding is avail... just thought you all might want to know.
For now... I am shelving the Incremental City Ranges based on known techs... since I can only get it to apply to the human players and the AI is always getting the max ever possible :(
 
you need to iterate it to get it to work:

Code:
for result in DB.Query("UPDATE Defines SET Value = 4 WHERE Name = 'CITY_ATTACK_RANGE'") do
 -- the query doesn't happen until the for loop tries to iterate the first result even if there aren't any.
end

Also bear in mind that it's possible this will do no good if these defines are cached in the DLL on game load / start. Certainly the case for techs / buildings / units costs and no doubt other values.
 
doing any sort of writing to the db seems impossible with db.query once a game is started. ive tried all sorts of stuff and they never got written.

the only thing that might work is if the modding class is available to lua scripts and then if db changes can be applied to a live game with Modding.EnableMod or something like that. but without file i/o it's probably gona make for some messy solutions
 
You can update the DebugDatabase using Lua at any point using this method:
  1. Create a custom set in your ModProperties->Actions (Eg. MyAction, UpdateDatabase, MyRules.xml)
  2. Execute this set with command: Modding.PerformActions("MyAction")
It does update the database, but it still doesn't seem to have any effect on the actual game. Maybe if there was a command to reload that database, as it does when the game begins, then this would be useful?
 
thanx, that's interesting... when you say "actual game" you mean civ5 launched, or after loaded/started a new game ? I mean can we change the DB in pregame to be used ingame ?

well, I must test that...
 
Actual game = after loaded/started a new game

Seems that you can update the game on-the-fly with data from Civ5LocalizationDatabase.db, but not from Civ5DebugDatabase.db.

It might be possible to use this reference tag to connect these two together? Perhaps even create a custom table in LocalizationDB and refer all the values you wish to update in DebugDB to that table? :scan:
Code:
<Column name="Description" type="text" reference="Language_en_US(Tag)"/>
 
you need to iterate it to get it to work:

Code:
for result in DB.Query("UPDATE Defines SET Value = 4 WHERE Name = 'CITY_ATTACK_RANGE'") do
 -- the query doesn't happen until the for loop tries to iterate the first result even if there aren't any.
end

Also bear in mind that it's possible this will do no good if these defines are cached in the DLL on game load / start. Certainly the case for techs / buildings / units costs and no doubt other values.

That worked to execute the query and as you said it worked for the text, but not to the update of the tecnology. so i could not update the database to disable tech agreeiment, i have to put the update in the phylosofy tech in the xml that go to the mod, i cannot put as an option for the user to choose as i cannot update the tech on the run
 
I even tried to change it in the memory like this

GameInfo["Technologies"]["TECH_PHILOSOPHY"]["ResearchAgreementTradingAllowed"] = true;

it changed the value if i query this, but does not re-enable the tech agreement (i disabled by default by xml in the mod)
 
Back
Top Bottom