I'd like to make a function that if my unit that has certain promotion kill enemy, I get science.

GreatBayethe

Chieftain
Joined
Jul 12, 2017
Messages
24
I made a code, and it seemed to work well. But, after my units that has the promotion kill enemies and get science, when my units that don't have the promotion kill enemies, I get science. What is the problem? Please help me.

Code:
function GuardogScienceBoostUnit(iKiller, iKilled, unitType)
    local pPlayer = Players[iKiller]
    if pPlayer:IsAlive() and pPlayer:GetCivilizationType() == pJimmyNeutron then
        for pMyUnit in pPlayer:Units() do
            if pMyUnit:IsHasPromotion(pGuardogPromotion) then
                local iScienceboost = math.max(GameInfo.Units[unitType].Combat, GameInfo.Units[unitType].RangedCombat)
                local pTeamTechs = Teams[pPlayer:GetTeam()]:GetTeamTechs()
                pTeamTechs:ChangeResearchProgress(pPlayer:GetCurrentResearch(), iScienceboost, pPlayer)
            end
        end
    end
end
GameEvents.UnitKilledInCombat.Add(GuardogScienceBoostUnit)
 
You're not specifying the unit that was killed. You're going through all of your units to check if any of them has the promotion regardless of which unit killed. You need to get unitID and then Unit from unitType and check from there, not go through a for loop for every unit in your empire. Now, I'm not completely sure whether unitType specifies the unittype that was killed or the unittype that killed (i think its the one that was killed), but regardless you can't get the combat strength of the unit that was killed while still getting the promotion of the unit that killed, at least not in that way. How you do that is beyond me, sorry. (Maybe check the plot of the unit that was killed and check which unit is on that plot now, but that would only work for melee units)
I'm bad at explaining and pretty new to this too, but I hope you understood some of it :p
 
You cannot determine which individual unit on the game-map killed which other individual unit on the game-map from GameEvents.UnitKilledInCombat()

It does not pass this information. It merely passes the player ID#s for the victor and the vanquished, and the type of unit from table <Units> that was killed. So the event will also fire if a city kills off an enemy unit.
 
Back
Top Bottom