Description
The Event System consists of two main components: an event provider and an event receiver. Events flow in one direction — from the provider to the receiver.
To ensure proper usage and maintainable design, only specific classes are allowed to act as providers or receivers. These restrictions offer better control over the system and help prevent misuse. Compatibility and connections are validated during compile-time.
Event Provider
- Must inherit from the EventProvider base class.
- To maintain clarity and simplicity, only selected native classes are allowed to inherit from EventProvider. This may vary between projects. For scripting scenarios, the class is sealed to disallow custom script-based providers. Valid providers typically include:
- Contains methods marked with [EventAttribute], representing the events it can broadcast.
API
- ConnectEvent
Establishes a connection between an event on the provider and a callback method on the receiver.
- The provider may restrict which receiver types are accepted.
- Receiver callbacks must have a compatible signature with the event.
- DisconnectEvent
Removes a specific event connection between a provider and a receiver.
- Alternatively, can be used to remove all connections between a specific provider and receiver.
- ThrowEvent
Invokes all registered callbacks for an event.
- Callbacks are executed in the order they were connected.
- Arguments must match the event declaration.
Example
{
[EventAttribute()]
void SomeEventA(TestWorldSystem sender,
int param1,
string param2);
[EventAttribute()]
{
}
}
Event Receiver
- Technically, any managed object may act as a receiver.
However, each provider can define which receiver types are permitted.
- Receiver methods must be marked with [ReceiverAttribute].
These methods can be connected to or disconnected from provider events.
Example
{
}
{
[ReceiverAttribute()]
void OnSomeEventA1(TestWorldSystem sender,
int param1,
string param2)
{
}
[ReceiverAttribute()]
void OnSomeEventA2(TestWorldSystem sender,
int param1,
string param2)
{
}
[ReceiverAttribute()]
{
}
void Init(TestWorldSystem system)
{
TestWorldSystem.ConnectEvent(system.
SomeEventA,
this.OnSomeEventA2);
TestWorldSystem.ConnectEvent(system.
SomeEventB,
this.OnSomeEventB);
}
{
EventProvider.DisconnectEvents(
m_system,
this);
}
}