Download

How to bind function between different class?

I have two modules, main and UI
the UI where it contains the widget blueprints is referencing to main module where is located the UI maneger so main module can’t reference UI to create Inventory by accessing its Widgetbluprint.
All of them called in BeginPlay overrided function.

So
From main module, UI_manager.h

DECLARE_DELEGATE(FDele_init_inventory_manager)
DECLARE_DELEGATE_OneParam(FDele_open_inventory_manager, bool)
FDele_init_inventory_manager  dele_init_inventory_manager;
FDele_open_inventory_manager  dele_open_inventory_manager;

And the cpp

void AUI_manager::Init_inventory()
{
    dele_init_inventory_manager.ExecuteIfBound();
}

void AUI_manager::Open_or_close_inventory(bool _is_opened)
{
    dele_open_inventory_manager.ExecuteIfBound(_is_opened);
}

And the Inventory.h

public:
UFUNCTION()
        void Init_inventory_manager();

    UFUNCTION()
        void Open_or_close_inventory(bool _is_opened);

cpp

void AInventory::BeginPlay()
{
    Super::BeginPlay();
    auto p_ui_manager = Get_UI_manager();
    p_ui_manager->dele_init_inventory_manager.BindUObject(this, &AInventory_manager::Init_inventory_manager);
    p_ui_manager->dele_open_inventory_manager.BindUObject(this, &AInventory_manager::Open_or_close_inventory);
}

Hey there

First thing, please take a look at and try to adhere to the Unreal coding standards Coding Standard | Unreal Engine Documentation

This makes it easier when you’re asking questions for other developers (like me) to understand your code and help you quicker.

As far as what you need to do for the code, you’ve got it all set up mostly correct, but you’re binding to this, which is an object of type AInventory. You need to get an AInventory_manager object, whose specified function can be bound to the delegate you are doing so with.

1 Like