LeeS
Imperator
To a Player or a City, yes.
But the modifier cannot be un-attached by lua.
Usable Toolkit function with an example of how to enact the function. In this case lua player # 0 (ie, the human player) would get the modifier called "FARMS_INCREASED_PRODUCTION_TRAIT_X" added to it. The modifier must first be defined in the normal way via either XML or SQL into the game's database. The text-string for the ModifierID must exactly match between that which is used in the Database and that which is used in the lua script.
The toolkit function also records whether or not the modifier has already been applied to that player by the toolkit function, and will not attempt a second application of the same modifier no matter how many times the same data is fed into the function by an lua script, or by saving and reloading a game.
The final argument for the toolkit function is a Boolean to turn ON/OFF printing into the lua log for debugging and confirmation purposes.
------------------------------------------------------------------------------
For units there is no direct way to attach a modifier to a unit, but we can (though I have not tested all of this as yet) attach and un-attach Unit Abilities which is a bit more complicated lua-codewise. Since UnitAbilities all have modifiers associated with them, altering the unit's "Unit Ability Count" for a given UnitAbility from "0" to "1", or vice versa would have the same net effect as adding and then later removing a modifier.
But the modifier cannot be un-attached by lua.
Code:
function AddModifierToPlayer(iPlayer, sModifierID, bDebugPrint)
local function Dprint(sMessage)
if bDebugPrint then print(sMessage) end
end
Dprint("function AddModifierToPlayer was executed with argument values of iPlayer = " .. tostring(iPlayer) .. ", sModifierID = " .. tostring(sModifierID) .. ", bDebugPrint = " .. tostring(bDebugPrint))
local pPlayer = Players[iPlayer]
if (pPlayer ~= nil) then
Dprint("AddModifierToPlayer(" .. tostring(iPlayer) .. ", " .. tostring(sModifierID) .. ", " .. tostring(bDebugPrint) .. "): Proceeding with evaluating whether the player already has the modifier.")
local PlayerProperty = pPlayer:GetProperty(sModifierID)
Dprint("pPlayer:GetProperty(" .. sModifierID .. ") == " .. tostring(PlayerProperty))
if (PlayerProperty == nil) then
Dprint("Modifier " .. sModifierID .. " was seen as NOT being already attached to the player")
pPlayer:AttachModifierByID(sModifierID)
pPlayer:SetProperty(sModifierID, 1)
Dprint("Modifier " .. sModifierID .. " attached to the player")
else
Dprint("Modifier " .. sModifierID .. " was seen as being already attached to the player")
end
else
Dprint("AddModifierToPlayer(" .. tostring(iPlayer) .. ", " .. tostring(sModifierID) .. ", " .. tostring(bDebugPrint) .. ") failed because the iPlayer value is not usable")
end
end
-- how to use the toolkit function
AddModifierToPlayer(0, "FARMS_INCREASED_PRODUCTION_TRAIT_X", false)
The toolkit function also records whether or not the modifier has already been applied to that player by the toolkit function, and will not attempt a second application of the same modifier no matter how many times the same data is fed into the function by an lua script, or by saving and reloading a game.
The final argument for the toolkit function is a Boolean to turn ON/OFF printing into the lua log for debugging and confirmation purposes.
------------------------------------------------------------------------------
For units there is no direct way to attach a modifier to a unit, but we can (though I have not tested all of this as yet) attach and un-attach Unit Abilities which is a bit more complicated lua-codewise. Since UnitAbilities all have modifiers associated with them, altering the unit's "Unit Ability Count" for a given UnitAbility from "0" to "1", or vice versa would have the same net effect as adding and then later removing a modifier.