Skip to content

Commit d318add

Browse files
[Docs] Event system info readme
1 parent 1fcee9a commit d318add

1 file changed

Lines changed: 53 additions & 3 deletions

File tree

Readme.md

Lines changed: 53 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,6 @@ Example of console logging (Renderer category, info & trace log level)
9191
Example of file logging (contains time stamp, category & log level)
9292
![Screenshot](docs/LoggerFileExample.png)
9393

94-
I do plan on supporting adding custom categories in the future (similar to Unreal Engines system).
95-
9694
### Debugging - Asserts
9795
- Assert only triggers in debug, the message is an optional parameter that will be logged to the console / file logger.
9896
- Check triggers in all builds, message is an optional parameter that will be logged to the console / file logger.
@@ -112,7 +110,59 @@ ME_VERIFY(CalculateAndValidatePath(), "Path must be valid");
112110
```
113111

114112
### Event System
115-
TODO
113+
The event system is simple to use. The only requirement is storing a Delegate and creating an event class.
114+
*Note: A MauCor::Delegate<> is implicitly the same type as MauCor::Delegate<void>*
115+
```cpp
116+
struct TestEvent
117+
{
118+
int i = 10;
119+
};
120+
121+
class Scene
122+
{
123+
MauCor::Delegate<TestEvent> m_DelegateTest{};
124+
MauCor::Delegate<> m_DelegateVoidTest{};
125+
};
126+
```
127+
128+
Broadcasting events and listening to events is also fairly simple, but it comes with different options, as you may want an immediate broadcast (which calls the corresponding function immediately when the event is broadcast). Or a delayed broadcast (which calls the corresponding function at the beginning of the next frame when the event is broadcast).
129+
130+
Similar to broadcasting, unsubscribes can be done immediately and delayed as well. The default here is to do it delayed, which prevents issues where you may unsubscribe, but there's still a lingering function call, resulting in nullptr or invalid ptr usage.
131+
132+
```cpp
133+
// Subscribe to an event
134+
m_DelegateTest += MauCor::Bind(&DemoScene::OnDelegate, this);
135+
136+
auto const& handle{ m_DelegateTest += MauCor::Bind<TestEvent>
137+
(
138+
[this](TestEvent const& event) { OnDelegate(event); }
139+
) };
140+
141+
m_DelegateTest.Get()->Subscribe([this](TestEvent const& event) { OnDelegate(event); }, this);
142+
143+
// Broadcast an event (the default is the immediate broadcast)
144+
m_DelegateTest.Get()->QueueBroadcast(event);
145+
m_DelegateTest.QueueBroadcast(event);
146+
m_DelegateTest << event;
147+
// Do an immediate broadcast:
148+
m_DelegateTest.Get()->Broadcast(event);
149+
m_DelegateTest.Broadcast(event);
150+
m_DelegateTest < event;
151+
152+
// Unsubscribe from an event
153+
m_DelegateTest /= this; // Immediate
154+
m_DelegateTest.UnSubscribeImmediate(handle02.owner);
155+
m_DelegateTest.UnSubscribeAllByOwnerImmediate(this);
156+
157+
m_DelegateTest -= handle; // Delayed
158+
m_DelegateTest.UnSubscribe(handle02.owner);
159+
160+
// The function called in this example
161+
void DemoScene::OnDelegate(TestEvent const& event)
162+
{
163+
ME_LOG_DEBUG(LogGame, "Event test: {}", event.i);
164+
}
165+
```
116166
117167
### Timer Manager
118168
TODO

0 commit comments

Comments
 (0)