• Check out the results of the Techtree Contest #19!
  • Listen to a special audio message from Bill Roper to the Hive Workshop community (Bill is a former Vice President of Blizzard Entertainment, Producer, Designer, Musician, Voice Actor) 🔗Click here to hear his message!
  • Read Evilhog's interview with Gregory Alper, the original composer of the music for WarCraft: Orcs & Humans 🔗Click here to read the full interview.
  • Create a void inspired texture for Warcraft 3 and enter Hive's 34th Texturing Contest: Void! Click here to enter!
  • The Hive's 22nd Icon Contest: Creep Abilities is now concluded, time to vote for your favourite set of icons! Click here to vote!

[Lua] Trigger with enter region event doesn't work

Status
Not open for further replies.
Level 25
Joined
Jun 26, 2020
Messages
1,971
I made a system to store the color of the unit (the actual, not the color of the player) and it should have a trigger for the units those apear in the map (and th user pre-placed in the editor), but it doesn't do anything, what's wrong?
Lua:
do

    --This library is to store the actual color of a unit and also if is changed.
    --Also add a constant PLAYER_COLOR_BLACK that you can use and a function GetUnitColor to have the saved unit color.

    LIBRARY_StoreUnitColor=true
    PLAYER_COLOR_BLACK=ConvertPlayerColor(PLAYER_NEUTRAL_AGGRESSIVE) ---@type playercolor
    
    local UnitColor={} ---@type playercolor
    
    local oldinit=InitBlizzard
    function InitBlizzard()
        local re=CreateRegion()
        local r=GetWorldBounds()
        RegionAddRect(re,r)
        RemoveRect(r)
        TriggerRegisterEnterRegion(CreateTrigger(),re,Filter(function()
            print(GetFilterUnit()) --Doesn't print nothing
            UnitColor[GetFilterUnit()]=GetPlayerColor(GetOwningPlayer(GetFilterUnit()))
            return false
        end))
        oldinit()
    end
    
    --I just had to change these 2 functions.

    local OldSetUnitColor=SetUnitColor
    ---@param whichUnit unit
    ---@param color playercolor
    function SetUnitColor(whichUnit,color)
        UnitColor[whichUnit]=color
        OldSetUnitColor(whichUnit,color)
    end

    local OldSetUnitOwner=SetUnitOwner
    ---@param whichUnit unit
    ---@param whichPlayer player
    ---@param changeColor boolean
    function SetUnitOwner(whichUnit,whichPlayer,changeColor)
        if changeColor then
            UnitColor[whichUnit]=GetPlayerColor(whichPlayer)
        end
        OldSetUnitOwner(whichUnit,whichPlayer,changeColor)
    end
    
    ---@param whichUnit unit
    ---@return playercolor
    function GetUnitColor(whichUnit)
        return UnitColor[whichUnit]
    end
end
 
Triggers, events and trigger action/conditions objects created in the global/root scope, including functions called from that scope, are (were last I checked...) subject to being garbage collected and so broken unless a reference was kept to them. As such all triggers, events and trigger action/condition objects should rather be created inside a function called from main rather than the global/root scope.
 
Triggers, events and trigger action/conditions objects created in the global/root scope, including functions called from that scope, are (were last I checked...) subject to being garbage collected and so broken unless a reference was kept to them. As such all triggers, events and trigger action/condition objects should rather be created inside a function called from main rather than the global/root scope.
Sorry, but, how?
 
You mean something like this?:
Lua:
function Init_StoreUnitColor()
    local re=CreateRegion()
    local r=GetWorldBounds()
    RegionAddRect(re,r)
    RemoveRect(r)
    TriggerRegisterEnterRegion(CreateTrigger(),re,Filter(function()
        print(GetFilterUnit())
        UnitColor[GetFilterUnit()]=GetPlayerColor(GetOwningPlayer(GetFilterUnit()))
        return false
    end))
end
  • Untitled Trigger 001
    • Events
      • Map initialization
    • Conditions
    • Actions
      • Custom script: Init_StoreUnitColor()
Because I wanted avoid that, and didn't even work.
 
Last edited:
Insert debug text and execute the functions with an exception handler. With Lua it is very easy to run into type or other runtime errors that were not possible with statically typed languages like JASS.

Otherwise you will need to provide a map with the script in it for debugging, which allows the system to be easily tested with minimal user interaction required (test map and it should be visible if it works or does not work).
 
Status
Not open for further replies.
Back
Top