Search in
Sort by:

Question Status:

Search help

  • Simple searches use one or more words. Separate the words with spaces (cat dog) to search cat,dog or both. Separate the words with plus signs (cat +dog) to search for items that may contain cat but must contain dog.
  • You can further refine your search on the search results page, where you can search by keywords, author, topic. These can be combined with each other. Examples
    • cat dog --matches anything with cat,dog or both
    • cat +dog --searches for cat +dog where dog is a mandatory term
    • cat -dog -- searches for cat excluding any result containing dog
    • [cats] —will restrict your search to results with topic named "cats"
    • [cats] [dogs] —will restrict your search to results with both topics, "cats", and "dogs"

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? :(

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??? :O

Please advise. Thanks.

Product Version: UE 4.20
more ▼

asked Jan 27 '19 at 11:38 PM in Using UE4

avatar image

23 1 2 7

(comments are locked)
10|2000 characters needed characters left
Viewable by all users

1 answer: sort voted first

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!

more ▼

answered Jan 28 '19 at 05:05 AM

avatar image

1.2k 4 5 8

avatar image JansenSensei Jan 28 '19 at 05:40 AM

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

avatar image JansenSensei Jan 28 '19 at 05:43 AM

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!

(comments are locked)
10|2000 characters needed characters left
Viewable by all users
Your answer
toggle preview:

Up to 5 attachments (including images) can be used with a maximum of 5.2 MB each and 5.2 MB total.

Follow this question

Once you sign in you will be able to subscribe for any updates here

Answers to this question