Skip to main content

Unreal Engine 5 C++ Project

✍ Last Updated : August 15, 2022

🚪 Prerequisites

Install Unreal Engine 5 and all of the prerequisites [Unreal Engine 5 Installation](Unreal Engine 5 Installation 3a14402970134e8689bdfce4fa8b4de5.md)

❓ Key Question / Problem / Issue

  1. How to create / start Unreal Engine 5 project with C++ as the base?
  2. How to create scripts which could control actor in a runtime?

✅ Expected Output/Definition of Done

  1. Unreal Engine 5 project with C++ as the base is created.
  2. C++ class scripts which have function to control / moving actor in a runtime.

🎁 Resulting Solution

Create C++ Project

  • Open/Launch Unreal Engine 5.

  • If there’s error on launching the apps, make sure you pass all of the engine requirement here Hardware and Software Specifications .

  • Once the Unreal Project Browser show up, at Games section, select one of game template or just select ‘Blank’.

  • At Project defaults, change to C++ as base project, let the rest default.

  • Fill up Project Name and click button Create.

    Untitled

  • Wait until the project open.

Open C++ Project

There’s two ways to open the project.

  • Open like usual from Unreal Project Browser:

    Untitled

  • Open from Visual Studio:

    • Open Visual Studio from project folder.

      Untitled

    • Set solution configuration to Development Editor

      Untitled

    • Run/Debug project with click F5 (Debugging) or Ctrl+F5 (Without Debugging), wait until Build succeeded and Unreal Engine Editor opened.

Create Custom Actor with C++ Script Component

  • Open Content Browser, at folder C++ Classes/[ProjectName].

  • Right click, and create New C++ Class..

    Untitled

  • Because we can make a script which can control object in scene, select Scene Component. Next

    Untitled

  • Fill up name, Create Class

    Untitled

  • After regenerating project files, its automatically open Visual Studio, with two script of our latest created C++ Class component MoveComponents.cpp (implementation) & MoveComponents.h (header) opened.

  • In MoveComponents.h add script to define parameter:

    private:
    	// Parameter
    	UPROPERTY(EditAnywhere)
    	FVector MoveOffset;
    	UPROPERTY(EditAnywhere)
    	float Speed = 1.0f;
    	UPROPERTY(EditAnywhere)
    	bool MoveEnable = true;
    
    	// Computed locations
    	FVector StartRelativeLocation;
    	FVector MoveOffsetNorm;
    	float MaxDistance = 0.0f;
    	float CurDistance = 0.0f;
    	int MoveDirection = 1;
    
  • In MoveComponents.cpp, BeginPlay function. add script:

	StartRelativeLocation = GetRelativeLocation();

	// compute normalized movement
	MoveOffsetNorm = MoveOffset;
	MoveOffsetNorm.Normalize();
	MaxDistance = MoveOffset.Length();

	// check if ticking is required
	SetComponentTickEnabled(MoveEnable);
  • In MoveComponents.cpp,Tick function. add script:

    	// set the current distance
    	if (MoveEnable)
    	{
    		CurDistance += DeltaTime * Speed * MoveDirection;
    		if (CurDistance >= MaxDistance || CurDistance <= 0.0f)
    			MoveDirection *= -1;
    	}
    
    	// compute and set current location
    	SetRelativeLocation(StartRelativeLocation + MoveOffsetNorm * CurDistance);
    
  • Save file, and recompiles. Wait until compiling succeeded.

    Untitled

  • Create blueprint class, right click at Content Browser.

    Untitled

  • Select Actor, name it CustomChair, and open.

    Untitled

  • In Blueprint Editor, Add Component, Find MoveComponents. Add it.

    Untitled

  • Drag MoveComponents to DefaultSceneRoot, to make MoveComponents default.

    Untitled

  • Add StaticMesh component (follow step you add MoveComponent), and name it Chair.

    Untitled

  • Add models at Static Mesh details.

    Untitled

  • At MoveComponents fill Move Offset and speed. Compile and save blueprint.

    Untitled

  • Add CustomChair to Maps by drag it in. Play Level, the chair will move to offset with speed you fill.

    Untitled

Source

#01 - Creating A Project | UE5 C++ Tutorial

#02 - Actors & Components & Custom Move Component | UE5 C++ Tutorial