Using a plugin in C++ code

Hi,

I want to use a plugin which is available in the engine source tree. Exactly that is:
Engine/Plugins/Runtime/CustomMeshComponent
How can I do that?

More details:

In a fresh project the UCustomMeshComponent class is not visible for the compiler. Do I have to include a specific file?
I tried with some combinations like “runtime/CustomMeshComponent.h” but it doesn’t work.

I also copy-pasted the CustomMeshComponent folder into my project’s plugins folder, however during compilation got errors that such a plugin is already registered, so it should be accessible to me straight away I guess.

1 Like

Ok, solved this:

In MyProject.build.cs I added the following lines:

PrivateDependencyModuleNames.AddRange(new string[] { "CustomMeshComponent" });
PrivateIncludePathModuleNames.AddRange(new string[] { "CustomMeshComponent" });

And then in the file which wanted to use this custom mesh component it was enough to include it like that:

#include "CustomMeshComponent.h"
6 Likes

Thank you for sharing your solution!

#:heart:

Rama

fszczemton,

Your solution works, but keep in mind that the plug-in is now no longer really a plug-in: it became a hard dependency that is statically linked to your project, which means that your project is coupled to the plug-in and won’t work if the plug-in is missing.

We generally discourage the adding of hard dependencies to plug-ins, because for plug-ins to be truly pluggable, the application consuming those plug-ins shouldn’t have to know about what those plug-ins are. If the app needs to know which plug-ins exist, then what’s the point of having plug-ins? :slight_smile:

That being said, if you want to go this path anyway, you may want to consider making it a somewhat less tight coupling by using DynamicallyLoadedModuleNames in combination with PrivateIncludePathModuleNames instead. This will then allow you to do a LoadModule() in your code and fail gracefully in case the plug-in is missing.

If you do not need access to any of the implementation details of the plug-in, but you simply want to load the plug-in as part of loading your game or your game’s Editor, then it is sufficient to add the plug-in as a dependency to your game’s Engine.ini mark the plug-in as enabled in the .uplugin file.

Thanks for the explanation. I wasn’t aware of the other possibilities and it’s really helpful of you to explain it more precisely - that will definitely be useful in the future :slight_smile: E.g. having that mesh plugin as a hard dependency rules-out HTML5 and that should be handled well.

I think in my case having a hard dependency would actually make sense, because I wanted to use the plugin to generate terrain geometry, which is crucial, however to have more control I’ll eventually implement my own code based on the CustomMeshComponent as example.

I am also wondering - is it possible for the plugin to be missing? I’m the person who has the local installation of engine and would package the game, so it’s hard to imagine such a situation for me.

Once you statically link to the plugin’s module you have a hard dependency and the module must be present, or otherwise the game will fail to load. You could also create a so called monolithic binary, in which case all dependencies would be compiled and linked into a single huge executable. There would be no DLLs in that case (except for third party dependencies maybe).

That being said, I’m not 100% sure to what extent we already support monolithic linking of plugin modules. I remember that a couple months ago there were still problems with that approach. You may want to start a separate AnswerHub thread if you’re interested in more details.

Another thing to be aware of when hard linking to plugin modules is that you effectively stop using the module as a plugin - it becomes a library instead. This may have subtle undesired side effects. For example, what happens if the plug-in gets updated and its API changes? You would have to recompile your own project, or else it will crash on start or have other undefined behavior.

Thanks a lot for all the details, really interesting stuff. I definitely don’t want to make anything fancy with the build system. And yes, somehow I forgot that a plugin, being a plugin, can change its API without notice and cause problems.
I’ll have to roll-out my own custom mesh code which will be quite educating I guess.

"then it is sufficient to add the plug-in as a dependency to your game’s Engine.ini "

I did this and it was still not enough.

[Plugins]
EnabledPlugins=CustomMeshComponent

Was this not enough? Did I not put this code in the right location?

You are right, this has recently changed. Whether plug-ins are enabled or disabled is now saved in the .uplugin file itself, no longer in the project’s Engine.ini file.

In the .uplugin file, add the following setting:

"EnabledByDefault" : true

Take a look at existing plug-ins for other settings that may have changed.

Ok, I’ve noticed that “EnabldByDefault” : True is already written in the source.

So, I am out of ideas as to why I am having this problem.

I cant use #include CustomMeshComponent.h … the editor cant see it.

“the editor can’t see it” - what exactly do you mean by that?

Where in the Editor are you trying to use the new class? If it’s in a blueprint, did you mark the class as BlueprintSpawnable?

meta=(BlueprintSpawnableComponent)

Sorry.

I am trying to use the plugin to create a custom mesh in c++. However, when I write

 #include CustomMeshComponent.h

I get a red squiggly line and an error stating that the compiler cannot find the file.

You need to add CustomMeshComponent to your private or public dependencies in your module’s build.cs file as the first answer indicates.

Ok, I thought that creates a hard dependency, which isnt desired.

Another thing to be aware of when hard linking to plugin modules is that you effectively stop using the module as a plugin - it becomes a library instead. This may have subtle undesired side effects. For example, what happens if the plug-in gets updated and its API changes? You would have to recompile your own project, or else it will crash on start or have other undefined behavior.

Are you saying there isnt a way to implement this in C++ without forcing a hard dependency?

If you want this to be a plug-in, then you should neither #include nor link that module.

It sounds to me like you’re trying to make this a regular library module, not a plug-in. If you need access to this module from another module, then it should not be a plug-in.

is there an example on how to use the module in code as a plug in?

I guess I dont understand how one would use this code- other than from another module. I just want to use it how it was intended to be used.

You do not “use” a plug-in in your code. A plug-in is programmed against an interface that is already provided by your code, and the plug-in registers itself with that. Your code is not aware about what exactly the plug-in is.

We have a number of plug-ins in our code base already. Take a look at the /Engine/Plugins/ directory.

It not working for me, I add Denepndencies in build.cs, than #include “CustomMeshComponent.h” in my code but it is not recognized.
Should I copy somethig?