• 🏆 Texturing Contest #33 is OPEN! Contestants must re-texture a SD unit model found in-game (Warcraft 3 Classic), recreating the unit into a peaceful NPC version. 🔗Click here to enter!
  • It's time for the first HD Modeling Contest of 2024. Join the theme discussion for Hive's HD Modeling Contest #6! Click here to post your idea!

Questions about the use of "if"

Status
Not open for further replies.
Level 3
Joined
Aug 6, 2019
Messages
74
Does using more than one "if" in a function cause problems?
Here's the code:
[lua]
function UnitDeath(u)
u=GetTriggerUnit()
local id=GetUnitTypeId()

if id==FourCC("h000") then
Action1(u)
end

if id==FourCC("h001") then
Action2(u)
end

...

if id==FourCC("h060") then
Action60(u)
end

end
 
You can create a table that holds the functions beeing executed when an unit of UnitCode is killed. Inside an deathEvent check if you saved one for that unitCode and if there is execute it.
Could look like that, does not include Setting up the Event.
Lua:
do
    UnitDeathAction = {} --create the table
    UnitDeathAction[FourCC('Hpal')] = function(unit) --Setup an actionfunction for Paladin Units
        print("Paladin was killed")
    end
 
    function DeathEvent()
        local unit = GetTriggerUnit()
        local unitCode = GetUnitTypeId(unit)
        if UnitDeathAction[unitCode] then --is there a function for this unitCode?
            UnitDeathAction[unitCode](unit) --execute it and give the unit.
        end

    end

end
 
Last edited:
Status
Not open for further replies.
Top