Hi Guys,
I’m a beginner in Unreal engine. I use Blueprints only for scripting as of now.
I’m good at unity3D and I been using event-based design pattern in my projects for a while. And I use Scriptable objects for making for this system. It is something like event assets. You can take a reference to this event asset inside an invoker and inside a listener. And the result is no tight coupling. While I started using UE, I thought this can be made happen in UE also. But I couldn’t find any result from internet/forums. So, I decided to make of my own.
I’m not sure if people are using this method, still I’m sharing the system here.
Unity Scriptable Objects = UE data assets. Mainly used as data container assets but you can do more with them…
1.Let’s make a project first
2.Now make a blueprint with PrimaryDataAsset as parent class
I’m naming it as SimpleEvent_BP. Open the Blueprint editor and declare an eventDispatcher. “SimpleEventDispatcher”
Done!! Our SimpleEvent Blueprint is ready
3.Now we can make Data Assets from SimpleEvent_BP
4.Now we can make an invoker ActorComponent to invoke this Event assets.
5.Edit the SimpleEventInvoker and make a variable to refer to the SimpleEvent Data Asset and a function InvokeEvent to invoke the SimpleEvent…
So our invoker component is ready
Now we can make a listener ActorComponent
Just as in SimpleEventInvoker, we can add a reference variable to refer simpleEvent. Also a dispatcher OnsimpleEventInvoked.
Now the event subscribing part…
Create a custom event SubscriberEvent to bind to the simpleEventDispatcher on beginplay and unbind on endplay…
This subscriber event will act as an intermediate that will call OnsimpleEventInvoked dispatcher when invoked…
As a result all the external events binded to OnsimpleEventInvoked will get invoked…
So our invoker is also ready. Now we can test it…
Make two actor Blueprints ExampleInvoker_BP and ExampleListener_BP
On ExampleInvoker_BP add actor component SimpleEventInvoker and add SimpleEventListener on ExampleListener_BP. On both components, select ExampleEvent in the exposed field simpleEvent.
Now in the ExampleInvoker_BP, add a key pressevent and callfunction invokeEvent
And in the ExampleListener_BP, bind a custom event to the dispatcher of listener…
Now test run it…Boom….