• 🏆 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!

[Wurst] Event

Status
Not open for further replies.
Level 14
Joined
Jun 27, 2008
Messages
1,325
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:
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

  • Event.w3x
    21.2 KB · Views: 63
Last edited:
Status
Not open for further replies.
Top