Help with setting up MaxBuildingCount parameter with the Building Library
I've added a new variable in my build_structure files: "MaxBuildingCount". The idea is to allow only a maximum number of that particular structure to be built and it can be set to whatever integer is desired (though all of mine is currently set to 1). Here is an example of how one of the KV files look like.
In my builder.lua I have this if check before any of the construction or preconstruction even begins:
if HasReachedMaxBuildingLimit(player, ability_name, building_name) then SendErrorMessage(playerID, "#error_max_building_count") return end
And this is the HasReachedMaxBuildingLimit script that is called:
function HasBuiltUltimateTower(player) local hero = player:GetAssignedHero() for _,building in pairs(hero.structures) do if building and IsValidEntity(building) and building:GetUnitLabel() == "UltimateTower" then return true end end return false end
It works perfectly if the structure is already built it doesn't seem to work if the structure hasn't been built and I spam the ghost models.
I've tried duplicating the if statement script to within the OnPreConstruction function but that did not help either. Here's the script I tried putting in there (slotted it on line 95):
-- If max count reached, stop if HasReachedMaxBuildingLimit(player, ability_name, building_name) then SendErrorMessage(playerID, "#test") --print("MaxBuildingCount limit reached! Aborting build command.") return false end
The builder.lua file can be previewed here.
Any thoughts on how I can fix it?