[BNW] Simple Lua Coding Effect (Ranged Attack dealing 0 damage)

JohnTheBaba

Chieftain
Joined
Mar 20, 2019
Messages
10
Hello dudes, first of all thank you for all the support and knowledge you provide to new members like me, who try to learn the basics of civ modding

I am trying to make a simple lua file that provides a certain effect for an air range attack: when a certain AIR unit with the ZeroDamageID promotion make an air strike, the defender will not lose any of its HP (despite unit's ranged combat strength) IN OTHER WORDS the dealing damage will be "0".

I think it is quite simple to do but I find myself struggling with lua coding so I want your help please.

Here is the code, it doesn't seem to work as it is now and I want someone to correct it:

function NewAttackEffectRanged(
iAttackingPlayer,
iAttackingUnit,
attackerDamage,
attackerFinalDamage,
attackerMaxHP,
iDefendingPlayer,
iDefendingUnit,
defenderDamage,
defenderFinalDamage,
defenderMaxHP,
plotX,
plotY
)

local pAttackingPlayer = Players[iAttackingPlayer];
local pDefendingPlayer = Players[iDefendingPlayer];
local pAttackingUnit = pAttackingPlayer:GetUnitByID(iAttackingUnit);
local pAttackingPlot = pAttackingUnit:GetPlot();

local pDefendingPlot = Map.GetPlot(plotX, plotY);
local pDefendingUnit = pDefendingPlayer:GetUnitByID(iDefendingUnit);

local ZeroDamageID = GameInfo.UnitPromotions["PROMOTION_ZERO_DAMAGE"].ID;

if pAttackingUnit:IsHasPromotion(ZeroDamageID) then
if (pDefendingUnit ~= nil) then
local DamageOri = pAttackingUnit:GetRangeCombatDamage(pDefendingUnit, nil, false)
local ZeroDamage = 0
pDefendingUnit:ChangeDamage(ZeroDamage, pAttackingPlayer)
print("Zero Damage Dealt!")
end
end
end
GameEvents.CombatResult.Add(NewAttackEffectRanged)
 
Last edited:
CombatResult is a RED dll event. Are you using that DLL (or another one with the same event)? Also, have you set the correct properties on the Lua file, such that the code will run - see link in my sig. And are there any clues in the logs - see another link in my sig.
 
Hello and thank you for your quick response! whoward69.

Yes, I am using superpowers modpack that has RED events integrated, I think.

Also, because I am using the mod's existing lua file and I've already done some other changes that had already effect, the correct properties and logs are in there. So, in my case the syntax of the lua script is what I care about in order for it to work properly.
 
pDefendingUnit:ChangeDamage(ZeroDamage, pAttackingPlayer)

Change the damage to the specified unit by 0 hit points - that's going to do nothing.

You probably want that to be

pDefendingUnit:ChangeDamage(-DamageOri, pAttackingPlayer)

that is, give the hit points back to the unit from the ranged attack
 
Your statement about red dll may be crucial! After a quick research to the gedemon's post for RED dll I realize that various dll mod components does not include that feature. But in superpower modpack's core dll is included (at least the part or the combatresult event). AOE, collateral damage and other similar effects use that feature.

So, the syntax is the only problem, the equation part, to be more specific.

Again thanks for your help, I will check it and I'll comment back!
 
Last edited:
The code seems to work but not exactly as I expected. If the enemy has full HP it doesn't work (deals the original damage of the range combat strength), Maybe I should try another way. The good thing is that the code is working as it is right now and it's not broken and the syntax is right.
 
Back
Top Bottom