Search code examples
c++unreal-engine4

Unreal Engine 4 CPP: 'AsShared': is not a member of 'MyModule'


I'm newbee in C++ and trying to migrate from Unity3D to Unreal Engine 4. Right now i'm trying to write my first plugin (UI plugins and tools is very important part of my job) and stuck on add OnClicked event to SButton I'd started from create Standalone Window plugin and add one button.

TSharedRef<SDockTab> FAssistLibModule::OnSpawnPluginTab(const FSpawnTabArgs& SpawnTabArgs)  {
FText WidgetText = FText::Format(
    LOCTEXT("WindowWidgetText", "Add code 111 222 to {0} in {1} to override this window's contents"),
    FText::FromString(TEXT("FAssistLibModule::OnSpawnPluginTab")),
    FText::FromString(TEXT("AssistLib.cpp"))
    );

FText ButtonLabel = FText::FromString("Button?");

return SNew(SDockTab)
    .TabRole(ETabRole::NomadTab)
    [
        // Put your tab content here!
        SNew(SVerticalBox)
        +SVerticalBox::Slot().VAlign(VAlign_Top)
        [
            SNew(STextBlock)
            .Text(WidgetText)
        ]
        +SVerticalBox::Slot().VAlign(VAlign_Top)
        [
            SNew(SButton)
            .Text(ButtonLabel)
            .OnClicked(this, &FAssistLibModule::CollectBlueprintsButtonClicked)
        ]

    ];}

And after compile UE give me this error

Info Compiling game modules for hot reload
Info Performing 2 actions (4 in parallel)
Info Module.MyModule.cpp
Info c:\program files (x86)\epic games\4.13\engine\source\runtime\core\public\delegates\DelegateSignatureImpl.inl(181): error C2039: 'AsShared': is not a member of 'FMyModuleModule'
Error D:\dev\repos\assist-lib-ue4\Plugins\MyModule\Source\MyModule\Public\MyModule.h(10) : note: see declaration of 'FMyModuleModule'
Info C:\Program Files (x86)\Epic Games\4.13\Engine\Source\Runtime\Slate\Public\Widgets\Input\SButton.h(54): note: see reference to function template instantiation 'TBaseDelegate<FReply> TBaseDelegate<FReply>::CreateSP<UserClass,>(UserClass *,FReply (__cdecl FMyModuleModule::* )(void))' being compiled
Info         with
Info         [
Info             UserClass=FMyModuleModule
Info         ]
Info C:\Program Files (x86)\Epic Games\4.13\Engine\Source\Runtime\Slate\Public\Widgets\Input\SButton.h(54): note: see reference to function template instantiation 'TBaseDelegate<FReply> TBaseDelegate<FReply>::CreateSP<UserClass,>(UserClass *,FReply (__cdecl FMyModuleModule::* )(void))' being compiled
Info         with
Info         [
Info             UserClass=FMyModuleModule
Info         ]
Error D:\dev\repos\assist-lib-ue4\Plugins\MyModule\Source\MyModule\Private\MyModule.cpp(89) : note: see reference to function template instantiation 'SButton::FArguments::WidgetArgsType &SButton::FArguments::OnClicked<FMyModuleModule>(UserClass *,FReply (__cdecl FMyModuleModule::* )(void))' being compiled
Info         with
Info         [
Info             UserClass=FMyModuleModule
Info         ]
Info c:\program files (x86)\epic games\4.13\engine\source\runtime\core\public\delegates\DelegateSignatureImpl.inl(181): error C2672: 'StaticCastSharedRef': no matching overloaded function found
Info c:\program files (x86)\epic games\4.13\engine\source\runtime\core\public\delegates\DelegateSignatureImpl.inl(181): error C2672: 'TBaseDelegate<FReply>::CreateSP': no matching overloaded function found
Info ERROR: UBT ERROR: Failed to produce item: D:\dev\repos\assist-lib-ue4\Plugins\MyModule\Binaries\Win64\UE4Editor-MyModule-6676.dll
Info Total build time: 46.77 seconds

Please help, i dont understant whats going on and thanks for any suggestions!


Solution

  • The FAssistLibModule looks like deriving from IModuleInterface and is not a shared pointer, so you have to specify the method to bind for OnClicked.

    I think it is correct to replace

    OnClicked(this, &FAssistLibModule::CollectBlueprintsButtonClicked)

    with

    OnClicked(FOnClicked::CreateRaw(this, &FAssistLibModule::CollectBlueprintsButtonClicked))

    or

    OnClicked_Raw(this, &FAssistLibModule::CollectBlueprintsButtonClicked)

    Check this thread, I think it's useful: https://forums.unrealengine.com/showthread.php?78289-Slate-button-causes-the-editor-to-hang-when-instantiated&p=343287&viewfull=1#post343287