Legacy/Plugins, How To Use CPP Code Of Plugin In Your Project

From UE4: Community Wiki
Jump to: navigation, search

Overview

Author: Rama (talk)

Dear Community,

When you have a UE4 code plugin whose code you want to utilize in your main UE4 project, here are the steps you need to take to be able to directly refer to and subclass C++ Code from the plugin!

Step 1: Plugin must export the proper classes

The first step is required of the plugin writer, you must properly export the classes you want other people to be able to use, here is example from my RamaSaveSystem Plugin:

#pragma once

#include "RamaSaveComponent.h"
#include "RamaSaveEngine.h"

#include "RamaSaveLibrary.generated.h"

UCLASS()
class RAMASAVESYSTEM_API URamaSaveLibrary : public UBlueprintFunctionLibrary
{
	GENERATED_BODY()
public:
	
	static ARamaSaveEngine* GetOrCreateRamaEngine(UWorld* World);

The part I am trying to emphasize is this:

 class RAMASAVESYSTEM_API URamaSaveLibrary

By using the macro RAMASAVESYSTEM_API, UE4 generates all the code for properly exporting this class as a class that other projects can "see into", call functions from, and even inherit from as a subclass! (do a search for "__declspec(dllexport)" for more info).

UE4 generates the macro automatically, so for your plugin it would be YOURPLUGIN_API and you dont have to declare that anywhere. (Correction: A Plugin is a collection of Modules. The Auto-Generated export is for YOURMODULE_API. Which is declared within your plugin.)

Because plugins are .dlls, you can export functionality from that dll for others to call into via the macro that UE4 generates for you automatically.

In the same way you can inherit from AActor in a precompiled UE4 engine version (without compiling a custom engine), so you can enable people to inherit from your plugin classes by properly exporting them as shown above.

Step 2: Build.cs

The user of the plugin's code has to include the plugin in their build cs, so your build cs will end up looking something like this:

PublicDependencyModuleNames.AddRange(new string[] { 
	"Core", 
	"CoreUObject", 
	"Engine", 
	"InputCore",
	
	"UMG", "Slate", "SlateCore", 
	 
	"AIModule",
	
	"PhysX", "APEX",

	//Rama Plugins  
	"RamaSaveSystem"   //<~~~~~~
});

Step 3: #include like you do with UE4 engine classes

Now that you've added the plugin to your build cs, you can #include whichever properly exported class files you want!

 #include "RamaSaveLibrary.h"

Now you can inherit from these included plugin classes, or extend them to create your own custom subclasses of base plugin classes without having to modify the source code at all, which is very handy for when the plugin author updates their source code!

Conclusion

With these 3 basic steps you can easily add code plugins to your UE4 project and modify or extend their functionality, or simply access them in C++ as opposed to only having access in blueprints!

Enjoy!

Rama (talk)