Romek
Super Moderator
- Reaction score
- 963
TriggerGroup Functions
By Romek
By Romek
v1.1b
What is a triggergroup?
A triggergroup is something I created when making a map earlier today. I needed to disable a lot of triggers at 1 time, and thought of making some sort of system for it. Here is that system. (I also saw something similar in some wc3 extension called RTC)
A triggergroup is basically a group of triggers. 'Nough said.
The Functions:
JASS:
function CreateTriggerGroup takes nothing returns triggergroup
Simply creates a triggergroup for use.
JASS:
function DestroyTriggerGroup takes triggergroup tg returns nothing
Destroys a triggergroup to free up a slot (819 slots)
Adds a trigger to a triggergroup (Maximum of 200 per group)
Removes a trigger from a triggergroup
JASS:
function ClearTriggerGroup takes triggergroup tg returns nothing
Removes every trigger from a triggergroup
JASS:
function CountTriggersInTriggerGroup takes triggergroup tg returns integer
Counts the triggers in a triggergroup. Returns the amount as an integer.
Executes a function (As a string (Remember to use the SCOPE_PRIVATE and SCOPE_PREFIX constants!)) for every trigger in the triggergroup
Runs a function for every trigger in the triggergroup.
Use in a "ForTriggerGroup" function to get the current trigger it's looping through.
JASS:
function FirstOfTriggerGroup takes triggergroup tg returns trigger
Gets the first trigger in the triggergroup.
JASS:
function RefreshTriggerGroup takes triggergroup tg returns nothing
A problem with triggergroups is that if a trigger is destroyed, and it is still in the group, it will become a null entry for the triggergroup and take up a slot.
This function simply removes all the null entries from a group.
JASS:
function TriggerGroupOrEvaluate takes triggergroup tg returns boolean
Returns true if at least 1 of the triggers evaluates true
JASS:
function TriggerGroupAndEvaluate takes triggergroup tg returns boolean
Returns true if all the triggers evaluate true
Extension Functions:
JASS:
function RunTriggerGroup takes triggergroup tg returns nothing
Runs every trigger in the triggergroup
JASS:
function DisableTriggerGroup takes triggergroup tg returns nothing
Disables every trigger in the triggergroup
JASS:
function EnableTriggerGroup takes triggergroup tg returns nothing
Enables every trigger in the triggergroup
JASS:
function ConditionalRunTriggerGroup takes triggergroup tg returns nothing
Runs the triggers in the group which evaluate true.
JASS:
function EvaluateTriggerGroup takes triggergroup tg returns nothing
Calls the condition of every trigger in the triggergroup
How to Use:
simply declare a local (or global) variable of type "triggergroup" and use it with the above functions
JASS:
local triggergroup TG = CreateTriggerGroup()
The Code:
JASS:
library TriggerGroups
struct triggergroup [163820] // 819 * 200 (So 819 total groups, with up to 200 triggers in each)
trigger array trig [200]
integer total = 0
method GET takes integer index returns trigger
return .trig[index]
endmethod
static method create takes nothing returns triggergroup
return triggergroup.allocate()
endmethod
endstruct
globals
private triggergroup Temp
private integer index = 0
private trigger callback = CreateTrigger()
endglobals
function TriggerGroupAdd takes triggergroup tg, trigger t returns nothing
set tg.total = tg.total + 1
if tg.total > 200 then
set tg.total = tg.total - 1
debug call BJDebugMsg("|cff995500TriggerGroup Error: No more space in TriggerGroup|r")
return
endif
set tg.trig[tg.total] = t
endfunction
function FirstOfTriggerGroup takes triggergroup tg returns trigger
return tg.trig[tg.total]
endfunction
function TriggerGroupRemove takes triggergroup tg, trigger t returns nothing
local integer a = 0
loop
exitwhen a > tg.total
if tg.trig[a] == t then
set tg.trig[a] = tg.trig[tg.total]
set tg.total = tg.total - 1
return
endif
set a = a + 1
endloop
debug call BJDebugMsg("|cff995500TriggerGroup Error: Attempt to remove a trigger not in group|r")
endfunction
// Creates the triggergroup for use
function CreateTriggerGroup takes nothing returns triggergroup
return triggergroup.create()
endfunction
// Used in ForTriggerGroup to get the current looping trigger
function GetEnumTrigger takes nothing returns trigger
return Temp.trig[index]
endfunction
// Counts how many triggers are in the triggergroup
function CountTriggersInTriggerGroup takes triggergroup tg returns integer
return tg.total
endfunction
// Refreshes the triggergroup:
// - Removes null (destroyed) triggers from the triggergroup
// - sets the total to the correct amount
function RefreshTriggerGroup takes triggergroup tg returns nothing
local integer a = 0
loop
exitwhen a > tg.total
if tg.trig[a] == null then
set tg.trig[a] = tg.trig[tg.total]
set a = a - 1
set tg.total = tg.total - 1
endif
set a = a + 1
endloop
endfunction
// Clear the triggergroup
function ClearTriggerGroup takes triggergroup tg returns nothing
local integer a = 0
loop
exitwhen a > tg.total
set tg.trig[a] = null
set a = a + 1
endloop
set tg.total = 0
endfunction
// Destroys the triggergroup to free up a slot for another one.
function DestroyTriggerGroup takes triggergroup tg returns nothing
local integer a = 0
loop
exitwhen a > tg.total
set tg.trig[a] = null
set a = a + 1
endloop
set tg.total = 0
call tg.destroy()
endfunction
// Loop through every trigger in the triggergroup
function ForTriggerGroupExecute takes triggergroup tg, string FuncName returns nothing
local integer a = 0
set Temp = tg
loop
exitwhen a > tg.total
set index = a
call ExecuteFunc(FuncName)
set a = a + 1
endloop
set Temp = 0
set index = 0
endfunction
// Loop through every trigger in the triggergroup
function ForTriggerGroup takes triggergroup tg, code FuncName returns nothing
local integer a = 0
set Temp = tg
call TriggerAddAction(callback, FuncName)
loop
exitwhen a > tg.total
set index = a
call TriggerExecute(callback)
set a = a + 1
endloop
call TriggerClearActions(callback)
set Temp = 0
set index = 0
endfunction
// Returns the evaluation of all the triggers, only 1 has to be true.
function TriggerGroupOrEvaluate takes triggergroup tg returns boolean
local integer a = 0
loop
exitwhen a > tg.total
if TriggerEvaluate(tg.trig[a]) then
return true
endif
set a = a + 1
endloop
return false
endfunction
// Returns the evaluation of all the triggers, all of them have to be true.
function TriggerGroupAndEvaluate takes triggergroup tg returns boolean
local integer a = 0
loop
exitwhen a > tg.total
if TriggerEvaluate(tg.trig[a]) == false then
return false
endif
set a = a + 1
endloop
return true
endfunction
endlibrary
The Extension:
These are basically some common tasks triggergroups will be used for made into a single function instead of requiring the user to use ForTriggerGroup(..)
How to Import:
Just add the code above the "endlibrary" in the main code.
JASS:
// |======================|
// | E X T E N S I O N |
// |======================|
// Run every trigger in the group
function RunTriggerGroup takes triggergroup tg returns nothing
local integer a = 0
loop
exitwhen a > tg.total
call TriggerExecute(tg.trig[a])
set a = a + 1
endloop
endfunction
// Evaluate every trigger in the group
function EvaluateTriggerGroup takes triggergroup tg returns nothing
local integer a = 0
loop
exitwhen a > tg.total
call TriggerEvaluate(tg.trig[a])
set a = a + 1
endloop
endfunction
// Disable every trigger in the group
function DisableTriggerGroup takes triggergroup tg returns nothing
local integer a = 0
loop
exitwhen a > tg.total
call DisableTrigger(tg.trig[a])
set a = a + 1
endloop
endfunction
// Enable every trigger in the group
function EnableTriggerGroup takes triggergroup tg returns nothing
local integer a = 0
loop
exitwhen a > tg.total
call EnableTrigger(tg.trig[a])
set a = a + 1
endloop
endfunction
// Runs the triggers which evaluate true (Condition is true)
function ConditionalRunTriggerGroup takes triggergroup tg returns nothing
local integer a = 0
loop
exitwhen a > tg.total
if TriggerEvaluate(tg.trig[a]) then
call TriggerExecute(tg.trig[a])
endif
set a = a + 1
endloop
endfunction
Changelog:
Code:
V1.1b:
- Changed the ForTriggerGroup function to take a code argument
- Added a ForTriggerGroupExecute function (Which is the old ForTriggerGroup function)
V1.1:
- Added some extra functions
- Made DestroyTriggerGroup also clear the triggergroup.
V1.0:
- Initial Release
A Demo Map:
Demonstrates most of the functions in this system