[Solved] The Trigger removes the unit in the wrong UnitGroup

Status
Not open for further replies.
Level 3
Joined
Jan 13, 2010
Messages
52
Hey all,

i have a problem with my following triggers:

The global variables are already created.

Lua:
function Trig_SetExplorationReward_Filter4()
        return GetOwningPlayer(GetFilterUnit()) == GetOwningPlayer(FlagUnit) and UnitAlive(GetFilterUnit()) == true and IsUnitType(GetFilterUnit(), UNIT_TYPE_HERO) == true
    end

    function CheckHeroesNearFlagTimer()
        TimerStart(NewTimer(5), 0.50, true, function()
        --////////////////////////// When i create one Flag it displays 1 in the first run. After that it removes the unit from the flag and a 0 is printed, but the unit never gets removed from this group
        DisplayTextToForce( GetPlayersAll(), I2S( CountUnitsInGroup(ExplorationRewardGroup) ))

        if FirstOfGroup(ExplorationRewardGroup) ~= nil then
            local ii1
            local locu
-- here i use a second group so i wont change the initial group
            CopyGroup = ExplorationRewardGroup
            local ExplorationRewardGroupcount = CountUnitsInGroup(CopyGroup)

            for count = ExplorationRewardGroupcount, 1, -1 do
                FlagUnit = FirstOfGroup(CopyGroup)
                locu = GetUnitLoc(FlagUnit)
                UnitsAroundFlagsGroup = GetUnitsInRangeOfLocMatching(400, locu, Condition(Trig_SetExplorationReward_Filter4))
-- this if Block is false and doesnt get excecuted which is correct
                if FirstOfGroup(UnitsAroundFlagsGroup) ~= nil then
                    ii1 = GetUnitUserData(FlagUnit)
                    PlaySoundAtPointBJ(udg_ErrorSound, 100, locu, 2.275)
                    --/////////////////////////////////////////////////////////
                    ExplorationReward = LoadInteger(udg_BountyHash, 2, ii1)
                    --/////////////////////////////////////////////////////////
                    --//this isnt even called So nowhere in the trigger the unit gets removed from the group
                    GroupRemoveUnit(ExplorationRewardGroup, FlagUnit)
                    RemoveUnit(FlagUnit)
                end
                --////////when i comment this line out
                GroupRemoveUnit(CopyGroup, FlagUnit)
            end
            GroupClear(UnitsAroundFlagsGroup)
            --////////and comment this out too. it counts 1 all the time right. but i never remove a unit from  ExplorationRewardGroup
            GroupClear(CopyGroup)
            end

        end)
    end

Anyone knows why the ExplorationRewardGroup gets changed?
 
Last edited:

Uncle

Warcraft Moderator
Level 73
Joined
Aug 10, 2018
Messages
7,866
print"test" should be print("test"), that's why the 2 lines below it aren't being called.

Also, i'm assuming you created CopyGroup, UnitsAroundFlagsGroup, FlagUnit, ExplorationReward as global variables somewhere else.

And you probably have a reason for doing this but I figured I'd say this anyway. If you're using Lua you don't need to use Hashtables/Custom Value. You can use any handle as the Index in your tables.
Example:
Lua:
function newAutotable(dim) --Creates an X dimensional table
    local MT = {};
    for i=1, dim do
        MT[i] = {__index = function(t, k)
            if i < dim then
                t[k] = setmetatable({}, MT[i+1])
                return t[k];
            end
        end}
    end
    return setmetatable({}, MT[1]);
end


function Example()
    --Create a 2 dimensional array (table)
    Bounty = newAutotable(2)
    local u = CreateUnit(Player(0), FourCC('Hpal'), 0, 0, 0)
    Bounty[u][2] = 100 --Sets the Bounty for our unit to 100
end
 
Last edited:
Level 3
Joined
Jan 13, 2010
Messages
52
The line print"test" prints test Ingame and is alright when i am testing. the fault was before i used this line to check if it goes into the if block. So the print call isnt the fault.

The meaning of this lines:
--//this isnt even called
were that i wanted to underline that the call GroupRemoveUnit(ExplorationRewardGroup, FlagUnit) isnt even called because the if block is false.

So why does this line prints:
Lua:
DisplayTextToForce( GetPlayersAll(), I2S( CountUnitsInGroup(ExplorationRewardGroup) ))
"1" in the first run and
In the second time the line prints "0".

I created the global variables somewhere else. Thats correct.

I am learning lua atm. I just used hashtable from gui because i transferred my gui trigger to lua.

Thanks for the exampel.i will change that in the future.

But when the group (ExplorationRewardGroup) is never cleared or destroyed why is it empty after the first run of the trigger and isnt empty when i comment this
Lua:
GroupRemoveUnit(CopyGroup, FlagUnit)
And this
Lua:
GroupClear(CopyGroup)
Out.

Why does the copygroup affects the other group (ExplorationRewardGroup).
 
Last edited:
Level 3
Joined
Jan 13, 2010
Messages
52
Ok I solved the problem.

The function wasnt able to handle the variables in the Timer Function.

I started to call my function in the timer and everything went fine.

Lua:
do

    ExplorationRewardGroup = CreateGroup()
    ExplorationReward = 0
    FlagUnit = nil
    ExplorationBounty = {}

    function Trig_SetExplorationReward_Filter4()
        return GetOwningPlayer(GetFilterUnit()) == GetOwningPlayer(FlagUnit) and UnitAlive(GetFilterUnit()) == true and IsUnitType(GetFilterUnit(), UNIT_TYPE_HERO) == true
    end
   
    function CheckHeroesNearFlag()

        if FirstOfGroup(ExplorationRewardGroup) ~= nil then
        local locu
        local CopyGroup = CreateGroup()
        local UnitsAroundFlagsGroup = CreateGroup()
        GroupAddGroup(ExplorationRewardGroup, CopyGroup)
        local ERGcount = CountUnitsInGroup(CopyGroup)

            for x = ERGcount, 1, -1 do
                FlagUnit = FirstOfGroup(CopyGroup)
                locu = GetUnitLoc(FlagUnit)
                UnitsAroundFlagsGroup = GetUnitsInRangeOfLocMatching(400, locu, Condition(Trig_SetExplorationReward_Filter4))

                if FirstOfGroup(UnitsAroundFlagsGroup) ~= nil then
                    PlaySoundAtPointBJ(udg_ErrorSound, 100, locu, 2.275)
                    --/////////////////////////////////////////////////////////
                    ExplorationReward = ExplorationBounty[FlagUnit]
                    --/////////////////////////////////////////////////////////
                    GroupRemoveUnit(ExplorationRewardGroup, FlagUnit)
                    RemoveUnit(FlagUnit)
                    GroupClear(UnitsAroundFlagsGroup)
                else
                    GroupRemoveUnit(CopyGroup, FlagUnit)
                end
             end
             DestroyGroup(CopyGroup)
             DestroyGroup(UnitsAroundFlagsGroup)
        end
    end

    function CheckHeroesNearFlagTimer()
        TimerStart(NewTimer(5), 0.5, true, function()
        CheckHeroesNearFlag()
        end)
    end
end

*SOLVED*
 
Status
Not open for further replies.
Top