0
\$\begingroup\$

I created a custom actor:

BPTest.h:

#include "GameFramework/Actor.h"
#include "BPTest.generated.h"

UCLASS(Blueprintable, BlueprintType)
class ABPTest : public AActor
{
    GENERATED_BODY()

public: 
    ABPTest();

    virtual void BeginPlay() override;

    UFUNCTION(BlueprintImplementableEvent, meta = (DisplayName = "Custom event"))
    void CustomEvent(float Value);
    void CustomEvent_Implementation(float Value);
};

BPTest.cpp:

#include "ProBuilderPrivatePCH.h"
#include "BPTest.h"

ABPTest::ABPTest()
{
    PrimaryActorTick.bCanEverTick = true;
}

void ABPTest::BeginPlay()
{
    Super::BeginPlay();
    CustomEvent(0.f);
}

void ABPTest::CustomEvent_Implementation(float Value)
{
    UE_LOG(LogTemp, Warning, TEXT("CustomEvent_Implementation: %f"), Value);
}

I then created a blueprint from this actor class.

I added an instance of this blueprint actor in my level.

I would like to call this custom event from c++ in the editor, not when the game is started.

I tried this in a custom editor mode (extending FEdMode):

bool FProBuilderEdMode::InputKey(FEditorViewportClient *ViewportClient, FViewport *Viewport, FKey Key, EInputEvent Event)
{
    for (TActorIterator<ABPTest> ActorIterator(GWorld); ActorIterator; ++ActorIterator)
    {
        ActorIterator->CustomEvent(1.f);
    }
    return FEdMode::InputKey(ViewportClient, Viewport, Key, Event);
}

but it does not work. I assume the game has to be started (hit play) in order for the blueprint event to be taken into account. Is my assumption correct?

How can I call a blueprint function from c++ when the game is not started (Is it even possible)? I have seen UObject::CallFunctionByNameWithArguments but it seems dirty.

\$\endgroup\$
1
  • \$\begingroup\$ The custom event is correctly called when the game starts (from the c++ BeginPlay() event). \$\endgroup\$
    – arthur.sw
    Apr 26, 2016 at 16:12

2 Answers 2

2
\$\begingroup\$

Nako Sung answer:

Without game, UE4 prevents script functions to be executed accidently. (I don't know the clear reason)

In C++ you can use FEditorScriptExecutionGuard:

FEditorScriptExecutionGuard ScriptGuard;
{
       // your code to execute scripts in editor while the game is not started goes here, for example:
       for (TActorIterator<ABPTest> ActorIterator(GWorld); ActorIterator; ++ActorIterator)
       {
            ActorIterator->CustomEvent(1.f);
       }
}

answered Apr 27 '16 at 8:57

arthur.sw

Hey all,

I know this post is pretty old. While reading through arthur.sw's answer and some research, figured out some Engine code that uses FEditorScriptExecutionGuard.

EditorTutorial.cpp
void UEditorTutorial::HandleTutorialClosed()
{
    FEditorScriptExecutionGuard ScriptGuard;
    OnTutorialClosed();
}

So in short, as long as the ScriptGuard is in scope, you can just call the BlueprintImplementableEvent / NativeEvent function.

Hope this helps someone.

\$\endgroup\$
1
\$\begingroup\$

Nako Sung answer:

Without game, UE4 prevents script functions to be executed accidently. (I don't know the clear reason)

In C++ you can use FEditorScriptExecutionGuard:

FEditorScriptExecutionGuard ScriptGuard;
{
       // your code to execute scripts in editor while the game is not started goes here, for example:
       for (TActorIterator<ABPTest> ActorIterator(GWorld); ActorIterator; ++ActorIterator)
       {
            ActorIterator->CustomEvent(1.f);
       }
}
\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .