[BNW] Help With UniqueDiplomacyUtils Lua?

FonziScheme

Chieftain
Joined
Aug 11, 2017
Messages
6
I have no clue what I'm doing with Lua. However, the utility I found seemed like it was pretty self-explanatory. But I can't seem to get it working and am wondering if anyone might be able to figure out what I'm doing wrong.

First of all, I downloaded the Lua file, added it into the mod project and set VFS to true. I didn't use OnModActivated nor IngameUIaddin, only change is that VFS is set to true.

In my diplomacy_responses.xml file (set to OnModActivated, works fine for first greeting and defeat so far in regards to not using lua), I added the following under the <Language_en_US> tag.
<Row Tag="TXT_KEY_LEADER_FONZI_LIZ_FIRSTGREETING_1">
<Text>The lua worked!</Text>
</Row>
<Row Tag="TXT_KEY_LEADER_FONZI_LIZ_FIRSTGREETING_2">
<Text>The lua worked Dos!</Text>
</Row>

I then added the following to the bottom of the Lua file:

include("UniqueDiplomacyUtilsV3.lua")

function FonziUniqueResponses()
local pActivePlayer = Players[Game.Get.ActivePlayer()]
local sLeaderType = GameInfo.Leaders[pActivePlayer:GetLeaderType()].Type
end

include("UniqueDiplomacyUtilsV3.lua")

function FonziUniqueResponses()
local pActivePlayer = Players[Game.GetActivePlayer()]
local sLeaderType = GameInfo.Leaders[pActivePlayer:GetLeaderType()].Type
if (sLeaderType -- "LEADER_ELIZABETH") then
ChangeDiplomacyResponse("LEADER_FONZI", "RESPONSE_FIRST_GREETING", "TXT_KEY_LEADER_FONZI_LIZ_FIRSTGREETING%")
end
end

include("UniqueDiplomacyUtilsV3.lua")

function FonziUniqueResponses()
local pActivePlayer = Players[Game.GetActivePlayer()]
local sLeaderType = GameInfo.Leaders[pActivePlayer:GetLeaderType()].Type
if (sLeaderType == "LEADER_ELIZABETH") then
ChangeDiplomacyResponse("LEADER_FONZI", "RESPONSE_FIRST_GREETING", "TXT_KEY_LEADER_FONZI_LIZ_FIRSTGREETING%")
end
end

FonziUniqueResponses()


What exactly am I missing?
 
Hello, it's me who made the utility/tutorial :)

Possibly because you haven't used Lua before, I think that you may have misunderstood the instructions.

First thing is: 'I then added the following to the bottom of the Lua file:'

Is 'the Lua file' you mean here UniqueDiplomacyUtilsV3.lua itself? If so, that's incorrect - you need to add this code to a new Lua script and then call that via IngameUIaddin. The 'include("UniqueDiplomacyUtilsV3.lua")' section will allow your code to call the functions from the utility.

Also, it appears that you copied the code from several of the code boxes in the tutorial and adjusted them for your own civ, and so have some duplicated code. The only part you need is the last bit:

Code:
include("UniqueDiplomacyUtilsV3.lua")

function FonziUniqueResponses()
    local pActivePlayer = Players[Game.GetActivePlayer()]
    local sLeaderType = GameInfo.Leaders[pActivePlayer:GetLeaderType()].Type
    if (sLeaderType == "LEADER_ELIZABETH") then
        ChangeDiplomacyResponse("LEADER_FONZI", "RESPONSE_FIRST_GREETING", "TXT_KEY_LEADER_FONZI_LIZ_FIRSTGREETING%")
    end
end

FonziUniqueResponses()

This should work without a problem, though you may want to add a check for if your civ is present as an AI player in your game - I suggest changing 'FonziUniqueResponses()' to something like this (assuming that your civ's type is 'CIVILIZATION_FONZI'):
Code:
for iPlayer = 0, GameDefines.MAX_MAJOR_CIVS - 1, 1 do
    local pPlayer = Players[iPlayer]
    if pPlayer ~= nil and pPlayer:IsAlive() and not pPlayer:IsHuman() and (pPlayer:GetCivilizationType() == GameInfo.Civilizations.CIVILIZATION_FONZI.ID) then
        Events.SequenceGameInitComplete.Add(FonziUniqueResponses) -- Will add it to the event that triggers when the game is initialised
        break
    end
end

If you have trouble getting that last bit to work then don't worry too much about it. :)
 
That would be my problem exactly. Works fine so far. I haven't tested it too much, but it's working for the First Greeting for two separate leaders.

If you wouldn't mind, I do have a couple questions:

When using this for a Civilization, assuming I'm just changing responses based on Leaders like above, I would add all the changes in the FonziUniqueResponses function, correct? The ones coming after the first "if" should be "elseif"?

For the second part of code you gave me for checking if the Leader is actually present, where exactly do you put it? I put it at the beginning of the FonziUniqueResponses function, and all the "if" statements followed after. I'm not sure if that actually is doing anything there (that's why I'm checking it's in the right spot), but it doesn't break the game, so I figured it was probably working.

The code basically:
Code:
include("UniqueDiplomacyUtilsV3.lua")

function FonziUniqueResponses()

for iPlayer = 0, GameDefines.MAX_MAJOR_CIVS - 1, 1 do
   local pPlayer = Players[iPlayer]
    if pPlayer ~= nil and pPlayer:IsAlive() and not pPlayer:IsHuman() and (pPlayer:GetCivilizationType() == GameInfo.Civilizations.CIVILIZATION_FONZI.ID) then
        Events.SequenceGameInitComplete.Add(FonziUniqueResponses) -- Will add it to the event that triggers when the game is initialised
        break
    end
end

    local pActivePlayer = Players[Game.GetActivePlayer()]
    local sLeaderType = GameInfo.Leaders[pActivePlayer:GetLeaderType()].Type
    if (sLeaderType == "LEADER_ELIZABETH") then
        ChangeDiplomacyResponse("LEADER_FONZI", "RESPONSE_FIRST_GREETING", "TXT_KEY_LEADER_FONZI_LIZ_FIRSTGREETING%")
    end
    elseif (sLeaderType == "LEADER_WASHINGTON") then
        ChangeDiplomacyResponse("LEADER_FONZI", "RESPONSE_FIRST_GREETING", "TXT_KEY_LEADER_FONZI_WASHINGTON_FIRSTGREETING%")
end

FonziUniqueResponses()

Thank you for your help, I really appreciate it. Ironically I've been having a hard time getting the diplomacy responses to actually work (besides first greeting and defeated), then realized I had found and downloaded your template for diplomacy responses weeks ago, right after I figured out how to fix them myself...
 
'When using this for a Civilization, assuming I'm just changing responses based on Leaders like above, I would add all the changes in the FonziUniqueResponses function, correct? The ones coming after the first "if" should be "elseif"?'

That's exactly correct. Just make sure you only put an 'end' at the end of all the if/elseif blocks, rather than after the if block as you do now. So it should be like:

Code:
    if (sLeaderType == "LEADER_ELIZABETH") then
        ChangeDiplomacyResponse("LEADER_FONZI", "RESPONSE_FIRST_GREETING", "TXT_KEY_LEADER_FONZI_LIZ_FIRSTGREETING%")
    elseif (sLeaderType == "LEADER_WASHINGTON") then
        ChangeDiplomacyResponse("LEADER_FONZI", "RESPONSE_FIRST_GREETING", "TXT_KEY_LEADER_FONZI_WASHINGTON_FIRSTGREETING%")
    end

'For the second part of code you gave me for checking if the Leader is actually present, where exactly do you put it?'

That block of code is supposed to replace "FonziUniqueResponses()" at the bottom. So it should look like:

Code:
function FonziUniqueResponses()
    local pActivePlayer = Players[Game.GetActivePlayer()]
    local sLeaderType = GameInfo.Leaders[pActivePlayer:GetLeaderType()].Type
    if (sLeaderType == "LEADER_ELIZABETH") then
        ChangeDiplomacyResponse("LEADER_FONZI", "RESPONSE_FIRST_GREETING", "TXT_KEY_LEADER_FONZI_LIZ_FIRSTGREETING%")
    elseif (sLeaderType == "LEADER_WASHINGTON") then
        ChangeDiplomacyResponse("LEADER_FONZI", "RESPONSE_FIRST_GREETING", "TXT_KEY_LEADER_FONZI_WASHINGTON_FIRSTGREETING%")
    end
end

for iPlayer = 0, GameDefines.MAX_MAJOR_CIVS - 1, 1 do
  local pPlayer = Players[iPlayer]
    if pPlayer ~= nil and pPlayer:IsAlive() and not pPlayer:IsHuman() and (pPlayer:GetCivilizationType() == GameInfo.Civilizations.CIVILIZATION_FONZI.ID) then
        Events.SequenceGameInitComplete.Add(FonziUniqueResponses) -- Will add it to the event that triggers when the game is initialised
        break
    end
end
 
Top Bottom