Unreal Engine 4--multi-cast Delegate and the realization of the observer pattern

Source: Internet
Author: User
Tags bind

This blog describes the use of Multi-cast delegate in UE4 and how to implement the Observer pattern using Multi-cast delgate. Delegate

The role of delegate is to bind an object to its function, resulting in a function that can be called globally. This functionality has been built into C #, but it does not seem to be an official implementation in C + + 11. For the implementation of delegate in C + + 11, you can refer to these two articles: implementation of Delegates in c++11,the impossibly Fast C + + Delegates UE4

UE4 is packaged for delegate and is now implemented using macros (probably due to historical baggage.) ), you can set up to 8 variables. Regarding the UE4 in the delegate, the corresponding document has been very clear, the portal. Observer Pattern

The Observer pattern is a very useful design pattern. As a short summary, the Observer pattern refers to an action by which all observers of the observer are triggered when the observed person changes. The introduction and implementation of specific design patterns can be referred to head first design Patterns and game programming Patterns.

It should be said that Multi-cast delegate is originally designed for the observer pattern, and can be designed in the following ways for the corresponding observer and the observer. observed (Subject)

You first need to declare a corresponding Multi-cast Delegate, which is used to manage the observer's events:

Declare_multicast_delegate_twoparams (onsomethingchanged, float previoushp, float currenthp);

It is important to note that the declaration of delegate in UE4 is implemented using a macro, so onsomethingchanged does not require quotes or text ("onsomethingchanged") operations.

The corresponding delegate variable can then be declared in the class, and the following example is a delegate for changing the life of the protagonist:

Class Amainplayerstate:public aplayerstate
{
    ...
    Onsomethingchanged onhpchanged;
}

In addition, a portal is needed to trigger actions when the health value changes:

    void Changehp ()
    {
        //do something important
        onhpchanged.broadcast (previoushp, CURRENTHP);
    }
Viewer (Observer)

The Observer observer can be anything-it can be a class, which can be a global context. We can bind it to a function of an object, or we can bind it to a global static function.

The following example is used to manipulate the blood bar UI when the player's health changes, first, in the Beginplay () method, register it as the observer of the player's blood Volume:

void Uplayerhpuserwidget::beginplay ()
{
    super::beginplay ();
    Hpdelegatehandle = _mainplayerstate->onhpchanged.adduobject (this, &uplayerhpuserwidget::receivehpchanged);
}

Here, receivehpchanged is a uplayerhpuserwidget function that accepts two float parameters, and the type is void.

In addition, when the UI is destroyed, the widget needs to be removed from the Observer list:

void Uplayerhpuserwidget::D estroyed ()
{
    mainplayerstate->onhpchanged.remove (hpdelegatehandle);
}

since the list of observers is based on the operation of the chain list, the time complexity of the remove () operation is O (n) o (n), and the sequence of triggering of the original delegates mentioned in the code note may change, which needs to be noted. PostScript

Design pattern Although this thing has been seen from the junior, but also to contact the commercial project can really begin to experience the design pattern of the essence. C + + is not like Java or C # in the language layer has built up a variety of design patterns, many have to implement their own. It's really learning.

< end of the full >

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.