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?
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???
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!
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!