ContextPtr and Stacks

Iceco

Warlord
Joined
Oct 27, 2010
Messages
188
In the DiploList and DiploRelationships.lua I'm trying to make some changes to the city-states.
They are generated as elements in a stack and I'm having trouble making changes to them. I want to make them support new CS traits other than the 3 vanilla ones and for that, I need to make certain changes around line 448 in DiploList.lua and line 490 in DiploRelationships.lua

Here is what I have and where I get an error message (in red):
Code:
-------------------------------------------------
-- Diplomatic
-------------------------------------------------
include( "IconSupport" );
include( "SupportFunctions"  );
include( "InstanceManager" );
include( "InfoTooltipInclude" );

local m_PlayerTable = Matchmaking.GetPlayerList();
local m_PlayerNames = {};
for i = 1, #m_PlayerTable do
    m_PlayerNames[ m_PlayerTable[i].playerID ] = m_PlayerTable[i].playerName;
end

local g_MajorCivButtonIM = InstanceManager:new( "MajorCivButtonInstance", "DiploButton", Controls.ButtonStack );
local g_MinorCivButtonIM = InstanceManager:new( "MinorCivButtonInstance", "LeaderButton", Controls.MinorButtonStack );
local g_MajorCivTradeRowIMList = {};

local g_iDealDuration = Game.GetDealDuration();

local g_Deal = UI.GetScratchDeal();
local g_iUs = -1; --Game.GetActivePlayer();
local g_pUs = -1;

-------------------------------------------------------------------------------
-------------------------------------------------------------------------------
function ShowHideHandler( bIsHide, bInitState )
	g_iUs = Game.GetActivePlayer();
	g_pUs = Players[ g_iUs ];
	
	InitMinorCivList();
end
ContextPtr:SetShowHideHandler( ShowHideHandler );

function InitMinorCivList()
	-- Clear buttons
	-- g_MinorCivButtonIM:ResetInstances();
	
	local iPlayer = Game.GetActivePlayer();
	local pPlayer = Players[ iPlayer ];
	local pTeam = Teams[pPlayer:GetTeam()];

	local iMinorMetCount = 0;
	local firstCityStateFound = nil;
	for iPlayerLoop = GameDefines.MAX_MAJOR_CIVS, GameDefines.MAX_CIV_PLAYERS-1, 1 do
		
		pOtherPlayer = Players[iPlayerLoop];
		iOtherTeam = pOtherPlayer:GetTeam();

		if (pPlayer:GetTeam() ~= iOtherTeam and pOtherPlayer:IsAlive()) then
			if (pTeam:IsHasMet(iOtherTeam)) then
				if (pOtherPlayer:IsMinorCiv()) then
						iMinorMetCount = iMinorMetCount + 1;
						SetCityStateTrait(iPlayerLoop)
				end
			end
		end
	end		
end

function SetCityStateTrait(iPlayer)
	local pPlayer = Players[iPlayer];

	local strTraitSuffix = "";
	local strTraitText = "";
	local strTraitTT = "";
	local iTrait = pPlayer:GetMinorCivTrait();

	local strTrait = GameInfo.MinorCivTraits[iTrait].Type
	strTraitSuffix = string.sub( strTrait, 13 )
	strTraitText = Locale.ConvertTextKey( "TXT_KEY_CITY_STATE_" .. strTraitSuffix .. "_ADJECTIVE" )
	strTraitTT = Locale.ConvertTextKey( "TXT_KEY_CITY_STATE_" .. strTraitSuffix .. "_TT" )
	
	strTraitText = "[COLOR_POSITIVE_TEXT]" .. strTraitText .. "[ENDCOLOR]";
	
	if strTraitText ~= nil then
		ContextPtr:LookUpControl("/InGame/DiploRelationships/TraitInfo"):LocalizeAndSetText(strTraitText)
		[color=red]-- Runtime Error: [string "<file path to my file>"]:77: attempt to index a nil value[/color]
	end
	if strTraitTT ~= nil then
		ContextPtr:LookUpControl("/InGame/DiploRelationships/TraitInfo"):SetToolTipString(strTraitTT)
		ContextPtr:LookUpControl("/InGame/DiploRelationships/TraitLabel"):SetToolTipString(strTraitTT)
	end
	-- pStack.TraitInfo:SetText(strTraitText);
	-- pStack.TraitInfo:SetToolTipString(strTraitTT);
	-- pStack.TraitLabel:SetToolTipString(strTraitTT);
end
So basically, I can't input my changes in the elements of the minor civ stack (called 'CityStack') in the DiploRelationships file.

PS: I put some code in "--" comments, those are in the DiploRelationships file and I kept them for reference.
Also, Happy Easter.
 
Well, the simple first step is that it would appear that either ContextPtr or the return value of LookupControl is nil.

Is that function supposed to use a : rather than a .? It's sometimes a little confused which is which, but it definitely matters.
 
The SetCityStateTrait() function is a duplicate from another place where I had to do the same (and it worked), though there the TraitInfo and TraitLabel belonged to a single element and not to several ones generated on-the-fly in a stack.
So the use of : or . is also correct in that part, if that was the section you were referring to.

Maybe I'm not getting iPlayer data from the function above that. I'll add a print statement to see what it gives.

And what's with line 16:
Code:
local g_MinorCivButtonIM = InstanceManager:new( "MinorCivButtonInstance", "LeaderButton", Controls.MinorButtonStack );
does this maybe help me determine how to control these elements with ContextPtr? It does seem relevant, but I'm not a proper coder.
LookupControl could be nil because it is the wrong command, as I believe you're saying.

EDIT: iPlayer returns a minor civ, as it should, so that's not the problem.
 
The use of a ":" in the following line is correct, but your ContextPtr:LookUpControl("/InGame/DiploRelationships/TraitInfo") is returning a nil value. It has nothing to do with the previous code, and nothing to do with your stack code, because the paramater passed is a static string.

Now when you use ContextPtr:LookUpControl it normally used the ID of an element in the XML UI description, that isn't what you have passed so your problem is here it seems.
 
Yes, my guess is that the problem is that the TraitInfo and TraitLabel elements within the CityStack can't be called because they refer to numerous instances, one created after the other. ContextPtr just doesn't know which instance I'm referring to.
 
Back
Top Bottom