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?

Comments

  • edited December 2015 Posts: 1,670

    Inside OnPreConstruction should work. Does the function work? i.e. does it actually get inside, prints, etc

    The concept of Modding Community doesn't go well together with Competitive Business
    My Project Page || My GitHub Profile ||

  • Noya, I just tested it with the prints. This is the script:

            print("precheck")
            if HasReachedMaxBuildingLimit(player, ability_name, building_name) then
                SendErrorMessage(playerID, "#test")
                print("check complete")
                return false
            end
    

    And here's what pops up in the console.

    [   PanoramaScript       ]: Send Build command. Queue: true
    [   VScript              ]: 879 200 false
    [   VScript              ]: precheck
    

    It seems that the build command goes through with a true even before it reaches the precheck? And I never got to see the "check complete" print. Not sure what to make of that.

  • Posts: 1,670

    You have to print inside HasReachedMaxBuildingLimit to see what parameters is comparing and what is it returning

    The concept of Modding Community doesn't go well together with Competitive Business
    My Project Page || My GitHub Profile ||

  • Can you tell me what I should print there? So far I'm only familiar with printing regular messages.

  • Posts: 1,670

    print variables and comparisons, like:

    • print(buildingCounter)
    • print(maxBuildingCount)
    • print(buildingCounter >= maxBuildingCount)

    The concept of Modding Community doesn't go well together with Competitive Business
    My Project Page || My GitHub Profile ||