Creating classes in visual studio

Anyone have a small tutorial on adding classes to your game in visual studio instead of adding them through unreal editor? It never seems to work for me.

You can just add the Cpp file and the H file from visual studio, but remember that you need to place them in their correct folders (VS wont add them properly by default), and then regenerate the project files so everything is updated.
What i do the most, is to just copypaste another file, so i have the template, then i just change the class name and start doing changes to it.

Remember that your file needs to have the same name as your class, and the class need to have the letterss( U for UObjects, A for AActors), so if yor class is AMyActor, the file must be named MyActor.h and MyActor.cpp

Just tried it out and “Generate project files” deleted the classes I added from visual studio.

I’ll assume you’ve created a project and have the basic Visual Studio project set up.

  1. Add a .cpp and a .h like you would normally from the Visual Studio Add → New Item… wizard (class wizard works too)
  2. These new files will be created in Your Project/Intermediate/Project Files folder. Go there and move them out to Your Project/Source/Your Project (where there’s probably a few default created classes already)
  3. Run Generate Visual Studio Files from right clicking on your .uproject file
  4. Fire up Visual Studio and edit your new files

General rules for .h files

  1. Use pragma once and not #ifndef MYFILE …
  2. Use forward slash in #include, e.g #include “GameFramework/Actor.h”
  3. Always include “MyClass.generated.h” last
  4. Prefix class names (but not the file name) with A for classes that extend AActor and U for classes that extend UObject.
  5. Use the Macros UCLASS(), UPROPERTY() etc. More here: Programming with CPP in Unreal Engine | Unreal Engine Documentation

General rules for .cpp files

  1. Include your main game/project .h file, #include “YourProject.h”

  2. Include the class .h file

  3. Create the constructor generated by Unreal Header Tool

    AMyActor::AMyActor(const class FPostConstructInitializeProperties& PCIP)
    : Super(PCIP)
    {
    }

And then you should be able to compile. Note that IntelliSense will tell you there are errors in your code before you build. Before the compile begins Unreal Header Tool will come in and generate a bunch of code for you. If IntelliSense still shows errors after a successfull build simply make a small change to a file to force it to reparse the files.

Example MyActor.h

// MyActor.h
#pragma once

#include "GameFramework/Actor.h"
#include "MyActor.generated.h"

UCLASS()
class AMyActor : public AActor
{
	GENERATED_UCLASS_BODY()

	UPROPERTY()
	bool bBool;

};

Example MyActor.cpp

// MyActor.cpp
#include "MyProject.h"
#include "MyActor.h"

AMyActor::AMyActor(const class FPostConstructInitializeProperties& PCIP)
	: Super(PCIP)
{
	bBool = true;

}

This should get you started. You can read more about source file folder structure here:
https://docs.unrealengine.com/latest/JPN/Engine/Basics/DirectoryStructure/index.html
Using the Classes, Public and Private folder structure will apparently speed up compile time but is not required.

EDIT
Code block is not playing nice :frowning:

1 Like

Thank you for going into detail. Helped a lot.

Do you know if there’s a visual studio configuration option that I can change which changes which directories new header and cpp files are inserted into? It’s a bit tedious to go through and move these files and repath them in the solution explorer :frowning:

Oh god, I’m stupid. I figured it out.

For the sake of future reference for anyone else:
When you right click on a filter in solution explorer and select “Add New Item”, there’s a dialogue box which pops up. Right below where you name the file is a directory path where the file will get placed. This is what you want to change. By default it points to a folder in the intermediate directory. You want to change this to point to your source directory so that the unreal build process will see your file.

I do not find the file “MyActor.generated.h” when I try add code to a project using c++ class wizard. What must be done? Please help

I wonder if there is a way we could create a plugin inside VS to automate all these steps. I am new to programming but I can see how horrible the process will be to create all the classes that I need via VS using these steps. If there isn’t a tool I will try to create one when I understand C++ more and I will share it.

There is a Plugin called Visual Studio Integration Tool. How to create class is described here: Unreal Engine Integrations Now Available in Visual Studio 2022 - C++ Team Blog