Builder Bob
Live free or don't
- Reaction score
- 249
What is this?
Why?
Blue Flavor:
Red Flavor:
I'd consider this a snippet as it'll probably make more sense to just code it directly into other systems, than to call these functions. Use whatever works best for you.
ExecuteSoon() will execute a function within 0 seconds, but not right away.
This allows all the current fired events to finish before executing your function.
This allows all the current fired events to finish before executing your function.
Why?
Some events have after effects. Here are just a few examples.
- Functions triggered by damage events will finish before any damage is dealt.
- Functions triggered when moving an item in the inventory (order event) will finish before the item has actually moved.
Blue Flavor:
JASS:
//==============================================================================
// ExecuteSoon (Blue flavor) -- by Builder Bob -- v1.1
//==============================================================================
//==============================================================================
// FUNCTION INDEX:
//==============================================================================
//
// -------------------------------------------------------------
//
// ExecuteSoon(condition, data)
//
// GetExecuteSoonData()
// returns integer
//
//-----------------------------------------------------------------------------
//==============================================================================
// DOCUMENTATION:
//==============================================================================
//
// WHAT IS THIS?
// * ExecuteSoon() will execute a function within 0 seconds, but not right away.
// This allows all the current fired events to finish before executing your function
//
// BLUE FLAVOR PROS:
// * More user friendly.
//
// * GetExecuteSoonData() is optional, rather than mandatory.
//
// * Consequtive ExecuteSoon calls within functions called via ExecuteSoon
// will also trigger after other queued functions finish, as designed.
//
// BLUE FLAVOR CONS:
// * Slightly less efficient.
//
// DETAILS:
// * The function passed as argument in ExecuteSoon must be made a condition
// like Condition(function whichFunction)
//
// * The condition used as argument in ExecuteSoon MUST return a boolean.
// true or false is irrelevant.
//
// * GetExecuteSoonData() is optional, and will return the data you pass
// along with the queued function
//
// THANKS TO:
// * Cohadar for good documentation standard
//
// HOW TO IMPORT:
// * create a trigger
// * convert it to text and replace the whole trigger text with this one
//
//==============================================================================
library ExecuteSoon initializer onInit
private struct Data
Data next
trigger trig
integer data
endstruct
globals
private timer Timer = CreateTimer()
private Data First = 0
private Data Last = 0
private Data This = 0
private Data Prev = 0
endglobals
private function Execute takes nothing returns nothing
set This = First.next
call First.destroy()
set First = Data.create()
set First.next = 0
set Last = First
loop
exitwhen(This == 0)
call TriggerEvaluate(This.trig)
call TriggerClearConditions(This.trig)
call This.destroy()
set Prev = This
set This = Prev.next
endloop
endfunction
function ExecuteSoon takes boolexpr condition, integer data returns nothing
set Last.next = Data.create()
set Last = Last.next
set Last.next = 0
set Last.data = data
if Last.trig == null then
set Last.trig = CreateTrigger()
endif
call TriggerAddCondition(Last.trig, condition)
call ResumeTimer(Timer)
endfunction
function GetExecuteSoonData takes nothing returns integer
return This.data
endfunction
private function onInit takes nothing returns nothing
set First = Data.create()
set First.next = 0
set Last = First
call TimerStart(Timer, .0, false, function Execute)
endfunction
endlibrary
Red Flavor:
JASS:
//==============================================================================
// ExecuteSoon (Red flavor) -- by Builder Bob -- v1.1
//==============================================================================
//==============================================================================
// FUNCTION INDEX:
//==============================================================================
//
// -------------------------------------------------------------
//
// ExecuteSoon(condition, data)
//
// GetExecuteSoonData()
// returns integer
//
//-----------------------------------------------------------------------------
//==============================================================================
// DOCUMENTATION:
//==============================================================================
//
// WHAT IS THIS?
// * ExecuteSoon() will execute a function within 0 seconds, but not right away.
// This allows all the current fired events to finish before executing your function
//
// RED FLAVOR PROS:
// * Faster than blue flavor
//
// RED FLAVOR CONS:
// * GetExecuteSoonData MUST be called once and only once in
// every function you call via ExecuteSoon.
//
// * Consequtive ExecuteSoon calls within functions called via ExecuteSoon
// will trigger instantly, rather than after other queued functions finish.
//
// DETAILS:
// * The function passed as argument in ExecuteSoon must be made a condition
// like Condition(function whichFunction)
//
// * The condition used as argument in ExecuteSoon MUST return a boolean.
// true or false is irrelevant.
//
// * GetExecuteSoonData MUST be called once and only once in
// every function you call via ExecuteSoon.
//
// THANKS TO:
// * Jesus4Lyf for help on Red Flavor for maximum speed
// * Cohadar for good documentation standard
//
// HOW TO IMPORT:
// * create a trigger
// * convert it to text and replace the whole trigger text with this one
//
//==============================================================================
library ExecuteSoon initializer onInit
globals
private timer Timer = CreateTimer()
private trigger Trigger = CreateTrigger()
private integer array Data
private integer Index = 0
private integer TopIndex = 0
endglobals
private function Execute takes nothing returns nothing
set Index = -1
call TriggerEvaluate(Trigger)
call TriggerClearConditions(Trigger)
set TopIndex = 0
endfunction
function ExecuteSoon takes boolexpr condition, integer data returns nothing
call TriggerAddCondition(Trigger, condition)
set Data[TopIndex] = data
set TopIndex = TopIndex + 1
call ResumeTimer(Timer)
endfunction
function GetExecuteSoonData takes nothing returns integer
set Index = Index + 1
return Data[Index]
endfunction
private function onInit takes nothing returns nothing
call TimerStart(Timer, .0, false, function Execute)
endfunction
endlibrary
I'd consider this a snippet as it'll probably make more sense to just code it directly into other systems, than to call these functions. Use whatever works best for you.