• 🏆 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!
  • 🏆 Hive's 6th HD Modeling Contest: Mechanical is now open! Design and model a mechanical creature, mechanized animal, a futuristic robotic being, or anything else your imagination can tinker with! 📅 Submissions close on June 30, 2024. Don't miss this opportunity to let your creativity shine! Enter now and show us your mechanical masterpiece! 🔗 Click here to enter!

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