[Lua] How to write normal trigger in LUA?

Status
Not open for further replies.
Level 30
Joined
Jun 11, 2017
Messages
989
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.
 
Level 45
Joined
Feb 27, 2007
Messages
5,578
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:
 
Level 30
Joined
Jun 11, 2017
Messages
989
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
 

Uncle

Warcraft Moderator
Level 73
Joined
Aug 10, 2018
Messages
7,866
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
 
Level 30
Joined
Jun 11, 2017
Messages
989
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.
Top