• 🏆 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!

Lua Question

Status
Not open for further replies.
Level 2
Joined
Apr 1, 2019
Messages
156
I can't seem to get this to work, all it ever prints is "2" gets stuck on
if IsUnitType(u, UNIT_TYPE_STRUCTURE) == false then

Lua:
function PortalActions (L, PG)
    print ("2")
    local u = GetTriggeringUnit()
    local n = GetUnitTypeId(u)
    if IsUnitType(u, UNIT_TYPE_STRUCTURE) == false then
        print ("true 1")
        if IsUnitInGroup(u, udg_PortalUnitGroup[PG]) == false then
        print ("true 2")
        if n ~= FourCC('n058') then
            print ("true 3")
            if n ~= FourCC('n054') then
                print ("true 4")
                if n ~= FourCC('n055') then
                    print ("true 5") 
                    if n ~= FourCC('n052') then
                        print ("true 6")
                        if n ~= FourCC('n051') then
                            print ("true 7")
                            if n ~= FourCC('n053') then
                                print ("true 8")
                                if n ~= FourCC('n057') then
                                    print ("true 9")
                                    if n ~= FourCC('n056') then
                                        print ("true 10")
                                        if n ~= FourCC('n04A') then
                                            print ("good")
                                            local c = GetUnitUserData(u)
                                            for P = 0, 11 do
                                                if c >=  1 + (P * 6 ) and
                                                c <= 6 + (P * 6 ) then
                                                    if L % 2 == 0 then
                                                        local Int = L + (P * 10)
                                                        local Snd = Int - 1
                                                    else
                                                        local Int = L + (P * 10)
                                                        local Snd = Snd + 1
                                                    end
                                                    print ("loop")
                                                    GroupAddUnitSimple( u, udg_PortalUnitGroup[PG] )
                                                    AddSpecialEffectLocBJ(udg_PortalPoint[Int], "Abilities\\Spells\\NightElf\\Blink\\BlinkCaster.mdl")
                                                    DestroyEffectBJ( GetLastCreatedEffectBJ() )
                                                    AddSpecialEffectLocBJ( udg_PortalPoint[Snd], "Abilities\\Spells\\NightElf\\Blink\\BlinkCaster.mdl" )
                                                    DestroyEffectBJ( GetLastCreatedEffectBJ() )
                                                    SetUnitX(u, GetLocationX(udg_PortalPoint[Snd]))
                                                    SetUnitY(u, GetLocationY(udg_PortalPoint[Snd]))
                                                    AddLightningLoc( FourCC("MFPB"), udg_PortalPoint[Int], udg_PortalPoint[Snd])
                                                    local light = GetLastCreatedLightningBJ()
                                                    SetLightningColorBJ(light, 0.10, 0.10, 1, 1)
                                                    local t = CreateTimer()
                                                    TimerStart(t, 0.20, false, function()
                                                        DestroyLightning(light)
                                                        PauseTimer(t)
                                                        DestroyTimer(t)
                                                    end)
                                            break
                                                end
                                            end
                                        end
                                    end
                                end
                            end
                        end
                    end
                end
            end
        end
        end
    end
end
 
Level 2
Joined
Apr 1, 2019
Messages
156
I run PortalInit on initialization. Then I run PortalEvent at game time 0 after portals are created and assigned to variables.

Lua:
function PortalInit ()
        gg_trg_Portal1 = CreateTrigger(  )
        TriggerAddAction(gg_trg_Portal1, function ()
        print ("1")
        PortalActions (1, 1)
    end)
        gg_trg_Portal2 = CreateTrigger(  )
        TriggerAddAction(gg_trg_Portal2, function ()
        print ("1")
        PortalActions (2, 1)
    end)
        gg_trg_Portal3 = CreateTrigger(  )
        TriggerAddAction(gg_trg_Portal3, function ()
        print ("1")
        PortalActions (3, 2)
    end)
        gg_trg_Portal4 = CreateTrigger(  )
        TriggerAddAction(gg_trg_Portal4, function ()
        print ("1")
        PortalActions (4, 2)
    end)
        gg_trg_Portal5 = CreateTrigger(  )
        TriggerAddAction(gg_trg_Portal5, function ()
        PortalActions (5, 3)
    end)
        gg_trg_Portal6 = CreateTrigger(  )
        TriggerAddAction(gg_trg_Portal6, function ()
        print ("1")
        PortalActions (6, 3)
    end)
        gg_trg_Portal7 = CreateTrigger(  )
        TriggerAddAction(gg_trg_Portal7, function ()
        print ("1")
        PortalActions (7, 4)
    end)
        gg_trg_Portal8 = CreateTrigger(  )
        TriggerAddAction(gg_trg_Portal8, function ()
        print ("1")
        PortalActions (8, 4)
    end)
        gg_trg_Portal9 = CreateTrigger(  )
        TriggerAddAction(gg_trg_Portal9, function ()
        print ("1")
        PortalActions (9, 5)
    end)
        gg_trg_Portal10 = CreateTrigger(  )
        TriggerAddAction(gg_trg_Portal10, function ()
        print ("1")
        PortalActions (10, 5)
    end)
end

function PortalEvent ()
    for P = 0, 11 do
        TriggerRegisterUnitInRangeSimple( gg_trg_Portal1, 100.00, udg_PortalUnitTrue[1 + (P * 10 )] )
        TriggerRegisterUnitInRangeSimple( gg_trg_Portal2, 100.00, udg_PortalUnitTrue[2 + (P * 10 )] )
        TriggerRegisterUnitInRangeSimple( gg_trg_Portal3, 100.00, udg_PortalUnitTrue[3 + (P * 10 )] )
        TriggerRegisterUnitInRangeSimple( gg_trg_Portal4, 100.00, udg_PortalUnitTrue[4 + (P * 10 )] )
        TriggerRegisterUnitInRangeSimple( gg_trg_Portal5, 100.00, udg_PortalUnitTrue[5 + (P * 10 )] )
        TriggerRegisterUnitInRangeSimple( gg_trg_Portal6, 100.00, udg_PortalUnitTrue[6 + (P * 10 )] )
        TriggerRegisterUnitInRangeSimple( gg_trg_Portal7, 100.00, udg_PortalUnitTrue[7 + (P * 10 )] )
        TriggerRegisterUnitInRangeSimple( gg_trg_Portal8, 100.00, udg_PortalUnitTrue[8 + (P * 10 )] )
        TriggerRegisterUnitInRangeSimple( gg_trg_Portal9, 100.00, udg_PortalUnitTrue[9 + (P * 10 )] )
        TriggerRegisterUnitInRangeSimple( gg_trg_Portal10, 100.00, udg_PortalUnitTrue[10 + (P * 10 )] )
    end
end
 
Level 2
Joined
Apr 1, 2019
Messages
156
Thanks that was it, sorry I'm new to writting in Lua, now the lightning effect isn't appearing or the blink special effect. They also Tp to near the center of the map
Also turns out I had to use ModuloInteger(L, 2) == 0 instead of L % 2 == 0

but anyway here's my gui trigger of the same thing which does work.

  • Portal B1
    • Events
    • Conditions
      • ((Triggering unit) is A structure) Equal to False
      • ((Triggering unit) is in PortalUnitGroup[2]) Equal to False
      • (Unit-type of (Triggering unit)) Not equal to Asteroid of Annhilation [Bonus]
      • (Unit-type of (Triggering unit)) Not equal to Asteroid of Depravity [Bonus]
      • (Unit-type of (Triggering unit)) Not equal to Asteroid of Energy [Bonus]
      • (Unit-type of (Triggering unit)) Not equal to Asteroid of Protection [Bonus]
      • (Unit-type of (Triggering unit)) Not equal to Asteroid of Reconstruction [Bonus]
      • (Unit-type of (Triggering unit)) Not equal to Asteroid of Velocity [Bonus]
      • (Unit-type of (Triggering unit)) Not equal to Asteroid of the Fortress [Bonus]
      • (Unit-type of (Triggering unit)) Not equal to Asteroid of the Moon [Bonus]
      • (Unit-type of (Triggering unit)) Not equal to Unhallowed Asteroid of Evil [Bonus]
    • Actions
      • Unit Group - Add (Triggering unit) to PortalUnitGroup[2]
      • For each (Integer A) from 1 to 12, do (Actions)
        • Loop - Actions
          • If (All Conditions are True) then do (Then Actions) else do (Else Actions)
            • If - Conditions
              • (Custom value of (Triggering unit)) Greater than or equal to (1 + (((Integer A) - 1) x 6))
              • (Custom value of (Triggering unit)) Less than or equal to (6 + (((Integer A) - 1) x 6))
            • Then - Actions
              • Special Effect - Create a special effect at PortalPoint[(3 + (((Integer A) - 1) x 10))] using Abilities\Spells\NightElf\Blink\BlinkCaster.mdl
              • Special Effect - Destroy (Last created special effect)
              • Special Effect - Create a special effect at PortalPoint[(4 + (((Integer A) - 1) x 10))] using Abilities\Spells\NightElf\Blink\BlinkCaster.mdl
              • Special Effect - Destroy (Last created special effect)
              • Unit - Move (Triggering unit) instantly to PortalPoint[(4 + (((Integer A) - 1) x 10))]
              • Lightning - Create a Mana Flare lightning effect from source PortalPoint[(3 + (((Integer A) - 1) x 10))] to target PortalPoint[(4 + (((Integer A) - 1) x 10))]
              • Set PortalPointLighningInt = (PortalPointLighningInt + 1)
              • Set PortalPointLightning[PortalPointLighningInt] = (Last created lightning effect)
              • Lightning - Change color of (Last created lightning effect) to (0.10 0.10 1.00) with 1.00 alpha
            • Else - Actions
      • For each (Integer A) from 1 to 12, do (Actions)
        • Loop - Actions
          • If (All Conditions are True) then do (Then Actions) else do (Else Actions)
            • If - Conditions
              • (Custom value of (Triggering unit)) Equal to (1 + (((Integer A) - 1) x 6))
            • Then - Actions
              • Unit - Order (Triggering unit) to Move To PointWaypointUnit[(1 + (((Integer A) - 1) x 5))]
            • Else - Actions
          • If (All Conditions are True) then do (Then Actions) else do (Else Actions)
            • If - Conditions
              • (Custom value of (Triggering unit)) Equal to (2 + (((Integer A) - 1) x 6))
            • Then - Actions
              • Unit - Order (Triggering unit) to Move To PointWaypointUnit[(2 + (((Integer A) - 1) x 5))]
            • Else - Actions
          • If (All Conditions are True) then do (Then Actions) else do (Else Actions)
            • If - Conditions
              • (Custom value of (Triggering unit)) Equal to (3 + (((Integer A) - 1) x 6))
            • Then - Actions
              • Unit - Order (Triggering unit) to Move To PointWaypointUnit[(3 + (((Integer A) - 1) x 5))]
            • Else - Actions
          • If (All Conditions are True) then do (Then Actions) else do (Else Actions)
            • If - Conditions
              • (Custom value of (Triggering unit)) Equal to (4 + (((Integer A) - 1) x 6))
            • Then - Actions
              • Unit - Order (Triggering unit) to Move To PointWaypointUnit[(4 + (((Integer A) - 1) x 5))]
            • Else - Actions
          • If (All Conditions are True) then do (Then Actions) else do (Else Actions)
            • If - Conditions
              • (Custom value of (Triggering unit)) Equal to (5 + (((Integer A) - 1) x 6))
            • Then - Actions
              • Unit - Order (Triggering unit) to Move To PointWaypointUnit[(5 + (((Integer A) - 1) x 5))]
            • Else - Actions
          • If (All Conditions are True) then do (Then Actions) else do (Else Actions)
            • If - Conditions
              • (Custom value of (Triggering unit)) Equal to (6 + (((Integer A) - 1) x 6))
            • Then - Actions
              • Unit - Order (Triggering unit) to Move To PointRegionArrow[(Integer A)]
            • Else - Actions
      • Wait 0.20 seconds
      • For each (Integer A) from 1 to 12, do (Actions)
        • Loop - Actions
          • If (All Conditions are True) then do (Then Actions) else do (Else Actions)
            • If - Conditions
              • (Custom value of (Triggering unit)) Greater than or equal to (1 + (((Integer A) - 1) x 6))
              • (Custom value of (Triggering unit)) Less than or equal to (6 + (((Integer A) - 1) x 6))
            • Then - Actions
              • Set PortalPointLightningInt2 = (PortalPointLightningInt2 + 1)
              • Custom script: call DestroyLightning(udg_PortalPointLightning[udg_PortalPointLightningInt2])
              • If (All Conditions are True) then do (Then Actions) else do (Else Actions)
                • If - Conditions
                  • PortalPointLighningInt Equal to PortalPointLightningInt2
                • Then - Actions
                  • Set PortalPointLighningInt = 0
                  • Set PortalPointLightningInt2 = 0
                • Else - Actions
            • Else - Actions
I like your thread title btw
 
Level 2
Joined
Apr 1, 2019
Messages
156
Thx fellas,
Found out "FourCC" isn't needed in lightning
This doesn't work
AddSpecialEffectLocBJ
This does
AddSpecialEffectLoc

I hear BJ's are bad anyway so I guess that's good, but personally I like BJ's

Also I had to declare a few locals at the start of the function X and Y were returning 0.0 or nil


Another question, is there anyway to create trigger arrays? If possible it can really slim down my code.

Also I don't see a way to remove events from triggers, if I just destroy the trigger then recreate will that leak anything?
 

Wrda

Spell Reviewer
Level 26
Joined
Nov 18, 2012
Messages
1,887
Lua:
array = {CreateTrigger(), CreateTrigger()} -- create table, and elements in table (array)
--do something
EnableTrigger(array[1])
print(array[1])
--or

array = {} -- create table
--and elements in table (array)
array[1] = CreateTrigger()
array[2] = CreateTrigger()
--do something
EnableTrigger(array[1])
print(array[1])

--loop indexes
for i, v in ipairs(array) do print(i, v) end
You can also have strings as keys.
lua-users wiki: Tables Tutorial
 
Level 2
Joined
Apr 1, 2019
Messages
156
Hey one more question, I can't get this to run the function
Lua:
function PortalInit ()
        gg_trg_Portal1 = CreateTrigger(  )
        TriggerAddAction(gg_trg_Portal1, PortalActions (1, 1))
end
But if I do this this it works
Lua:
function PortalInit ()
        gg_trg_Portal1 = CreateTrigger(  )
        TriggerAddAction(gg_trg_Portal1, function ()
        PortalActions (1, 1)
    end)
end
Is the second code however slower because it has an additional function?
 
"TriggerAddAction(gg_trg_Portal1, PortalActions (1, 1))"
the game expects only a reference to a function. That line would tell Lua to to run PortalActions with arguments of 1 and 1. And use the returned value of PortalActions as TriggerAction, if that returned value is not a function you get a problem.
 
Status
Not open for further replies.
Top