[R&F] Is it possible to change loyalty with lua? [SOLVED]

Gueux

Warlord
Joined
Mar 30, 2018
Messages
279
Location
France
Hello,

I am new at modding and I didnt found any lua function that could change the loyalty, I only found some Get.. function but when I try to use Getloyalty and GetCulturalIdentity, it doens't work and in the lua.log I get:

"lua:18: function expected instead of nil stack traceback:"


does that mean that these functions are "nil"?
Or did I mistyped?


the code :

Code:
function GetCitiesLoyalty(player)
    local pPlayer = Players[player]
    local playerCities = pPlayer:GetCities();
    for j, pCity in playerCities:Members() do
          if pCity ~= nil then   
           local currentLoyalty = pCity:GetCulturalIdentity():GetLoyalty()  -- line 18 is here
 
          end                   
    end   
end

GameEvents.PlayerTurnStarted.Add( GetCitiesLoyalty )

At the end I want to find a function that would change the city loyalty...
(Sorry if my english is bad I am not a native speaker)

Any help is welcome
 
Oups... Just found that "City:ChangeLoyalty()" was exactly what I was searching for... But always do not understand why I got the error on my previous code...
 
Ok I think that's cause I was in my script lua file.. But now I have an another question how do I use a script function I created in UI lua? (I wait for my own answer again?)
 
Look for threads that discuss „exposing Lua functions to other contexts”.
In short, either use LuaEvents or ExposedMembers global.
Also, there are reasons why some functions are exposed to only one cotext, not the other, and you should be aware of them.
 
Hi, I would resurrect this post, if possible...

After the October 2020 patch City Flipping because of Loyalty now needs a negative loyalty per turn. Before It was sufficient that the city would reach 0 loyalty.
For the base game this is good...kinda, but that breaks some mods, for example the "Loyalty matters" mod.

That mod adds some negative loyalty, based on some calculations like the domestic opposition in Civilization 5 (Tourism ratio opponents/player etc...)
fact is that even if the city reach 0 loyalty now doesn't flip...before it worked very well.

I have no experience of LUA coding, I am an amateur with some experience in Papyrus script for bethesda games.
I hoped to solve the problem forcing change the owner of the city plot when loyalty is less than 1 but it just doesn't work and surely I am missing something very obvious...




function CheckCityFlipping(PlayerID, CityID)
local city = CityManager.GetCity(PlayerID, CityID);
local pCulturalIdentity1:table = city:GetCulturalIdentity();
local currentLoyalty1 = pCulturalIdentity:GetLoyalty();
if currentLoyalty1 < 1 then
local cityPlot = Map.GetPlot(CityInstance:GetX(), CityInstance:GetY());
cityPlot:SetOwner(62,cityID,true);
end
end

function Initialize()
Events.CityLoyaltyChanged.Add(CheckCityFlipping);
end

Initialize();

Thank you for your help!
 
Solved by hooking to a different event and using the appropriate game function to flip city to free cities which is:

CityManager.TransferCityToFreeCities(CityInstance);

Note that you could also use the function to transfer the city to a specific player, but I haven't tested this one:
CityManager.TransferCity(Player);
 
Back
Top Bottom