Improvement Added Hook (Lua)

OnlyHeStandsHere

Chieftain
Joined
May 4, 2018
Messages
46
Does anyone have any experience with the "Events.ImprovementAddedToMap()" or "Events.ImprovementChanged()" Lua hooks? The game keeps hard-crashing when I try to use them, and I don't know if it's because too many improvements are being checked or if I did something else weird. Is there a separate hook I should be using? Or some other way I should initialize it other than waiting for the loading screen to close?

Here's my code:
Code:
function SwissPastureCreated(iPlotX, iPlotY, iImprovement, iPlayer, iResource, iPillaged, iWorked)
        print("Improvement has Changed")
end
function InitializeSwissPastureCreated()
    Events.ImprovementAddedToMap(SwissPastureCreated)
end
Events.LoadScreenClose.Add(InitializeSwissPastureCreated)
 
You're missing .Add here
Code:
Events.ImprovementAddedToMap(SwissPastureCreated)
 
Wow, I'm honestly impressed with how much I messed that up, haha. I replaced the hook with a different one a couple times, which worked of course, yet somehow never noticed I was forgetting to put the .Add in. Well, thanks for the help!
 
Another question on this topic. So the function I'm trying to make culture bombs adjacent mountain tiles when the custom civ places a pasture. I've gotten the culture bomb part to work, but interestingly the tiles do not become workable. I'm guessing that plots added to a city using the CityManager() skip the step that makes mountain tiles workable, which I've done normally through xml files. Atleast that's my best guess. Is anyone aware of the method I can use to make the new tile workable?
 
Tiles culture-bombed or otherwise given via lua are unworkable to my knowledge unless the
Code:
 WorldBuilder.CityManager():SetPlotOwner(PlotObject, CityObject);
method is used.

Plot:SetOwner(iPlayer, iCityID) does not work -- the plots are never workable.
I wasn't aware of a direct CityManager method for setting plot ownership.Nor one using the Cities "system".
 
I did mean the WorldBuilder.CityManager() method. Sorry for not being clear. Specifically, I used:

Code:
WorldBuilder.CityManager():SetPlotOwner(adjacentPlot:GetX(), adjacentPlot:GetY(), iPlayer, pCityID)

I'll give it a go using just the plot and city variables and see if that gives a different result.

EDIT: Hm... when I try to reference the city it doesn't seem to culture bomb at all. So, maybe my code is just failing to find the proper city to add the tile to? I'm gonna do some testing on my own to see what problem I'm getting.
 
Last edited:
So, interesting development. I tried using both different methods and neither seem to work perfectly.

Code:
WorldBuilder.CityManager():SetPlotOwner(adjacentPlot, pCityID)
With the above, the tiles actually got anti-culture-bombed, where the plots were just taken out of my empire (even one that was next to my city center, which honestly I find kinda funny). I had a print statement double-check the city being put in the variable, and it is the correct city. Still weird though.

Code:
WorldBuilder.CityManager():SetPlotOwner(adjacentPlot:GetX(), adjacentPlot:GetY(), iPlayer, pCityID)
Using the above, the tile is successfully culture-bombed, but remains unworkable. Hovering over the tile does confirm that the city owns the tile, so that info seems to be passing through ok. I'm guessing that it still has to do with the tile not being turned workable, or perhaps it having no yields assigned to it.

EDIT:
civscreen_culturebombmtn.jpg
 
Last edited:
Try it on a non-mountain tile to confirm the method otherwise works correctly. Another thing with the WorldBuilder.---- method is that the UI doesn't update to reflect extra yields coming from modifiers and the like until the game is saved and reloaded (and I think the gamecore does not update for these modifiers either) so you may be running into a fallout effect of that bug with grabbing tiles directly via lua.
 
Hoho, it seems like it was the UI like you mentioned. I saved and reloaded after the tile was culture-bombed and it was workable with all the necessary yields. Is there a way to force update the interface in-game, or is a dead-end kind of problem?
 
It's a dead-end kind of problem so far as I know, Only save and reload will force a "recalculate" because it is a problem not really caused by the UI but rather an incomplete hook-up of the SetPlotOwner() to instruct the gamecore to also update the plot with all the relevant effects of modifiers and what-not -- so only base yields if any show, and alterations that also come from mods don't seem to show either, like making mountain plots workable.
 
Gotcha. Well, it was a fun experiment. Seems like it might be better to simply use the Australia pasture bomb effect, even if that picks up a few extra tiles. Thanks for the help and advice!
 
Top Bottom