Sponsored By

UE4Cookery CPP004: Editor actions with CallInEditor

Topic: Editor actions with CallInEditor

Source for UE4.23: https://github.com/klauth86/UE4Cookery/tree/main/CPP004

Artur Kh, Blogger

March 8, 2021

2 Min Read
Game Developer logo in a gray background | Game Developer

Thanx to Alecann01 on https://answers.unrealengine.com/questions/1000550/dynamically-spawning-actors-in-the-editor.html

It is rather usual task to check something on level or location. For example, you can test some sort of spawning classes or different explosion effects to see if their logic is correct and conformed with some requirements. It is great to check this while running game in Editor, but there is an opportunity for some of this tests to be called in Editor without any need of running game by Play button. That can be very valuable for simple and quick verifications and is connected with UFUNCTION specifier CallInEditor. Another application for this specifier  - auto generating of level content by sequence of spawn and arrange actions. Let's examine it.

We can create new project from template and add new Actor class

 

MyActor.h


// Fill out your copyright notice in the Description page of Project Settings.

#pragma once

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

UCLASS()
class CPP004_API AMyActor : public AActor {
	
	GENERATED_BODY()

protected:

	UPROPERTY(Category = "MyActor: Test1", EditAnywhere)
	AActor* Test1Actor;

	UFUNCTION(Category = "MyActor: Test1", BlueprintCallable, CallInEditor)
	void Test1();
};

 

MyActor.cpp


// Fill out your copyright notice in the Description page of Project Settings.


#include "MyActor.h"
#include "Engine/World.h"

void AMyActor::Test1() {
	if (!Test1Actor) return;

	int i = 10;
	while (i-- > 0) {
		auto offset = 400 * (FVector2D::UnitVector + FMath::RandPointInCircle(1));
		auto location = FVector(offset, 0) + GetActorLocation();
		GetWorld()->SpawnActor(Test1Actor->GetClass(), &location, &FRotator::ZeroRotator);
	}
}

 

The main role as mentioned above is played by CallInEditor specifier for Test1 function. To see what's going on let's start Editor and add some instance of UMyActor to new level. When we open Details panel for created instance, we'll see there Editor action called Test1. By clicking on it we can execute CPP code in this function body right in Editor:

 

CallInEditor in action

Read more about:

Blogs

About the Author

Daily news, dev blogs, and stories from Game Developer straight to your inbox

You May Also Like