[GS] Making The Tooltip in Tech/CivicsTree show more Information for Buildings [Fixed]

Zegangani

King
Joined
Oct 9, 2020
Messages
898
Hey Guys! I wanted to edit the tooltip in the Tech/CivicsTree of Buildings to show more informations (ie. Production Cost, Maintenance, ResourceCost...) just like the one of Units. I am new to lua and just starting to learn it, sp i got the tooltip to show the ProductionCost and the Maintenance Amount but not the strategicresources required for the buildings and their strategicresource maintenance.

So here is what i did:
( ToolTipHelper.lua )
Code:
local cost = building.Cost or 0;
    if(cost ~= 0 and building.MustPurchase == false) then
        local yield = GameInfo.Yields["YIELD_PRODUCTION"];
        if(yield) then
            table.insert(toolTipLines, "[NEWLINE]" .. Locale.Lookup("LOC_TOOLTIP_BASE_COST", cost, yield.IconString, yield.Name));
        end
    end
    
    AddBuildingStrategicResourceTooltip(buildingHash, toolTipLines);

    AddBuildingResourceMaintenanceTooltip(buildingHash, toolTipLines);
    
    local maintenance = building.Maintenance or 0;
    if(maintenance ~= 0) then
        local yield = GameInfo.Yields["YIELD_GOLD"];
        if(yield) then
            table.insert(toolTipLines, Locale.Lookup("LOC_TOOLTIP_MAINTENANCE", maintenance, yield.IconString, yield.Name));
        end
    end

(ToolTipLoader_Expansion2)
Code:
BASE_AddBuildingStrategicResourceTooltip = AddBuildingStrategicResourceTooltip;
BASE_AddBuildingResourceMaintenanceTooltip = AddBuildingResourceMaintenanceTooltip;

AddBuildingStrategicResourceTooltip = function(buildingHash, tooltipLines)
    local buildingReference:table = GameInfo.Buildings[buildingHash];
    if (buildingReference ~= nil) then
        local buildingType:string = buildingReference.BuildingType;
        if (pBuildQueue ~= nil ) then
            local StartProductionCost = pBuildQueue:GetBuildingResourceCost(buildingReference.Index);
            if (resource ~= nil) then
                local resourceIcon = "[ICON_" .. resource.ResourceType .. "]";
                return Locale.Lookup("LOC_UNIT_PRODUCTION_RESOURCE_COST", StartProductionCost, resourceIcon, resource.Name);
            end
        else
            if (GameInfo.Building_ResourceCosts~= nil) then
                for row in GameInfo.Building_ResourceCosts() do
                    if (row.BuildingType == BuildingType) then
                        local resource = GameInfo.Resources[row.ResourceType];
                        local StartProductionCost = row.StartProductionCost;
                        local resourceIcon = "[ICON_" .. resource.ResourceType .. "]";
                        return Locale.Lookup("LOC_UNIT_PRODUCTION_RESOURCE_COST", StartProductionCost, resourceIcon, resource.Name);
                    end
                end
            end
        end
    end
    return "";
end

-- ===========================================================================
AddBuildingResourceMaintenanceTooltip = function(buildingHash, tooltipLines)
    local buildingReference:table = GameInfo.Buildings[buildingHash];
    if (buildingReference ~= nil) then
        local buildingType:string = buildingReference.BuildingType;
        if (GameInfo.Building_ResourceCosts~= nil) then
            for row in GameInfo.Building_ResourceCosts() do
                if (row.BuildingType == BuildingType) then
                    local PerTurnMaintenanceCost = row.PerTurnMaintenanceCost;
                    if (PerTurnMaintenanceCost > 0) then
                        local resource = GameInfo.Resources[row.ResourceType];
                        if (resource ~= nil) then
                            local fuelName = Locale.Lookup(resource.Name);
                            local iconName:string = "[ICON_" .. row.ResourceType .. "]";
                            return Locale.Lookup("LOC_UNIT_PRODUCTION_FUEL_CONSUMPTION", PerTurnMaintenanceCost,  iconName, fuelName);
                        end
                    end
                end
            end
        end
    end
    return "";
end

I tryed everything to get this to work, with no result, except that the code do not crush get errors or something. So i would really apreciate it if someone helps me with this.
 
Code:
if (pBuildQueue ~= nil ) then
pBuildQueue is almost certainly nil at the scoping level where you are attempting to access it.

See the difference between your opening function define and this one
Code:
AddUnitStrategicResourceTooltip = function(unitReference, formationType, pBuildQueue)

resource is also almost certainly nil here
Code:
if (resource ~= nil) then
Again because it has never been defined nor passed into the function.
Code:
AddBuildingStrategicResourceTooltip = function(buildingHash, tooltipLines)
    local buildingReference:table = GameInfo.Buildings[buildingHash];
    if (buildingReference ~= nil) then
        local buildingType:string = buildingReference.BuildingType;
        if (pBuildQueue ~= nil ) then
            local StartProductionCost = pBuildQueue:GetBuildingResourceCost(buildingReference.Index);
            if (resource ~= nil) then
Compare to the Firaxis code:
Code:
AddUnitStrategicResourceTooltip = function(unitReference, formationType, pBuildQueue)
	local resource = GameInfo.Resources[unitReference.StrategicResource];
	if (pBuildQueue ~= nil ) then
		local resourceAmount = pBuildQueue:GetUnitResourceCost(unitReference.Index, formationType);
		if (resource ~= nil) then
The Firaxis code passes the pBuildQueue data into the function and then the first thing it does is define the variable for the strategic resource the unit requires.

This will never be evaluated as true
Code:
if (row.BuildingType == BuildingType) then
Lua is case sensitive. You have not defined a variable called "BuildingType" -- you have defined one called "buildingType".
 
Code:
if (pBuildQueue ~= nil ) then
pBuildQueue is almost certainly nil at the scoping level where you are attempting to access it.

See the difference between your opening function define and this one
Code:
AddUnitStrategicResourceTooltip = function(unitReference, formationType, pBuildQueue)

resource is also almost certainly nil here
Code:
if (resource ~= nil) then
Again because it has never been defined nor passed into the function.
Code:
AddBuildingStrategicResourceTooltip = function(buildingHash, tooltipLines)
    local buildingReference:table = GameInfo.Buildings[buildingHash];
    if (buildingReference ~= nil) then
        local buildingType:string = buildingReference.BuildingType;
        if (pBuildQueue ~= nil ) then
            local StartProductionCost = pBuildQueue:GetBuildingResourceCost(buildingReference.Index);
            if (resource ~= nil) then
Compare to the Firaxis code:
Code:
AddUnitStrategicResourceTooltip = function(unitReference, formationType, pBuildQueue)
    local resource = GameInfo.Resources[unitReference.StrategicResource];
    if (pBuildQueue ~= nil ) then
        local resourceAmount = pBuildQueue:GetUnitResourceCost(unitReference.Index, formationType);
        if (resource ~= nil) then
The Firaxis code passes the pBuildQueue data into the function and then the first thing it does is define the variable for the strategic resource the unit requires.

This will never be evaluated as true
Code:
if (row.BuildingType == BuildingType) then
Lua is case sensitive. You have not defined a variable called "BuildingType" -- you have defined one called "buildingType".

Thank you LeeS for the help! I will try it out. I was actually reading your beginners guidance for modding and it really helped me to understand how lua works (although I am just starting to try it out), so a BIG THANK from me for providing as (modding newbies) with such help!!
 
I'v tryed to make it as you suggested, but still doesn't show the resources required for the buildings.
And of course i've made some buildings to require resource for production and maintenance (Building_ResourceCosts can do that) in order to test this code. So it's not of this end that it doesn't show up.

here is the code i've tested:
Code:
AddBuildingStrategicResourceTooltip = function(buildingHash, pBuildQueue)
    local buildingReference:table = GameInfo.Buildings[buildingHash];
    if (buildingReference ~= nil) then
        local buildingType:string = buildingReference.BuildingType;
        local resource = GameInfo.Resources[buildingReference.StrategicResource];
        if (pBuildQueue ~= nil ) then
            local StartProductionCost = pBuildQueue:GetBuildingResourceCost(buildingReference.Index);
            if (resource ~= nil) then
                local resourceIcon = "[ICON_" .. resource.ResourceType .. "]";
                return Locale.Lookup("LOC_UNIT_PRODUCTION_RESOURCE_COST", StartProductionCost, resourceIcon, resource.Name);
            end
        else
            if (GameInfo.Building_ResourceCosts~= nil) then
                for row in GameInfo.Building_ResourceCosts() do
                    if (row.BuildingType == buildingType) then
                        local resource = GameInfo.Resources[row.ResourceType];
                        local StartProductionCost = row.StartProductionCost;
                        local resourceIcon = "[ICON_" .. resource.ResourceType .. "]";
                        return Locale.Lookup("LOC_UNIT_PRODUCTION_RESOURCE_COST", StartProductionCost, resourceIcon, resource.Name);
                    end
                end
            end
        end
    end
    return "";
end

-- ===========================================================================
AddBuildingResourceMaintenanceTooltip = function(buildingHash)
    local buildingReference:table = GameInfo.Buildings[buildingHash];
    if (buildingReference ~= nil) then
        local buildingType:string = buildingReference.BuildingType;
        if (GameInfo.Building_ResourceCosts~= nil) then
            for row in GameInfo.Building_ResourceCosts() do
                if (row.BuildingType == buildingType) then
                    local PerTurnMaintenanceCost = row.PerTurnMaintenanceCost;
                    if (PerTurnMaintenanceCost > 0) then
                        local resource = GameInfo.Resources[row.ResourceType];
                        if (resource ~= nil) then
                            local fuelName = Locale.Lookup(resource.Name);
                            local iconName:string = "[ICON_" .. row.ResourceType .. "]";
                            return Locale.Lookup("LOC_UNIT_PRODUCTION_FUEL_CONSUMPTION", PerTurnMaintenanceCost,  iconName, fuelName);
                        end
                    end
                end
            end
        end
    end
    return "";
end

Maybe i've made another mistake in the lua code that i don't know of?!
 
Last edited:
When executing AddBuildingStrategicResourceTooltip elsewhere in the code what are you passing into it for the argument data ?

And from where are you pulling that argument data to send into the function.
 
When executing AddBuildingStrategicResourceTooltip elsewhere in the code what are you passing into it for the argument data ?

And from where are you pulling that argument data to send into the function.

in the ToolTipHelper.lua i added the line:
AddBuildingStrategicResourceTooltip(buildingHash, pBuildQueue, toolTipLines);
it's in : ToolTipHelper.GetBuildingToolTip = function(buildingHash, playerId, city)

so if i understand it correctly, i have also to add (pBuildQueue) as an argument in ToolTipHelper.GetBuildingToolTip as follows: ToolTipHelper.GetBuildingToolTip = function(buildingHash, playerId, city, pBuildQueue), right?
 
I've changed in the ToolTipHelper.lua the Building tooltip to:
ToolTipHelper.GetBuildingToolTip = function(buildingHash, playerId, city, pBuildQueue)

And i've also noticed that i have missed something in ToolTipHelper.lua and i added them then:

Code:
function AddBuildingStrategicResourceTooltip(buildingHash, pBuildQueue)
    local resource = GameInfo.Resources[buildingReference.StrategicResource];
    local resourceString :string ="";
    if(resource) then
        resourceString = resourceString .. "[NEWLINE]" .. Locale.Lookup("LOC_TOOLTIP_BUILDING_REQUIRES");
        resourceString = resourceString .. "[NEWLINE][ICON_BULLET] " .. "[ICON_" .. resource.ResourceType .. "]" .. Locale.Lookup(resource.Name);
    end
    return resourceString;
end

-------------------------------------------------------------------------------
function AddBuildingResourceMaintenanceTooltip(buildingHash)
    return "";
end

But still no effect! :undecide:

Another question: is it possible to make a new table in wich you set the columns to have two different resourcetypes , one for resource cost and one for resource maintenance, (ie. BuildingA requires 20 IronResource to be produced and 2 Coal maintenance per turn.) just like the Units? I've tryed it (without lua) but it didn't work, maybe there is some workaround that i don't know of?!
 
Last edited:
Top Bottom