-
Notifications
You must be signed in to change notification settings - Fork 0
Events and Container
XDot Framework includes powerful and lightweight events system for your game. You can create and subscribe your objects at events, adds reactive fields etc.
Any event or reactive field has listeners and invoker.
Base event can be created like:
private readonly GameEvent myEvent = new GameEvent();Any events can provide handlers data:
public struct MyHandler
{
public string Message;
}
private readonly GameEvent<MyHandler> myEvent = new GameEvent<MyHandler>();To add or remove listener you can use GameEvent methods:
private readonly GameEvent<MyHandler> myEvent = new GameEvent<MyHandler>();
// Initialize Listeners
private void MyInitMethod()
{
myEvent?.AddListener(OnListenerInvoked);
myEvent?.Invoke(new MyHandler { Message = "My Message" });
myEvent?.RemoveListener(OnListenerInvoked);
}
// Called on Invoke
private void OnListenerInvoked(MyHandler handlerData)
{
Debug.Log(handlerData.Message);
}Game Field is an event-system extension. Unlike conventional events, GameField have a constant value of the Handler class. You can subscribe at Value changes and get it as reactive value.
But in general it works like regular events, only instead of Invoke, you update the Value of the Handler class, which can always be accessed, not just in the Invoke method.
For Example:
private readonly GameField<MyHandler> myField = new GameField<MyHandler>(new MyHandler { Message = "Default Message" });
// Initialize Listeners
private void MyInitMethod()
{
myField?.AddListener(OnListenerInvoked); // Here we subscribe our listener
myField?.Update(new MyHandler { Message = "My New Message" }); // Update our Value
Debug.Log((MyHandler)myField?.Value().Message); // Returns "My New Message"
myEvent?.RemoveListener(OnListenerInvoked);
}
// Called on Update
private void OnHandlerValueUpdated(MyHandler handlerData)
{
Debug.Log(handlerData.Message);
}The Event Container can be used to bind and inject events for any object of MVP triad. For example, if you need to create player position update event and get them in the camera - you can bind event in the container.
Simple usage of conainer:
// For example we have some method for bindings
private void EventContainerSample()
{
// Simple Events Binding
GameEvent<MyEventHandler> _myEvent = new GameEvent<MyEventHandler>();
EventContainer.Bind(_myEvent);
// Add Events Listener
_myEvent.AddListener(OnEventFired);
// Invoke Events from Container
// By direct ref or using container
_myEvent.Invoke(new MyEventHandler{
Message = "Direct Message"
});
// Invoke throw container
EventContainer.Get<MyEventHandler>().Invoke(new MyEventHandler{
Message = "Container Message"
});
// Simple Events Unbinding
EventContainer.Unbind(_myEvent);
}
// Here we grab event data
private void OnEventFired(MyEventHandler handlerData){
Debug.Log(handlerData);
}Event Handler Example:
// For all events we need some handlers
// for example Struct Handler
public struct MyEventHandler
{
// Here we provide event data
public string Message;
}This framework is under MIT license. Developed by Ilya Rastorguev specially for Pixel Incubator.
You can ask me about XDot Framework using Telegram @SodaBoom or by email: iliya-sdt@yandex.ru