Using c++ actors from a plugin

Hello!

I’m working on a project with some friends and we want to create a tool to help our game designers to tweak all game feel (and 3C) related variables. We want the tool to be used in editor mode but also during the play mode (PIE). The changes made during the play mode must be applied on the object even if we stop the play mode.

**The current work : **

  • I created an empty c++ project, called ActorLinking, to make things easier. I created a dummy actor, called TheActor. This actor is a part of the project.
  • Then I created a small plugin, called ThePlugin, to add a button in the editor.
  • When I click on this button, I retrieve the dummy actor in the scene from the editor world or the engine world.

**The problem : **

  • I get a linker error when I try to use any functions of this actor (See image)

**The project file : (.uproject) **

{
	"FileVersion": 3,
	"EngineAssociation": "4.21",
	"Category": "",
	"Description": "",
  "Modules": [
    {
      "Name": "ActorLinking",
      "Type": "Runtime",
      "LoadingPhase": "Default",
      "AdditionalDependencies": [
        "Engine"
      ]
    }
  ]
}

**The actor header : **

#pragma once

#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "TheActor.generated.h"

UCLASS()
class ACTORLINKING_API ATheActor : public AActor
{
	GENERATED_BODY()
	
public:	
	ATheActor();

	UFUNCTION()
	void MoveActor();

protected:
	virtual void BeginPlay() override;

public:	
	virtual void Tick(float DeltaTime) override;
};

The actor implementation : (TheActor.cpp)

#include "TheActor.h"

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

void ATheActor::BeginPlay() {
	Super::BeginPlay();
}

void ATheActor::MoveActor() {
	UE_LOG(LogTemp, Warning, TEXT("MoveActor"));
}

void ATheActor::Tick(float DeltaTime) {
	Super::Tick(DeltaTime);
}

**And now the code of the plugin : (.uplugin) **

{
 [...]
 "Modules": [
  {
   "Name": "ThePlugin",
   "Type": "Editor",
   "LoadingPhase": "Default",
  }
 ]
}

The build file (ThePlugin.Build.cs) :

using UnrealBuildTool;

public class ThePlugin : ModuleRules
{
	public ThePlugin(ReadOnlyTargetRules Target) : base(Target)
	{
		PCHUsage = ModuleRules.PCHUsageMode.UseExplicitOrSharedPCHs;
		
		PublicIncludePaths.AddRange (new string[] {});
		PrivateIncludePaths.AddRange(new string[] {});
			
		PublicDependencyModuleNames.AddRange (new string[] {"Core", "ActorLinking" });
		PrivateDependencyModuleNames.AddRange(
			new string[]
			{
				"Projects",
				"InputCore",
				"UnrealEd",
				"LevelEditor",
				"CoreUObject",
				"Engine",
				"Slate",
				"SlateCore",
                "ActorLinking" // The module of the project
            }
			);
		
		DynamicallyLoadedModuleNames.AddRange(new string[]{});
	}
}

And finally the code causing the linker error : (ThePlugin.cpp)

void FThePluginModule::PluginButtonClicked()
{
    [...]
	ATheActor* p_actor = nullptr;

    /* Getting the actor */

	p_actor ->MoveActor(); // MoveActor undefined reference

	[...]
}

I’m new to Unreal Engine so it makes me think I’m doing it wrong. I’m trying to figure out why it doesn’t work. It seems to work with propertyies but not with functions. I’m unable to find appropriate ressources that’s why I’m posting here. Thank you for your help!

Hey,

Thank you for your time! I filled the public and private include path with the right path but there is still the linker error

Hello,

you also need to fill the “PublicIncludePaths” ( and “PrivateIncludePaths” if required) in the “ThePlugin.Build.cs” file

exemple :

PublicIncludePaths.AddRange( new string[] { "ActorLinking/Public" });

be sure tu put the correct path depending of your actor header file location

edit: with additional comment of the final answer :

You need to have it in the following syntax :

#include "ModuleName/Path/TheActor.h"

so in your exemple that could be

#include "ActorLinking/Public/TheActor.h"

do you also have the correct include in “ThePlugin.cpp” ?

You need to have it in the folowing syntax :

#include "ModuleName/Path/TheActor.h"

so in your exemple that could be

#include "ActorLinking/Public/TheActor.h"

For now, I have #include “TheActor.h”

Hi, I tried with “ActorLinking/Public/TheActor.h” but it doesn’t work :confused: I will check all from the beginning

So everything was okay with the right include paths.

And… after having refreshed the visual studio solution, cleaned up the solution, and rebuilt everything, no more linker error :slight_smile: Thank you for your help!