MaxPlayerInstances for Buildings

Dnorak

Chieftain
Joined
Feb 23, 2015
Messages
18
Location
Ukraine
Could somebody give me advice how I can set limit on the number of buildings simple like MaxPlayerInstances for BuildingClasses but I need for just Buildings.
Is there a solution how to do it?
 
If you want an individual player to only be able to create 6 copies of a building, you just use MaxPlayerInstances under BuildingClasses, and set it to 6. Refer to how Firaxis did the Recycling Center.

This method will apply to all players, and for all unique versions of the building that belong to the same building-class, though. So if that isn't what you want, then the method(s) Nutty mentioned is a better approach.
 
My building must have the standard class because that class opens to the production other buildings (other mod refer to it), if classes have something like inheritance I could create own but they haven't. So I have to use lua?
 
.... So I have to use lua?
Yup.
Just remember that if your building is any of the following classes of buildings, then the National Wonder that requires that class of building will become unbuildable:
Spoiler :
BUILDINGCLASS_POLICE_STATION => Intelligence Agency
BUILDINGCLASS_TEMPLE => Grand Temple
BUILDINGCLASS_HOTEL => Tourist Center
BUILDINGCLASS_BARRACKS => Heroic Epic
BUILDINGCLASS_MONUMENT => National Epic
BUILDINGCLASS_COLOSSEUM => Circus Maximus
BUILDINGCLASS_MARKET => Natioanl Treasury / East India Company
BUILDINGCLASS_LIBRARY => National College
BUILDINGCLASS_WORKSHOP => Ironworks
BUILDINGCLASS_UNIVERSITY => Oxford University
BUILDINGCLASS_OPERA_HOUSE => Hermitage

If your building belongs to BUILDINGCLASS_LIBRARY this will mean that not only National College but also Oxford University will become unbuildable for that player.
 
Trying to use lua, I'm new to this language and I could not find good guides with describing the methods for civilization 5 modding. This is my first experience of modding for civilization. Could you give me advice, I want that a player to only be able to create 3 copies of the building. I wrote this code:
Code:
GameEvents.PlayerDoTurn.Add(function(iPlayer)
    if Players[iPlayer]:GetCivilizationType() == GameInfoTypes.CIVILIZATION_INDIA then
        local numBuilds = 0;
        for pCity in iPlayer:Cities() do
                if pCity:IsHasBuilding(GameInfoTypes.BUILDING_INDIAN_FESTIVAL) then
                    numBuilds = numBuilds + 1;
                end
        end
        if numBuilds >= 3 then
            --to do: disable construction BUILDING_INDIAN_FESTIVAL
        else
            --to do: enable construction BUILDING_INDIAN_FESTIVAL
        end
    end
end)
is it correct at total ? And how I can do disable/enable construction of my building ? Could somebody help ?
 
CityCanConstruct is called every time the production popup window is generated for the City (any, and every City is handled individually.)
Though that's not strictly how it works, you can think of that effect for the human player. The AI players will also abide by the restrictions set by this event.

The best thing you can do is to create a second function to check for how many buildings of a particular type you have ahead of time (say, on PlayerDoTurn) and cache that into a global variable, then reference that variable in your CityCanConstruct function.

CityCanConstruct can only ever be used to deny construction of a building that would otherwise be buildable; it cannot be used in reverse to force the allowance of a building for which its requirements have not yet been met.
 
I have used this:

Code:
Player:GetBuildingClassCount(BuildingClassType)

I have not used the two following (hopefully they are actually "connected" thru the dll and will do something):

Code:
Player:GetBuildingClassCountPlusMaking(BuildingClassType)

Player:GetBuildingClassMaking(BuildingClassType)
 
Player:GetBuildingClassCount(BuildingClassType) this is for BuildingClass but I need to count Buildings, not class.
I wrote this code:
Code:
GameEvents.PlayerCanConstruct.Add(
    function(iPlayer, buildingTypeID)
        local player = Players[iPlayer]
            if player:GetCivilizationType() == GameInfoTypes.CIVILIZATION_INDIA then	
                local buildingType = GameInfo.Buildings[buildingTypeID].Type;
                local numBuilds = 0;
                if buildingType == GameInfoTypes.BUILDING_INDIAN_FESTIVAL then
                for pCity in iPlayer:Cities() do
                    if pCity:IsHasBuilding(GameInfoTypes.BUILDING_INDIAN_FESTIVAL) then
                        numBuilds = numBuilds + 1;
                    end
                end
                    if numBuilds >= 3 then
                        return false
                    end
                end
            end
        return true
    end)
and it's doesn't work :(
How I can debug my script ? And check if it loaded ?
update: I figured out, this Player.CountNumBuildings works fine and full working code, maybe for someone it's be useful:
Code:
GameEvents.CityCanConstruct.Add(function(iPlayer, iCity, iBuildingType)
    local pPlayer = Players[iPlayer];
    if pPlayer:GetCivilizationType() == GameInfoTypes.CIVILIZATION_INDIA then
        if (iBuildingType == GameInfo.Buildings["BUILDING_INDIAN_FESTIVAL"].ID) then
            if (pPlayer:CountNumBuildings(iBuildingType) > 2) then
                return false;
            end
        end
    end
    return true;
end);
Thank you all for help.
 
Player:GetBuildingClassCount(BuildingClassType) this is for BuildingClass but I need to count Buildings, not class.
It actually is what you want to easily figure how many of Building_x an empire has already built. Because no one empire is allowed to build more than one building from within one class, for any individual player it is actually going to return how many buildings they own from within that class.

Of course, as soon as a mod-creator starts using stuff like hidden buildings where they assign a host of hidden buildings to the same building-class, and add those hidden buildings directly through lua methods, the method becomes useless for that incorrectly structured building-class, but for the standard Firaxis-supplied building-classes, and assuming no one tries anything crazy with a mod for those Firaxis-supplied building-classes, the method is good.
 
Now I understand. It works too:
Spoiler :
Code:
GameEvents.CityCanConstruct.Add(function(iPlayer, iCity, iBuildingType)
    local pPlayer = Players[iPlayer];
    if pPlayer:GetCivilizationType() == GameInfoTypes.CIVILIZATION_INDIA then
        if (iBuildingType == GameInfo.Buildings["BUILDING_INDIAN_FESTIVAL"].ID) then
            if (pPlayer:GetBuildingClassCount(GameInfo.BuildingClasses.BUILDINGCLASS_CIRCUS.ID) > 2) then
                return false;
            end
        end
    end
    return true;
end);
and I think GetBuildingClassCount is faster than CountNumBuildings so I'll use it, thanks.
 
Instead of GameInfo.Buildings["BUILDING_INDIAN_FESTIVAL"].ID you can probably just use GameInfoTypes.BUILDING_INDIAN_FESTIVAL since the GameInfoTypes table is faster to query than the GameInfo table. As well, since you're querying it every time this function fires (CityCanConstruct is fired multiple times each time you open a City's construction window) it may help for you to define those into a global variable beforehand. (Same with the Civilization ID and the Circus BuildingClass.)

But otherwise, yeah, you can normally only build one building per buildingclass anyway, and the game goes through some lengths in its dumb assumptions to make certain building stuff not work if you're not "supposed" to be able to build that particular building. (My Civ technically has 2 Shrine replacements, and I've had to go through high hell to get it working properly.)
 
Top Bottom