[Lua] How to write normal trigger in LUA?

Status
Not open for further replies.
Level 35
Joined
Jun 11, 2017
Messages
1,243
Lua:
do
  local trigger = CreateTrigger()
  TriggerRegisterAnyUnitEventBJ(trigger, EVENT_UNIT_DEATH)
  TriggerAddCondition(trigger, Condition(function()
  end))
  TriggerAddAction(trigger, function()
    print("Works")
  end)
end


I have map only with that code. Why my map crashes to main screen and not loading?
Latest version of Reforged.
 
It's advised not to use a raw, unscoped do ... end in WC3 Lua because sometimes data declared there get garbage collected erroneously. Instead you should use something like this:
 
It's advised not to use a raw, unscoped do ... end in WC3 Lua because sometimes data declared there get garbage collected erroneously. Instead you should use something like this:
Thanks for advice.
As I undestood correctly, more proper version of my custom script should be next:

Lua:
do
 OnInit()
   local trigger = CreateTrigger()
   TriggerRegisterAnyUnitEventBJ(trigger, EVENT_UNIT_DEATH)
   TriggerAddCondition(trigger, Condition(function()
   end))
   TriggerAddAction(trigger, function()
     print("Works")
    end)
end
 
I don't even bother using Conditions in Lua. Just throw an If Then Else in your Action and filter things there, although maybe that's slower?
Lua:
do
 OnInit()

   local trigger = CreateTrigger()
   TriggerRegisterAnyUnitEventBJ(trigger, EVENT_UNIT_DEATH)

   TriggerAddAction(trigger, function()
       local u = GetTriggerUnit()
       if GetUnitLevel(u) <= 5 then
           print("it works")
       end
    end)

end
 
I don't even bother using Conditions in Lua. Just throw an If Then Else in your Action and filter things there, although maybe that's slower?
Lua:
do
 OnInit()
   local trigger = CreateTrigger()
   TriggerRegisterAnyUnitEventBJ(trigger, EVENT_UNIT_DEATH)

   TriggerAddAction(trigger, function()
       local u = GetTriggerUnit()
       if GetUnitLevel(u) <= 5 then
           print("it works")
       end
    end)
end
I agree with you, was trying to simulate standard GUI trigger in LUA, adding all fields (event, condition, action).
However I found where issue was - it was in event, which I used uncorrectly.
Event should be
Lua:
 EVENT_PLAYER_UNIT_DEATH
 
Status
Not open for further replies.
Back
Top