• 🏆 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] Trigger with enter region event doesn't work

Status
Not open for further replies.
Level 24
Joined
Jun 26, 2020
Messages
1,852
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
 

Dr Super Good

Spell Reviewer
Level 64
Joined
Jan 18, 2005
Messages
27,201
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.
 
Level 24
Joined
Jun 26, 2020
Messages
1,852
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?
 
Level 24
Joined
Jun 26, 2020
Messages
1,852
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:

Dr Super Good

Spell Reviewer
Level 64
Joined
Jan 18, 2005
Messages
27,201
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.
Top