LUA to change database TEXT at game start

sman1975

Emperor
Joined
Aug 27, 2016
Messages
1,370
Location
Dallas, TX
Hello,

Have been combing the boards looking for a way to change TXT key data values using LUA. I've seen some people saying the localization data base with the text values is not cached at game start, which sounds promising.

I am working on a mod with several maps, each covering a ~3 year period of time from 1799 AD to 1815 AD. Being the lazy slob I am, I'm trying to use a single mod to define the player civs and minor civs for all of these maps.

Due to the historical nature of the minor civs changing names from the time frame covered in the first map to the time frame covered in the last map, some of the minor civs need a different historical name and adjective on different maps.

For example, on the first map, a city state has a description of "Tuscany", adjective of "Tuscan". On the next map, the same city state needs to use a description of "Eturia", adjective of "Eturian".

I've experimented and can't seem to find the correct Event and/or Method to use to get this to work. Appreciate it if someone can tell me if this is at least possible, and if so which LUA construct I need to use to get it working. I promise shared designer credit on the mod! ;)

Thanks again,

sman
 
I find it easier to use Google to search the site. Put your query in Google and add site:civfanatics.com to the end.

With this method, I found this thread right away, which gave a couple of methods: DB.Query() or Modding.PerformActions().
DB.Query adapted from lemmy101's example in post #7.
Code:
for result in DB.Query("UPDATE Language_en_US SET Text = 'Eturia' WHERE Tag = 'TXT_KEY_CIV5_TUSCANY_TEXT';") do
-- the query doesn't happen until the for loop tries to iterate the first result even if there aren't any.
end

However, for your use-case, I would recommend using Modding.PerformActions instead, i.e., using XML or SQL files that aren't executed until called by your Lua function--instead of "OnModActivated", use a custom name:
  1. Create a custom set in your ModProperties->Actions (Eg. MyAction, UpdateDatabase, MyRules.xml)
  2. Execute this set with command: Modding.PerformActions("MyAction")
 
Mr. Nutty - thanks for the tip on google - will add that one to my notes - will save a ton of time sorting through chaff...

I'm trying to build what you've suggested, but haven't got it working yet. Here's what I've done so far:

- Created a sql file with these statements:
Code:
UPDATE Language_en_US SET Text = 'Kingdom of Italy' WHERE Tag = 'TXT_KEY_CITYSTATE_MILAN'
UPDATE Language_en_US SET Text = 'Italian' WHERE Tag = 'TXT_KEY_CITYSTATE_MILAN_ADJ'

- Created the custom set named "Update1803" that updates the database with the new sql file - took a few seconds to figure out how to do this, but now it shows up on the ModProperties->Actions in a separate section under the OnModActivated, so it looks right.

- Updated the lua script - in my OnGameInit function, adding this command:
Code:
Modding.PerformActions("Update1803")

No errors in the lua console, so I know we reach the function that uses the command. Unfortunately, the name changes aren't happening.

The OnGameInit function is called using this code:
Code:
Events.SequenceGameInitComplete.Add( OnGameInit )

Is there a different Event I should attach it to?

Thanks!
 
To be clear, I've never actually tried using this method myself, but it seems like everything is set up right.

However, one problem is definitely the fact that you're missing the semicolons at the end of your SQL statements...
 
Understood - thanks for the advice. Corrected the sql file, but still same behavior.

I researched the API, and noticed the only use of the Modding.PerformActions is documented in the LoadTutorial.lua file. It fires off the "Start Button" call back:
Code:
Controls.StartButton:RegisterCallback( Mouse.eLClick, OnStart );

Will have to play around with that - I'm thinking that using SequenceGameInitComplete might be too late in the start up process, and maybe tying to an earlier Event could help...

Thanks again! Hopefully I can get this wrapped up in order to show off some of your good artwork in an epic Napoleonic scenario!
 
And you can always go back to DB.Query ...

I take very little credit for those reskins, but that sounds like a great project. Good luck!

EDIT: Did you check in a SQLite browser to see if the database actually gets updated?
 
@Nutty,

Tried the DB.Query with this code:
Code:
local result

for result in DB.Query("UPDATE Language_en_US SET Text = 'Kingdom of Italy' WHERE Tag = 'TXT_KEY_CITYSTATE_MILAN';") do
        -- the query doesn't happen until the for loop tries to iterate the first result even if there aren't any.
end

for result in DB.Query("UPDATE Language_en_US SET Text = 'Italian' WHERE Tag = 'TXT_KEY_CITYSTATE_MILAN_ADJ';") do
        -- the query doesn't happen until the for loop tries to iterate the first result even if there aren't any.
end

local name = Locale.ConvertTextKey("TXT_KEY_CITYSTATE_MILAN")
print("Civ Name now is [" .. name .. "]")

The result of the last print statement shows the same name as before, "Ligurian Republic" - no changes on the map, either. The struggle continues....

Uh, SQLite Browser to read text keys? I didn't know there was such a thing. I use SQLite constantly, but can only see what's in the normal tables - in this case, the TXT_KEY didn't change, only the value so I didn't see if the code worked or not from SQLite. I haven't figured out how to resolve those keys using SQLite - hence my code tends to be littered with print statements....

Your artwork is quite nice - fits well with the scenario which is basically finished. One mod using 7 different maps to cover the War of the Second Coalition thru the War of the Seventh Coalition, with an 1812 Invasion of Russia map added for fun. Am working off the last few items on the gig sheet now, but this city state renamer problem is the only one I'm not sure how to fix. The alternative is create a different city state for each possible instantiation of the minor civs for all map sheets, but.... with the maps already completed, I've gone through great pains to get the city states appearing in alphabetical order in the diplomacy list. Adding a new minor civ in Worldbuilder pushes that civ to the bottom of the list regardless of name, which kind of makes things hard to find - which is a problem with 20-30 city states in the game. Adding the city state in at the correct place means deleting all the minor civs below it alphabetically and recreating them after the civ is added - meaning all the cities need to be recreated, and units, culture borders, buildings, alliances, influences, etc. - for all of those deleted civs. Ugh. If only Worldbuilder moved minor civs like it does player civs....

I also wanted to play with using "tactical" maps - key off on the game speed = quick to change the top panel turns to minutes instead of weeks, and see if I can re-run Trafalgar, Waterloo, Austerlitz, etc. Won't take long to test, if I ever get there...

The last major problem will be redoing the cityview.lua script production queue. The mod has 10 different infantry units, 6 cannon, and 5 cavalry. They're strewn out all over the list, making it hard to find the right unit, especially if not quite familiar with the unit names. Oh well, something for another post...

Thanks again, man, you're the greatest!
 
Update - it's working now... As one would expect JFD has already solved the problem...

https://forums.civfanatics.com/thre...ough-sql-still-possible.535966/#post-13511608

Basically, you have to "bounce" each change with the following statement:
Code:
Locale.SetCurrentLanguage( Locale.GetCurrentLanguage().Type )

Doesn't seem to take multiple reassignments - have to reset current language for each text key change.

Thanks everyone for the assistance!
 
I'm glad you got it working. Your mod sounds very interesting (and ambitious) indeed.

As far as viewing game text in a SQLite browser, just open the Localization-Merged.db, and you'll find Language_en_US is actually a View, not a table, but it works similarly.
 
Thanks again for a great tip - didn't realize it was so easy to examine text. Wrapping up the mod now, getting the unit production list resorted proved easier than expected. Now.... The dreaded game manual.... :cry:
 
Top Bottom