Issue with epithets lua from RTP within Anno Domini

Rob (R8XFT)

Ancient Briton
Retired Moderator
Joined
Aug 11, 2002
Messages
10,866
Location
Leeds (UK)
There seems to be an issue with some lua from RTP's epithets when mixed with Anno Domini. The highlighted line (in red) gives this runtime error in the lua log: bad argument #2 to '?' (Key must be of type 'number' or 'string'.)

I'm clueless as to what the issue is and would be grateful for any assistance :).

Code:
-- JFD_HasAnyEpithet
function JFD_HasAnyEpithet(playerID)
	local player = Players[playerID]
	for row in GameInfo.JFD_Epithets() do
		if player:HasPolicy(GameInfoTypes[row.PolicyType]) then
			return true
		end
	end
	return false
end

-- JFD_HasEpithet
function JFD_HasEpithet(playerID, epithetID)
	local player = Players[playerID]
	local epithet = GameInfo.JFD_Epithets[epithetID]
	local policyID = GameInfoTypes[epithet.PolicyType]
	return player:HasPolicy(policyID)
end

-- JFD_SetHasEpithet
function JFD_SetHasEpithet(playerID, epithetID, setHasEpithet, sendNotification)
	local player = Players[playerID]
	[COLOR="Red"]local epithet = GameInfo.JFD_Epithets[epithetID][/COLOR]
	local policyID = GameInfoTypes[epithet.PolicyType]
	if setHasEpithet then
		player:GrantPolicy(policyID, true)
		local leaderName = JFD_GetDefaultLeaderName(playerID)
		--if Sovereignty 
		--JFD_GetLeaderTitleShort(playerID)
		local epithetShortDesc, epithetDesc = JFD_GetEpithetDescription(playerID, epithetID)
		if sendNotification then
			if player:IsHuman() then
				JFD_SendNotification(playerID, "NOTIFICATION_GENERIC", Locale.ConvertTextKey("TXT_KEY_NOTIFICATION_JFD_EPITHET", leaderName, epithetShortDesc), Locale.ConvertTextKey("TXT_KEY_NOTIFICATION_JFD_EPITHET_SHORT"))
			else
				JFD_SendWorldEvent(playerID, Locale.ConvertTextKey("TXT_KEY_NOTIFICATION_JFD_EPITHET_OTHER", leaderName, epithetShortDesc), Locale.ConvertTextKey("TXT_KEY_NOTIFICATION_JFD_EPITHET_SHORT"))
			end
		end
		for row in GameInfo.JFD_Epithets("ID <> '" .. epithetID .. "'") do
			JFD_SetHasEpithet(playerID, row.ID, false)
		end
	else
		player:RevokePolicy(policyID)
	end
end
 
Perhaps it would help to see the whole lua file? If so, it is below with the offending line highlighted in red. I am not very good at lua (hence the threads here ;)) and would be extremely grateful for any help :). Incidentally, I do have JFD_RTPUtils.lua added, so it is there to include, in case anyone was going to ask.

Code:
-- JFD_RTP_EpithetsUtils
-- Author: JFD
-- DateCreated: 10/14/2015 7:38:08 PM
--=======================================================================================================================
-- INCLUDES
--=======================================================================================================================
include("JFD_RTPUtils.lua")
--=======================================================================================================================
-- UTILITIES
--=======================================================================================================================
-- GLOBALS
------------------------------------------------------------------------------------------------------------------------
local convertTextKey = Locale.ConvertTextKey
local mathCeil	= math.ceil
local mathFloor = math.floor
local mathMin	= math.min
------------------------------------------------------------------------------------------------------------------------
-- EPITHETS
------------------------------------------------------------------------------------------------------------------------
-- JFD_DoEpithet
function JFD_DoEpithet(playerID, epithetID, epithetChance)
	local player = Players[playerID]
	if JFD_GetRandom(1,100) <= epithetChance then
		if player:IsHuman() then
			LuaEvents.JFD_RTP_Epithets_ShowChooseEpithetPopup(epithetID)
		else
			JFD_SetHasEpithet(playerID, epithetID, true)
		end
	end
end

-- JFD_GetEpithetID
function JFD_GetEpithetID(playerID)
	local player = Players[playerID]
	for row in GameInfo.JFD_Epithets() do
		if player:HasPolicy(GameInfoTypes[row.PolicyType]) then
			return row.ID
		end
	end
end

-- JFD_GetEpithetDescription
function JFD_GetEpithetDescription(playerID, epithetID)
	local player = Players[playerID]
	local epithet = GameInfo.JFD_Epithets[epithetID]
	local epithetShortDesc = epithet.ShortDescription
	if (epithet.HasFeminineAlt and JFD_IsLeaderFeminine(playerID)) then epithetShortDesc = epithetShortDesc .. "_FEMININE" end
	local civType = GameInfo.Civilizations[player:GetCivilizationType()].Type
	for row in GameInfo.JFD_Epithet_CivilizationTitles("EpithetType = '" .. epithet.Type .. "' AND CivilizationType = '" .. civType .. "'") do
		epithetShortDesc = row.ShortDescription	
	end
	local religionID = player:GetStateReligion()
	local religion = GameInfo.Religions[religionID]
	if religion then
		for row in GameInfo.JFD_Epithet_ReligionTitles("EpithetType = '" .. epithet.Type .. "' AND ReligionType = '" .. religion.Type .. "'") do
			epithetShortDesc = row.ShortDescription	
		end
	end
	return epithetShortDesc, epithet.Description
end

-- JFD_HasAnyEpithet
function JFD_HasAnyEpithet(playerID)
	local player = Players[playerID]
	for row in GameInfo.JFD_Epithets() do
		if player:HasPolicy(GameInfoTypes[row.PolicyType]) then
			return true
		end
	end
	return false
end

-- JFD_HasEpithet
function JFD_HasEpithet(playerID, epithetID)
	local player = Players[playerID]
	[COLOR="Red"]local epithet = GameInfo.JFD_Epithets[epithetID][/COLOR]
	local policyID = GameInfoTypes[epithet.PolicyType]
	return player:HasPolicy(policyID)
end

-- JFD_SetHasEpithet
function JFD_SetHasEpithet(playerID, epithetID, setHasEpithet, sendNotification)
	local player = Players[playerID]
	local epithet = GameInfo.JFD_Epithets[epithetID]
	local policyID = GameInfoTypes[epithet.PolicyType]
	if setHasEpithet then
		player:GrantPolicy(policyID, true)
		local leaderName = JFD_GetDefaultLeaderName(playerID)
		--if Sovereignty 
		--JFD_GetLeaderTitleShort(playerID)
		local epithetShortDesc, epithetDesc = JFD_GetEpithetDescription(playerID, epithetID)
		if sendNotification then
			if player:IsHuman() then
				JFD_SendNotification(playerID, "NOTIFICATION_GENERIC", Locale.ConvertTextKey("TXT_KEY_NOTIFICATION_JFD_EPITHET", leaderName, epithetShortDesc), Locale.ConvertTextKey("TXT_KEY_NOTIFICATION_JFD_EPITHET_SHORT"))
			else
				JFD_SendWorldEvent(playerID, Locale.ConvertTextKey("TXT_KEY_NOTIFICATION_JFD_EPITHET_OTHER", leaderName, epithetShortDesc), Locale.ConvertTextKey("TXT_KEY_NOTIFICATION_JFD_EPITHET_SHORT"))
			end
		end
		for row in GameInfo.JFD_Epithets("ID <> '" .. epithetID .. "'") do
			JFD_SetHasEpithet(playerID, row.ID, false)
		end
	else
		player:RevokePolicy(policyID)
	end
end
--=======================================================================================================================
--=======================================================================================================================
 
JFD_SetHasEpithet() is being called but not correctly passing the ID of an epithet. Do you include Epithets in AD?

Yes. I have had a successful epithet (not played AD much, just modded it a lot), but this message has appeared with the same event, one that gives gold and grants an epithet (possibly "the fortunate" but I can't remember).
 
Hm. The event-based epithets (incl. the Fortunate) are situated in a file other than the main Epithets file in RtP. Make sure you copied those over as well. Otherwise, I'm not really sure. To quieten that error, you use this version of the Utils file:

Code:
-- JFD_RTP_EpithetsUtils
-- Author: JFD
-- DateCreated: 10/14/2015 7:38:08 PM
--=======================================================================================================================
-- INCLUDES
--=======================================================================================================================
include("JFD_RTPUtils.lua")
--=======================================================================================================================
-- UTILITIES
--=======================================================================================================================
-- GLOBALS
------------------------------------------------------------------------------------------------------------------------
local convertTextKey = Locale.ConvertTextKey
local mathCeil	= math.ceil
local mathFloor = math.floor
local mathMin	= math.min
------------------------------------------------------------------------------------------------------------------------
-- EPITHETS
------------------------------------------------------------------------------------------------------------------------
-- JFD_DoEpithet
function JFD_DoEpithet(playerID, epithetID, epithetChance)
	local player = Players[playerID]
	if JFD_GetRandom(1,100) <= epithetChance then
		if player:IsHuman() then
			LuaEvents.JFD_RTP_Epithets_ShowChooseEpithetPopup(epithetID)
		else
			JFD_SetHasEpithet(playerID, epithetID, true)
		end
	end
end

-- JFD_GetEpithetID
function JFD_GetEpithetID(playerID)
	local player = Players[playerID]
	for row in GameInfo.JFD_Epithets() do
		if player:HasPolicy(GameInfoTypes[row.PolicyType]) then
			return row.ID
		end
	end
end

-- JFD_GetEpithetDescription
function JFD_GetEpithetDescription(playerID, epithetID)
	local player = Players[playerID]
	local epithet = GameInfo.JFD_Epithets[epithetID]
	local epithetShortDesc = epithet.ShortDescription
	if (epithet.HasFeminineAlt and JFD_IsLeaderFeminine(playerID)) then epithetShortDesc = epithetShortDesc .. "_FEMININE" end
	local civType = GameInfo.Civilizations[player:GetCivilizationType()].Type
	for row in GameInfo.JFD_Epithet_CivilizationTitles("EpithetType = '" .. epithet.Type .. "' AND CivilizationType = '" .. civType .. "'") do
		epithetShortDesc = row.ShortDescription	
	end
	local religionID = player:GetStateReligion()
	local religion = GameInfo.Religions[religionID]
	if religion then
		for row in GameInfo.JFD_Epithet_ReligionTitles("EpithetType = '" .. epithet.Type .. "' AND ReligionType = '" .. religion.Type .. "'") do
			epithetShortDesc = row.ShortDescription	
		end
	end
	return epithetShortDesc, epithet.Description
end

-- JFD_HasAnyEpithet
function JFD_HasAnyEpithet(playerID)
	local player = Players[playerID]
	for row in GameInfo.JFD_Epithets() do
		if player:HasPolicy(GameInfoTypes[row.PolicyType]) then
			return true
		end
	end
	return false
end

-- JFD_HasEpithet
function JFD_HasEpithet(playerID, epithetID)
	local player = Players[playerID]
	local epithet = GameInfo.JFD_Epithets[epithetID]
	local policyID = GameInfoTypes[epithet.PolicyType]
	return player:HasPolicy(policyID)
end

-- JFD_SetHasEpithet
function JFD_SetHasEpithet(playerID, epithetID, setHasEpithet, sendNotification)
	local player = Players[playerID]
        if (not epithetID) then return end
	local epithet = GameInfo.JFD_Epithets[epithetID]
	local policyID = GameInfoTypes[epithet.PolicyType]
	if setHasEpithet then
		player:GrantPolicy(policyID, true)
		local leaderName = JFD_GetDefaultLeaderName(playerID)
		--if Sovereignty 
		--JFD_GetLeaderTitleShort(playerID)
		local epithetShortDesc, epithetDesc = JFD_GetEpithetDescription(playerID, epithetID)
		if sendNotification then
			if player:IsHuman() then
				JFD_SendNotification(playerID, "NOTIFICATION_GENERIC", Locale.ConvertTextKey("TXT_KEY_NOTIFICATION_JFD_EPITHET", leaderName, epithetShortDesc), Locale.ConvertTextKey("TXT_KEY_NOTIFICATION_JFD_EPITHET_SHORT"))
			else
				JFD_SendWorldEvent(playerID, Locale.ConvertTextKey("TXT_KEY_NOTIFICATION_JFD_EPITHET_OTHER", leaderName, epithetShortDesc), Locale.ConvertTextKey("TXT_KEY_NOTIFICATION_JFD_EPITHET_SHORT"))
			end
		end
		for row in GameInfo.JFD_Epithets("ID <> '" .. epithetID .. "'") do
			JFD_SetHasEpithet(playerID, row.ID, false)
		end
	else
		player:RevokePolicy(policyID)
	end
end
--=======================================================================================================================
--=======================================================================================================================

I've merely added a check that, if the epithet ID is invalid, the function will not proceed.
 
Back
Top Bottom