• 🏆 Texturing Contest #33 is OPEN! Contestants must re-texture a SD unit model found in-game (Warcraft 3 Classic), recreating the unit into a peaceful NPC version. 🔗Click here to enter!
  • It's time for the first HD Modeling Contest of 2024. Join the theme discussion for Hive's HD Modeling Contest #6! Click here to post your idea!

[Snippet][Extension] Event

Read the header, like always :)

JASS:
// --------------------------------------------------------------------------------------------------------------------
//
//    Event
//    =============
//
//        Version:            1.0.0
//        Author:             Anachron
//
//        Requirements:
//            Stack [by Anachron]
//            (New) Table [by Bribe] (v. 3.1) [url]http://www.hiveworkshop.com/forums/jass-resources-412/snippet-new-table-188084/[/url]
//
//        Description:
//            This is a custom library to define and trigger your self made
//            events with dynamically changeable code pieces.
//
//        History:
//            1.0.0: Initial Release
//
//        API:
//        ------------------------------------------------------------------------------------------------
//            Methods
//        ------------------------------------------------------------------------------------------------
//        fire()                     --> Trigger the event and all attached codes, 
//                                       after that it will trigger all childs
//
//        attach(code x)             --> Add a code block to the current event
//        detach(code x)             --> Remove a code block from the current event
//        clear()                    --> Clear all code blocks from the event
//
//        adopt(Event x)             --> Add a child event to this event
//        orphane(Event x)           --> Remove child event from this event
//        abort()                    --> Clear all child events from this event
//
// --------------------------------------------------------------------------------------------------------------------------
library Event requires Table, Stack

	module IsEvent
		private Table BoolHandles = 0
		private Stack BoolIndex = 0
		private Stack ChildIndex = 0
		private trigger Invoker = null
		
		public static method create takes nothing returns thistype
			local thistype this = thistype.allocate()
			set .BoolHandles = Table.create()
			set .BoolIndex = Stack.create()
			set .ChildIndex = Stack.create()
			set .Invoker = CreateTrigger()
			return this
		endmethod
		
		public method adopt takes thistype theChild returns boolean
			if .ChildIndex.has(theChild) then
				return false
			endif
			
			return .ChildIndex.add(theChild)
		endmethod
		
		public method orphane takes thistype theChild returns boolean
			if not .ChildIndex.has(theChild) then
				return false
			endif
			
			return .ChildIndex.delete(theChild)
		endmethod
		
		public method abort takes nothing returns nothing
			call .ChildIndex.clear()
		endmethod
		
		private static method execute takes thistype this returns boolean
			local boolean did = TriggerEvaluate(.Invoker)
			
			call .ChildIndex.reset()
			loop
				exitwhen not .ChildIndex.hasNext()
				call thistype(.ChildIndex.getNext()).fire()
			endloop
			
			return did
		endmethod
		
		public stub method fire takes nothing returns nothing
			call thistype.execute(this)
		endmethod
	
		public method attach takes code theCode returns boolean
			local boolexpr boolExpr = Condition(theCode)
			local integer boolExprId = GetHandleId(boolExpr)
			if .BoolIndex.has(boolExprId) then
				return false
			endif
			set .BoolHandles.triggercondition[boolExprId] = TriggerAddCondition(.Invoker, boolExpr)
			call .BoolIndex.add(boolExprId)
			return true
		endmethod
		
		public method detach takes code theCode returns boolean
			local boolexpr boolExpr = Condition(theCode)
			local integer boolExprId = GetHandleId(boolExpr)
			if .BoolHandles.has(boolExprId) then
				return false
			endif
			call TriggerRemoveCondition(.Invoker, .BoolHandles.triggercondition[boolExprId])
			call .BoolHandles.remove(boolExprId)
			call .BoolIndex.delete(boolExprId)
			return true
		endmethod
		
		public method clear takes nothing returns nothing
			local integer boolExprId = 0
		
			loop
				exitwhen .BoolIndex.count == 0
				set boolExprId = .BoolIndex.get(.BoolIndex.count)
				call TriggerRemoveCondition(.Invoker, .BoolHandles.triggercondition[boolExprId])
				call .BoolHandles.remove(boolExprId)
				call .BoolIndex.delete(boolExprId)
			endloop
		endmethod
		
		private method onDestroy takes nothing returns nothing
			call .abort()
			call .clear()
			call .BoolHandles.destroy()
			call .BoolIndex.destroy()
			call .ChildIndex.destroy()
			call DestroyTrigger(.Invoker)
		endmethod
	endmodule
	
	struct Event
		implement IsEvent
	endstruct

endlibrary
 
Why another event system? Aren't those existing already enough? Why do people keep re-inventing the wheel?
I bet there is now at least one person out there who will use your event system in one of his spells/systems/snippets.
Which would at some point force me to import ANOTHER event library although I already got one (well, let's be honest; I never import spells but always write them on my own, but you see my point).
 
Level 31
Joined
Jul 10, 2007
Messages
6,306
Yea, the only reason for the current Event system was to have a common API so that you didn't have to write wrappers and so on and so that people could use your events more easily. It was also to easily add OO syntax to events.

The later PriorityEvent was done purely for events to run in a specific order regardless of when they were added and to keep the evaluation of those events uber fast : ).

Purge hit on #2, but he didn't hit #1.

Events are typically used for systems. There is a reason that dynamic events haven't been done, and that's because at that point you should just use triggers directly >.>.

Now removal of fast evaluating events could be done ;p, but as Purge pointed out, nobody has needed them, so it hasn't been included in Event =). At that point, people just use triggers and lists of instances.
 
Top