Event Dispatchers
- So far we have two ways for blueprints to talk to each other.
- We can search for all instances of a class and call a method or event.
- But the documentation says this is expensive.
- We can have an instance recorded in a variable and call it.
- Event dispatchers give us a third option.
- With an event dispatcher we can
- Generate a special event
- Have other blueprints subscribe to that event
- This is still instance based, but now the client needs to know what server it is communicating with.
- Ie no searching for all and no long lists of clients.
- Example 1, a broadcaster
- In this example, an actor will "broadcast" when it is stepped on.
- Other actors, "receivers" will register to receive broadcasts from that actor..
- This is really a broadcast. The message is sent, those tuned in will hear it.
- Application building alarm.
- Create a blueprint actor, BP_Broadcast.
- Add something to be visible and a collision box.
- Add a EventSendMessage in the Event Dispatchers area.
- Drag this onto the panel and select call
-
- Connect this to on actor begin overlap.
-
- Now, when this actor is overlapped, everyone who subscribes will receive an message
- Place this in the scene.
- Build an blueprint actor BP_Receiver
- I added a sphere and a light
- I added a variable of type BP_Broadcaster, called MySender.
- This is who this blueprint will listen to.
- Drag this variable onto the event graph and select
Bind Event to Event Send Message
- Hook This up to the EventBeginPlay event.
-
- Add a custom event, GotMessage and hook this up to the bind node.
-
- I added code to switch the lights on and off.
-
- I added four of these to the scene and "tuned" them into the broadcaster by setting the variable to the broadcaster.
- You can do this the other was as well
- Create a central report station.
- Think of this as a hall of records or a centralized log.
- BP_Recorder
- Create a event Dispatcher EventGotAReport
- Drag this onto the event graph but this time select Assign
-
- Connect this to the Begin Play event.
-
- Build something to happen.
- I added a string so as input so we can see who called us.
-
- Compile and add it to the scene.
- Finally, build a reporter class BP_Reporter
- Make something to step on and detect collision
- I added a variable, MyReport of type BP_Recorder
- I added a string, "My Name".
- Expose both of these variables.
- Then in the event graph added the following
-