- Joined
- Nov 7, 2014
- Messages
- 571
While reading this thread I was thinking if there could be a "better" (for some definition of better) way
of calling code when some events happen. And I came up with this:
which dispatches events from this source:
The only downside that I can see is that it could reach the op-limit if the event source emits many events in a single batch.
Otherwise this should be much faster than
Edit: okay there's another major downside, i.e if we try to call a method jasshelper will generated "the bonus" stuff which behind the scenes uses
of calling code when some events happen. And I came up with this:
JASS:
library EventSwitch
function EventSwitch takes integer ev, integer data returns nothing
local RedEscKeyPressCount r
// if we had a lot of type of events I guess one has to come up with creative names
// (a .. z, can only get you so far, but aa .. zz can get you much further)
if ev == EVENT_RED_ESC_KEY_PRESS_COUNT_MULTIPLE_OF_3 then
set r = RedEscKeyPressCount(data)
call BJDebugMsg("multiple of 3: data: " + I2S(r.count))
elseif ev == EVENT_RED_ESC_KEY_PRESS_COUNT_MULTIPLE_OF_5 then
set r = data // don't really need the typecast
call BJDebugMsg("multiple of 5: data: " + I2S(r.count))
endif
// an if statement can have ~127 branches
// so if we had more events than that we would simply use 2 if statements =)
endfunction
endlibrary
which dispatches events from this source:
JASS:
// As ridiculous of an event as events can be...
library RedEscKeyPressCount initializer init requires EventSwitch
globals
integer red_esc_key_press_count = 0
integer EVENT_RED_ESC_KEY_PRESS_COUNT_MULTIPLE_OF_3 = StringHash("EVENT_RED_ESC_KEY_PRESS_COUNT_MULTIPLE_OF_3")
integer EVENT_RED_ESC_KEY_PRESS_COUNT_MULTIPLE_OF_5 = StringHash("EVENT_RED_ESC_KEY_PRESS_COUNT_MULTIPLE_OF_5")
endglobals
struct RedEscKeyPressCount
integer count = 0
endstruct
globals
private RedEscKeyPressCount instance
endglobals
private function on_esc takes nothing returns nothing
set instance.count = instance.count + 1
if ModuloInteger(instance.count, 3) == 0 then
call EventSwitch(EVENT_RED_ESC_KEY_PRESS_COUNT_MULTIPLE_OF_3, instance)
endif
if ModuloInteger(instance.count, 5) == 0 then
call EventSwitch(EVENT_RED_ESC_KEY_PRESS_COUNT_MULTIPLE_OF_5, instance)
endif
endfunction
private function init takes nothing returns nothing
local trigger t
set instance = RedEscKeyPressCount.create()
set t = CreateTrigger()
call TriggerRegisterPlayerEventEndCinematic(t, Player(0))
call TriggerAddAction(t, function on_esc)
endfunction
endlibrary
The only downside that I can see is that it could reach the op-limit if the event source emits many events in a single batch.
Otherwise this should be much faster than
TriggerEvaluate/Execute
, ExecuteFunc
, and ForForce
.Edit: okay there's another major downside, i.e if we try to call a method jasshelper will generated "the bonus" stuff which behind the scenes uses
TriggerEvaluate/Execute
...
Last edited: