LUA - Determine Game Turn a Building was Built?

sman1975

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

Am working on a mod that would like to be able to determine which game turn a building in a city was completed.

I've looked at the pCity:GetBuildingOriginalTime(Building Type) method, which returns the "year" the building was completed, e.g. "-4000" for the Palace in a game starting in the Ancient Era, etc.

Have tested with other buildings for several turns, and this seems to be the way the method works - returning an integer "year" when the building was built.

I haven't found any method that returns the exact game turn a building was created, or a way of "converting" a year into a game turn.

I suppose I could calculate the approximate turn, using the game speed - but it gets a little fuzzy if a different mod introduces a new game speed.

Anyone have any suggestions on how to a game turn for building creation?

Thanks!
 
You could always hook up a function to GameEvents.CityConstructed(playerID, cityID, buildingType, bIncludeGold, bIncludeFaithOrCulture) and use that to keep a record of what turn a building was built on (it will not catch buildings added via InGame Editor or City:SetNumRealBuilding, however). This is probably the easiest way, unless there's another method I haven't heard of. You'll just need a way of saving the info you've collected whenever the player quits the game and loads it back up.
 
Thanks, @marius, but the reason I wanted to grab the game turn was so I could hack a dummy building to keep track of a game turn event. This would allow me to avoid using a table save/loader function for a trivial piece of data in case of save games.

In this example, I'm using a dummy building to represent expanded capabilities for various forms of government (Monarchy, Democracy, Communism, etc.). The dummy buildings will have a few capabilities that will add to a civ while the government is in effect. Kind of a like a "super wonder."

Occasionally, it will be allowed to change the form of government, and also change these dummy buildings (destroy the old one, add the new one) - with a few turns of anarchy thrown in for fun.

I was interested in keeping track of when these dummy buildings were built, so I could prevent the change of government from occurring too frequently - say, no sooner than every 50 turns or so.

Requiring the table saver for 10-12 bytes of data seems like overkill.
 
I see. I wasn't sure if you wanted a specific building or not, but since it is, then you can add a dummy building for the dummy building. For instance, when the player first adopts a new form of government, stack 50 'cool down' dummy buildings and use PlayerDoTurn to remove them one by one. You simply have to check if the cool down building is present to see if the player is eligible to change governments, although you run the risk of the city being captured and will need a few extra checks for handling that (or you could just allow the player to select a new gov't right off the bat, which I like the sound of honestly).
 
You'll still have to deal with gamespeed issues, since 50 turns on Standard is 150 on Marathon, but only 34 on Quick. But you can grab the game speed being used pretty easily as a modifier value via as an example
Code:
GameInfo.GameSpeeds[PreGame.GetGameSpeed()].TrainPercent / 100
Though you would probably want "ConstructPercent" instead of "TrainPercent"
 
No need to use the TableSaver or jump through hoops with dummy buildings, just use the in-built methods for saving an integer value into the modding db - see any of the scenarios for Modding.OpenSaveData. The hit is so minimal that database performance is a non-issue
 
@whoward69 - by Golly - that works!!!! :worship:

The only (minor) problem I had was with the difficulty working with tables using OpenSaveData, but I noticed the Wonder Scenario (DLC 06) used dynamic variable names, which makes it more or less simple to work around.

Thanks soooooooooo very much for such a great tip!
 
Top Bottom