Creating classes in 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