[UE4] C + + Implementation Delegate Event instance (example, example, sample)

Source: Internet
Author: User

Transferred from: http://aigo.iteye.com/blog/2301010

Although the official Doc says that the event binding method is exactly the same as multi-cast usage, there are many examples on the Multi-cast forum, but the actual is different. And there is no such example on the forum. Later looked at the next Runtime/core source, the event of the binging manner as follows (attached complete steps):

1, first define the custom event, in this case we define in Gamemode, the name is called Myinitevent. In the example, the parameterless function is defined, and if a parameter is required, the official doc

Uclass ()classMyproj_api Amygamemode: PublicAgamemode {generated_body () Public: Ahgamemode (); //Defining an eventdeclare_event (Myuobject, myinitevent)//function to get event reference, easy to execute binding outside of Gamemodemyinitevent& OnInitialize () {returninitevent;} Private:      //instance of eventmyinitevent initevent; //binding pointer to eventFdelegatehandle Dhandle; }  

2, execute the binding. Myuobject::testfun is a function that needs to be triggered, which is assumed to be a normal parameterless member function:

myuobject* MyObj = NULL;    // This assumes the creation  of Myuobject if (MYOBJ)  {      = Mygamemode->oninitialize (). Adduobject (MYOBJ, &myuobject::testfun);  }  

Note: Any uobject normal function can be set to the callback function of the event, but if the function is ufunction, you need to replace Adduobject with addufunction.

3, triggering event, here is assumed to be triggered in Beginplay:

void Amygamemode::beginplay ()  {      initevent.broadcast ();  }  

4, remove the event, such as the previous bingding event does not want to trigger again:

Mygamemode->oninitialize (). Remove (Dhandle);  

How to implement delegate Event with blueprints:

The current example uses a version of v4.10, which includes the following:

When you press the letter key, go to invoke the pre-bound event Dispatcher, in this event Dispatcher, to modify the text of a button component in a widget blueprint.

1, first add an input action in project settings, named Testoutstr, triggered when the V key is pressed.

2, implement the operation calling event dispatcher in the blueprint script.

Here has built a Playercontroller blueprint, called: MYPLAYERCONTROLLER_BP. After opening this blueprint, create a new variable: newvar_0,string type, the default value is "BBBB", we will pass this "BBBB" string to the custom event defined later, so that the button text is displayed as "BBBB";

Create a new event Dispatcher, named: Updatebtntxt, we need to pass parameters, so for this event Dispatcher create a new string parameter named NewString.

3, the logic of this MYPLAYERCONTROLLER_BP blueprint is detailed as follows:

The Updatebtntxt event is triggered when the V key is pressed, and a "press v++++" is printed on the screen to test whether the call dispatch event has been executed.

4, add a button and a text component in the pre-built widget blueprint, the button ID is button_txt, and the following blueprint will use the default value of this Id,text component as AAAA, as shown in the structure:

5. Open the graph panel of the widget Blueprint and right-click to create a custom Event

6, this custom event name is Changetxtevent, because to accept the MYPLAYERCONTROLLER_BP blueprint passed in the string type parameter, so here is also a new string type parameter, named Newparam.

7, finally using the Blueprint node to connect the implementation logic, we bind in the event construct events pre-defined dispathcer:updatebtntxt.

When Updatebtntxt is called in the MYPLAYERCONTROLLER_BP Blueprint, the custom event:changetxtevent of the widget blueprint is triggered to modify the button's text.

Resources:

UE4 Event Dispatchers

Https://www.youtube.com/watch?v=k-MAALv3P-Y

Event Dispatchers

Https://docs-origin.unrealengine.com/latest/INT/Engine/Blueprints/UserGuide/EventDispatcher/index.html

[UE4] C + + implements Delegate event instance (example, example, sample)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.