Need lua code for simple effect.

luei333

Warlord
Joined
May 25, 2014
Messages
185
Hey there all! I'm getting to the point where I'm putting finishing touches on the mod I'm making, and one of the things I've yet to delve into is Lua coding. I did plenty of it in Civ 5, but lots of stuff has changed in 6, and I don't know where to find stuff like event hooks, etc. I was hoping someone would be so kind as to provide a simple snippet of code for me to work on, or even some pseudocode, I just need stuff like the particular hook and function to use, and I'm golden.

The effect I'm trying to make is thus: When completing a mission for a specific type of City State, gain an extra Envoy that that City State. I plan to use this as an ability for multiple leaders/civs, each with a different City State type for this effect.

Alternately, if someone could point me to a list of lua functions/hooks/etc, I'd be very grateful.
 
mmm, this might not be as simple as I was hoping. Here's my code so far:
Code:
function ZzzLua( fromPlayerID:number, toPlayerID:number)
   local toLeader:string = PlayerConfigurations[toPlayerID]:GetLeaderTypeName();
   print(toLeader);

   if not toLeader == "LEADER_LUEI_ZZZ" then return end

   local fromLeader:string = PlayerConfigurations[fromPlayerID]:GetLeaderTypeName();
   local fromLeaderInfo:table = GameInfo.Leaders[fromLeader];
   print(fromLeader);
   
   local pToPlayer = Players[toPlayerID];
   
   if (fromLeader == "LEADER_MINOR_CIV_SCIENTIFIC" or fromLeaderInfo.InheritFrom == "LEADER_MINOR_CIV_SCIENTIFIC") then
       pToPlayer:GetInfluence():ChangeTokensToGive(1);
   end
end
Events.QuestChanged.Add(ZzzLua);
This code ALMOST works, but instead of granting an envoy on completion of the quest, it grants one whenever the quest changes. So, when you meet the City State and it gives you the quest, when you complete the quest, and when the era changes, etc. I looked into ways to tell if a quest was completed, but I haven't found any appropriate events to hook into. I also looked into the questsManager (ie Game.GetQuestsManager()), but I don't see any functions related to completing the quests. I could just have some variable track whether a player has a quest from the City State and grant an envoy when they no longer do, but that also happens when a City State goes to war with a player. I suppose I could check whether the City State is at war with the player, but I feel like there must be an easier way.
 
Got it working! Almost ideally, too. The only thing it doesn't do it give the envoy to the actual city state you completed the mission for. I may try and make that change if I find the appropriate function, but I haven't found said function yet. Here's my code, for anyone interested:

Code:
local playerQuests = {}
for k, v in pairs(PlayerManager.GetWasEverAliveIDs()) do
   if PlayerConfigurations[v]:GetLeaderTypeName() == "LEADER_LUEI_SPIDER_MAN" then
       playerQuests[v] = {}
       for x, i in pairs(PlayerManager.GetWasEverAliveIDs()) do
           if PlayerConfigurations[i]:GetLeaderTypeName() ~= nil and (PlayerConfigurations[i]:GetLeaderTypeName() == "LEADER_MINOR_CIV_SCIENTIFIC"  or GameInfo.Leaders[PlayerConfigurations[i]:GetLeaderTypeName()].InheritFrom == "LEADER_MINOR_CIV_SCIENTIFIC") then
               playerQuests[v][i] = false;
               
               for questInfo in GameInfo.Quests() do
                   if Game.GetQuestsManager():HasActiveQuestFromPlayer( v, i, questInfo.Index) then
                       playerQuests[v][i] = true;
                   end
               end
           end
       end
   end
end

function SpidermanLua( fromPlayerID:number, toPlayerID:number)
   for i, v in pairs(playerQuests) do
       if toPlayerID == i then
           for j, x in pairs(playerQuests[i]) do
               if fromPlayerID == j then
                   local quest = false;
                   local questsManager   :table = Game.GetQuestsManager();
                   for questInfo in GameInfo.Quests() do
                       if questsManager:HasActiveQuestFromPlayer( i, j, questInfo.Index) then
                           quest = true;
                       end
                   end
                   if not quest and playerQuests[i][j] then
                       local pToPlayer = Players[toPlayerID];
                       if not pToPlayer:GetDiplomacy():IsAtWarWith(fromPlayerID) then
                           pToPlayer:GetInfluence():ChangeTokensToGive(1);
                       end
                   end
                   playerQuests[i][j] = quest;
               end
           end
       end
   end
end
Events.QuestChanged.Add(SpidermanLua);

This code even supports saving and loading, and (though I haven't tested it) multiple copies of the appropriate leader! Thanks much Infixo, your comment set me on the right track.
 
Top Bottom