OnKill Hook?

Darkon47

Chieftain
Joined
Feb 13, 2017
Messages
1
I am trying to make a civ with a % chance to "capture" a unit on killing it. Ideally this would be done by adding an OnKill event to units of this Civ that spawns a builder at about a 10% rate when one of your units kills an enemy unit, so in psuedocode it would be

function captureChance(attacker){
local player = Players[attacker.ID.player];
local playerConfig = PlayerConfigurations[attacker.ID.player];
if(player.GetCivilizationName() == "MY_CIV_NAME"){

if(Math.random()*100 < 15){
player.GetUnits().Create(
Integer.parse((GameInfo.Units["UNIT_BUILDER"].getIndex)),attacker.getX(), attacker.getY());
}
}
}
the problems I have are how to actually hook this function to run on an attacker killing a unit, and then to make sure I have the right syntax, I don't actually do any coding in lua, mostly just Java, with a bit of python and C++, so my syntax is probably off.
 
Code:
function OnCombat( combatResult )
    local attacker = combatResult[CombatResultParameters.ATTACKER]
    local defender = combatResult[CombatResultParameters.DEFENDER]
    if (defender[CombatResultParameters.ID].type == ComponentType.UNIT) and (defender[CombatResultParameters.FINAL_DAMAGE_TO] >= defender[CombatResultParameters.MAX_HIT_POINTS]) then
        captureChance(attacker) -- calling your function here
    end
end
Events.Combat.Add( OnCombat )

see the Events.Combat tab here to get the player ID and x, y of the attacked plot in the attacker table.

it's math.random for the syntax (and if ... then ... end), but I suppose it may cause desync in MP, there is Automation.GetRandomNumber(100) that may be safer (but not tested)
 
Top Bottom