How can I share code between projects?

I want to write some reusable code - mostly UObject derived classes. The code would need access to some core engine code such as FVector and the like, but not any of my game project specific code. I assume I should encapsulate the code inside a UE4 module, however everything I read seems to say that all code needs to be within the project’s source directory structure. Obviously I don’t want to have to duplicate the code on disk though.

Does anyone know the recommended way to do this? Would I essentially have to add a module to the engine? If anyone can point to a project/example somewhere which does this that would be great.

I think you want to make it a Plugin.
That you can add to any project.

Thanks for your reply.
I think that would probably work, and I may attempt it. However, this discussion confirms my suspicion that plugins are not really designed for this purpose. What I want is to create a compartmentalized library of code, which I can create a hard dependency on from multiple projects. But rather than just a standard C++ static lib, the shared code should itself be integrated with UE4’s reflection system.

I feel sure there must be a way to do this, surely others have wanted to do the same thing?

If you’re using Git you can put your reusable module into its own repository and add that repository as a Git submodule to any of your project repositories. Mercurial has something similar except that it uses the term subrepository instead of submodule. Alternatively, if you’re building UE4 from source, and you’re using that build for multiple projects you could put the source for your reusable module into the UE4 source (as a Git submodule).

Hadn’t thought of using source control in that way. Got it all set up with git yesterday and working fine, thanks so much!
Only issue I can see is that the syncing could get a little annoying if I’m frequently jumping between projects and modifying the shared code from each project’s local copy, but in practice this shouldn’t be common. Cheers.

That is exactly what plugins are for. Put your plugin in the engine’s plugins folder and that’s it.
And git is the worst choice for source control in game engines - you have to use Perforce, SVN or Plastic. Git is for code, not for big and binary files. Git can’t handle big files and most important - git can’t check out files.