Splitting a project into parts C++

Hi all. Help a newbie understand the basics of creating games in C++. In my project, I created a function that sets the position of the actor.

The problem is that there will be a lot of such functions and they will take up a lot of space. Can I put this function in another file? How to create this file and what type should it be? And where can I read about it?
I have already studied this question a little, but still I do not understand much.

Usually functions should be located where they are relevant. In your example, the function has responsibility of moving the “MyActor” instance, so its place looks fine.

If there are some functions that will be repeated in various source files, you can consider creating a file called HelperFunctions.cpp / HelperFunctions.h

Here you can keep the functions that are used in many places.

For example:

HelperFunctions.h:

void DoSomething();

HelperFunction.cpp:

void DoSomething() {
// implementation goes here
}

Then you can call it in where needed

MyActor.cpp:

#include "HelperFunctions.h"
void AMyActor:SetLocation()
{
     DoSomething();
}

In order to create these files, in UE editor, go to C++ classes, right-click, select “New C++ Class”, from the options you can select “None”. You can remove whatever is unnecessary there. All cpp files are located in your project directory, in the Source folder. You can edit them from there too.

You can also read about inheritance, which in turn can decrease code repetition.

But overall, IMO it is not a big problem if there are many functions. Just try to keep code repetition to minimum and make them readable.

1 Like

Also consider enrolling to beginner level C++ UE course in Udemy.

Otherwise learning everything on your own might be painful. You can get a good starting point there.

1 Like

I understood everything, thanks. There is just one more question. In this case, I can do without the cpp file. The function can be described in a header file.

Do I need to use the cpp file in this case?

It is considered a bad practice to make definitions in the header. Correct way is just to make the declaration in the header, and definition in the cpp file.

And this is a chatgpt reply :slight_smile:

In C++, it is common practice to define function prototypes (declarations) in header files and the actual function definitions in source (cpp) files. This separation is known as the “header/source file separation.” While it might be tempting to define functions entirely in header files, it can lead to several problems:

  1. Multiple Definitions: If the header file with function definitions is included in multiple source files, each translation unit will have its own copy of the function definition. When linking these units together, the linker will encounter multiple definitions of the same function, resulting in a linker error.
  2. Code Bloat: Placing function definitions in header files increases the size of each source file that includes the header. This can lead to code bloat, as the same function code is duplicated across multiple source files, contributing to larger executable sizes.
  3. Increased Compilation Time: As the function definitions are in the header file, every source file including that header needs to process the function definition during compilation. This can increase compilation time significantly, especially in large projects.
  4. Inconsistent Function Behavior: If the same function is defined in multiple places (due to including the header in multiple source files), it becomes difficult to ensure consistency in the behavior of that function across all translation units.
  5. Debugging Difficulties: Debugging can become more complicated as the function implementation is spread across multiple files, making it harder to trace the execution flow during debugging sessions.

To avoid these problems, it is generally recommended to keep function declarations in header files and move the function definitions to separate source files (cpp files). This way, the declarations are shared across translation units, and the function definition is only compiled once, reducing code duplication and ensuring consistent behavior throughout the program.

You can achieve this by defining the function prototypes in the header file and including that header in the corresponding source file where the function is implemented.

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.