How do I reuse C++ scripts between projects?

Coming from a decade in Unity I am finding this the most confusing thing of all so far. If I write a C++ class in ProjectA then it seems that code can only ever be used in ProjectA. What am I missing here?

UE4 has no “Import C++” option that I can see…
VS2017 has an “Add existing” option which only seems to copy in a link to the file as the file is not copied over to the project and in VS I actually see 2 copies of the .h and the .cpp if I tell UE4 to create the class for me again.

I am writing utility classes that I intend to use in all future projects and yet UE4 seems to say “Nope. Start from scratch and rewrite all code every time”. Surely this must be wrong… so what am I missing here? :frowning:

Also, please note that I also can’t go into the original project and select an Actor or Pawn that the code may be on to generate some sort of package from it since my code is not derived from any of the classes that allows it to be placed in the scene. Plain, non-subclassed classes which are then subclassed by my other classes, all making use of FString and other UE4 classes… not visible in the editor at all, no way to move it into another project…

Can it be that an engine as powerful as UE4 lacks this simple ability to let you reuse code between projects??? :open_mouth:

Please advise. Thanks.

Provided the class files (.h and .cpp) that you’re copying don’t rely on other, un-copied classes from the original project, you should be able to copy what you need over and get things working with little effort.

Off the top of my head, I can think of just a handful of short steps you’ll need to take to adapt your code from ProjectA to make it work with ProjectB:

  • Change the project module name in each of your header files from MYPROJECTA_API (or whatever) to MYPROJECTB_API (or whatever) — just make the copied headers match the destination project; find/replace should make this easy
  • Delete (or simply move) the Intermediate and Binaries folders in ProjectB’s root; this is like a manual “Clean” — these directories and their contents will be recreated by the following steps
  • Re-generate your project files by right-clicking your project’s .uproject file (in Explorer or Finder) and choosing Generate Visual Studio Project Files (for Windows) or Generate Xcode Project (for Mac OS)
  • Re-open your project’s solution in either Visual Studio or Xcode, and recompile

For a better explanation of why these steps are needed, have a look at this Managing Game Code article in the UE4 official docs. The latter half of that document is probably of more relevance in your case.

Hopefully this sheds some light on the subject and helps move you forward!

5 Likes

Thanks a lot for that info. This is going into my bookmarks ASAP as this includes far more detail than what I ended up doing.

After much searching and trying and failing and retrying I finally managed to get my code into another project by manually copy pasting the code from one project into the other and then in VS I selected to Add Existing and selected the files that were now already inside the project. This basically created a shortcut to the files that were where they needed to be while compiling as able to find the files since they were were they were meant to be.

A hack to be sure but it worked. I was actually hoping there would be a way like Unity’s Export Package system where you can package up your code and just install into any project with a simple drag and drop. Taking into account the fact that people are selling assets on the store that includes code, surely Unreal has a way to package up code and install it from there again… but I haven’t gotten that far in my Unreal learning yet… but that seemed the most logical answer and yet Google seems to be filled with people asking the same question I just asked but starting years back already.

Odd that this would be such a convoluted process… I’m guessing this is just the price one has to pay for using an ACTUAL coding language that mods the engine rather than using an INTERMEDIARY language that just gets interpreted at runtime… or something along those lines.

Anyway, thanks for the info. Will be sure to give that a go when I export this code into the next project and will definitely give that article a read.

Thanks a bunch

P.s. I see what you meant…

Code files can be created through Visual Studio and added to the game project through the Solution Explorer in the usual manner. You can also add code files to the appropriate folders outside of Visual Studio and rebuild the solution and project files automatically. This makes it easy to add lots of files quickly through the operating system UI

That will in deed simplify life and skip me from hacking things into place… Thanks!

Here’s a translated guide for integrating modules from an old Unreal Engine project into a new project without needing to replace API macros across multiple files or modify the Build.cs, as this approach can be tedious and potentially risky. Here’s a simpler method:

  1. Copy the Folder: Take the folder under the Source directory of the old project (the folder containing the C++ code you want to reuse) and copy it into the Source directory of the new project.
  2. Edit the .uproject File: Open the .uproject file of the new project. This is a JSON file. Under the "Modules" section, add a new entry that matches the module configuration of the old project. For example:
    “Modules”: [
    {
    “Name”: “CurrentProjectName”,
    “Type”: “Runtime”,
    “LoadingPhase”: “Default”,
    “AdditionalDependencies”: [
    “Engine”
    ]
    },
    {
    “Name”: “CopiedProjectModuleName”,
    “Type”: “Runtime”,
    “LoadingPhase”: “Default”,
    “AdditionalDependencies”: [
    “Engine”
    ]
    }
    ]
    Compile, Link, and Run: After adding the copied module to the uproject file, compile and run the project. You should see that all C++ scripts from the old project are now added to the new project in their own separate folder.

1 Like

I know this is a resolved topic, but I feel that you really have to mention plugins in this context. Plugins are made for sharing functionality across multiple projects, and they can add functionality to both the editor and your games.

For example, I have several plugins in my github that I can bring into my projects by adding them as git submodules. This makes the process smooth and painless (well, almost) and any updates to a plugin will automatically reflect in all projects where it is used. If you don’t use git, I’m sure that your version control system has something similar, or a simple copy-paste may also work (though updating can be a hazzle).

So one way of going about this could be to develop things with modules, and as soon as you need to reuse something, you turn it into a plugin. Then you only need to perform the migration described by StarlinGele once. You can also reintegrate the new plugin in the old project, so it can benefit from new updates :slight_smile: