Call a BlueprintImplementableEvent from c++ in editor?

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.

The custom event is correctly called when the game starts (from the c++ BeginPlay() event).

as always: a much better version of the question on gamedev.stackexchange

I don’t think thats possible because VM is not fully active in editor other then construction script. You might consider look up bluetilities for something more

Depending on what you do you might also try modifying something in editor UI, like in properties in you of actor, there options for that

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);
       }
}
2 Likes