Design for a very simple and lightweight Event system for the Wurst StandardLib.
Note that there is already the ClosureEvents package for flexible dynamic events. This package aims to provide an alternative for high performance non-dynamic events.
Currently Wurst doesnt allow the usage of TriggerRegisterVariableEvent (just like ExecuteFunc) but if that changes on day this library will be changed to make use of VariableEvents. The API will stay the same.
System:
I think the approach is pretty straight forward and basically uses global variables to pass event data. However instead globals variables static class members are used to add some safety by providing a namespace.
Example:
GitHub:
https://github.com/muzzel/APT/tree/master/wurst/lib/Event
Note that there is already the ClosureEvents package for flexible dynamic events. This package aims to provide an alternative for high performance non-dynamic events.
Currently Wurst doesnt allow the usage of TriggerRegisterVariableEvent (just like ExecuteFunc) but if that changes on day this library will be changed to make use of VariableEvents. The API will stay the same.
System:
Wurst:
package Event
public module EventModule
static trigger trig = CreateTrigger()
static function fire()
trig.evaluate()
static function addAction(code c)
trig.addCondition(Filter(c))
I think the approach is pretty straight forward and basically uses global variables to pass event data. However instead globals variables static class members are used to add some safety by providing a namespace.
Example:
Wurst:
package EatFishEvent
import Event
import ClosureTimers
public class EatFishEvent
use EventModule
static string penguin
static int amountOfFish
function getRandomPenguin() returns string
var peng = "|cff00ff00"
switch GetRandomInt(0, 3)
case 0
peng += "Crigges"
case 1
peng += "menag"
case 2
peng += "muzzel"
default
peng += "WaterKnight"
peng += "|r"
return peng
init
// Make a random penguin eat fish every 2 seconds:
doPeriodically(2., (CallbackPeriodic _cb) -> begin
EatFishEvent.penguin = getRandomPenguin()
EatFishEvent.amountOfFish = GetRandomInt(1, 8)
EatFishEvent.fire()
end)
// Print a message when a penguin eats some fish:
EatFishEvent.addAction(() -> print("\n" + EatFishEvent.penguin + " ate " + EatFishEvent.amountOfFish.toString() + " fishes!"))
// Print a comment about the penguins eating habits:
EatFishEvent.addAction(() -> begin
if EatFishEvent.amountOfFish <= 3
print(EatFishEvent.penguin + " should eat more fish.")
else if EatFishEvent.amountOfFish <= 5
print("Enjoy your fish, " + EatFishEvent.penguin)
else
print("Stahp, " + EatFishEvent.penguin + " fat penguins can't fly :(")
end)
GitHub:
https://github.com/muzzel/APT/tree/master/wurst/lib/Event
Attachments
Last edited: