Presentation is loading. Please wait.

Presentation is loading. Please wait.

Unreal Engine and C++ We’re finally at a point where we can start working in C++ with Unreal Engine To get everything set up for this properly, we’re going.

Similar presentations


Presentation on theme: "Unreal Engine and C++ We’re finally at a point where we can start working in C++ with Unreal Engine To get everything set up for this properly, we’re going."— Presentation transcript:

1

2 Unreal Engine and C++ We’re finally at a point where we can start working in C++ with Unreal Engine To get everything set up for this properly, we’re going to need an IDE to create C++ files in Which Unreal Engine can link to

3 Unreal Engine and C++ As of Unreal Engine 4.18, we can use Visual Studio Code as a C++ IDE And Unreal Engine will act as the compiler This is a lightweight application that acts as a text editor Though has many extensions for supporting syntax highlighting, code completion, and debugging For lots of different languages Make sure these are definitely installed before moving on Windows: Latest Visual Studio Community Edition 2015/2017 Mac: Latest XCode

4 When making a new project, we see two tabs at the top
Making a C++ Project To use C++ in an Unreal Engine project, we actually need to use a different project type When making a new project, we see two tabs at the top Blueprints C++

5 Feel free to keep the starter content
Making a C++ Project Make sure that C++ is selected Then select Basic Code Give it a location/name Then click on Create Project Feel free to keep the starter content

6 Making a C++ Project Two things will happen here
Unreal Engine will open as normal (like it does whenever we make a project) The default C++ IDE will open as well Windows: Visual Studio 2015/2017 Mac: Xcode If the program crashes, it’s most likely due to 4.18 requiring the latest updates for Visual Studio/Xcode If an earlier Unreal Engine is installed (like 4.16), feel free to use that for now And repeat the earlier steps to make the C++ project

7 The IDE will take quite a while to get set up
Making a C++ Project The IDE will take quite a while to get set up This is because it has to look through all the C++ files in the project Including all the C++ files in Unreal Engine as a whole

8 Making a C++ Project Ex First, make sure the latest version of Visual Studio 2015/2017 or XCode is installed Then run Unreal Engine Create a new C++ project Choose the Basic Code template Choose its name and location Click on Create Project

9 Opening a C++ File In Unreal Engine, most C++ files act as templates to be used Like parent classes Once a C++ file has been made, we decide how it works Afterwards we make an item in Unreal Engine and make it extend the C++ functionality

10 Opening a C++ File We can see all the C++ files the project uses from the Content Browser Click on the Folder icon near the top Choose C++ Classes Here we’ll see everything it contains so far

11 Opening a C++ File All C++ Basic Code projects start with a single C++ file The Game Mode of the current level Let’s look at this file in our C++ IDE

12 Opening a C++ File The IDE project actually contains two key areas
The source code of the engine itself The source code of the project This is separated into two folders: Engine and Games Expand the Games folder Expand the Project folder Expand the Source folder Expand the Project folder inside This is where all the C++ files will go

13 Opening a C++ File The C++ source (.cpp) and header (.h) files for the project itself are here Leave them alone, as they attach any C++ code to the project There’s also the GameModeBase files Let’s open both the header and source files

14 Opening a C++ File The source file is empty (we’ll ignore it for now) The header file has some interesting code here

15 Opening a C++ File Every header file has
The namespace (the project’s name) The class name (prefixed with a letter) A class to extend

16 Opening a C++ File The GENERATED_BODY() function is required It lets Unreal Engine make all the necessary files for this class It has to be the first line inside the class So don’t move it

17 Opening a C++ File Let’s tweak this file slightly
We’ll add a constructor to the header file Note that we need to use the same name as the class And we need to put it in the public section

18 Opening a C++ File We’ll then implement this constructor in the source file We’re going to make it output “Hello World” to the console But how can we do that?

19 Opening a C++ File Unreal Engine has a UE_LOG function
Lets us output text to a log Need to specify Which log to use The type of log it is The text to output

20 Opening a C++ File There are a few types of log
Fatal: prints to log and file, and crashes the game Error: prints to log and file (appears red) Warning: prints to log and file (appears yellow) Display: prints to log and file (appears default colour) Verbose: prints to file only

21 Opening a C++ File Any time we make a change to a C++ file, we need to tell Unreal Engine to compile those files We do not compile from the IDE We can do that by clicking on the Compile button near the top of the editor

22 Opening a C++ File After some compilation, it’ll say Compile Complete near the bottom of the screen Now lets change the GameMode of this level to this class Done in the Blueprints menu In GameMode Here we select the C++ file

23 Opening a C++ File With that selected, we’re nearly ready to test this
First we need to show the log in the editor So we can see our output This is done from the Window menu Developer Tools  Output Log

24 Opening a C++ File Let’s run the game and see if we can spot the output It can be hard to spot, but it’s there!

25 Opening a C++ File Ex In your IDE, go to the [ProjectName]GameModeBase files Games  [ProjectName]  Source  [ProjectName] Open both the header and source files Make a public constructor in the header file, and implement it in the source file Make it output some text to the log Using UE_LOG Save the file, and compile it from the Unreal Engine editor While it’s compiling, open the Output Log window Once compiled, run your game and look for your output in the log

26 Creating an Actor (1) Just editing this GameMode would by pretty dry
Let’s try creating our own Actor class At any point in the Content Browser, we can right-click to add a file At the very top, we’ll see New C++ Class

27 Creating an Actor (1) Clicking on this option opens a familiar window
Looks like the New Blueprint window That’s because we can make a lot of the same files here We’ll click on Actor, and then Next

28 Creating an Actor (1) We then need to decide on
It’s name: this is what we’d refer to it as in Unreal Engine It’s location: which we always leave alone If it’s public or private Public classes can be used in other projects Private classes cannot We’ll choose public for this one, and we’ll call it RotatingCube Can finish by clicking on Create Class

29 This adds two files in a Public and Private folder
Creating an Actor (1) This adds two files in a Public and Private folder The header and source files

30 Creating an Actor (1) We have the class name Also has GENERATED_BODY()
Prefixed with A like before Also has GENERATED_BODY() But it has some more functions this time A constructor The BeginPlay function The Tick function

31 Creating an Actor (1) The BeginPlay and Tick functions work like the events do in a Blueprint Whenever these events are triggered, these functions are called first Then the Blueprint logic

32 Creating an Actor (1) Let’s go to the source file and see these functions They are all here, and ready to go

33 Creating an Actor (1) The constructor sets a Boolean flag to true
This lets the actor tick Meaning we can run some logic every update The BeginPlay and Tick functions simply run their parent’s version We need to keep these here As they help Unreal Engine position and render the actor

34 Creating an Actor (1) Let’s output something to the log when this actor appears in the level Via BeginPlay We’ll then compile our code

35 Creating an Actor (1) Ex Right-click anywhere in your Content Browser
And select New C++ Class Select Actor as the parent class Call it RotatingCube Make the class Public In your IDE, open the source file of this class Add a log message to the BeginPlay function Compile your C++ code in the Unreal Engine editor

36 Creating an Actor (2) Whenever we make a C++ class like this, we need to make a Blueprint for it Otherwise we can’t use it in the game As such, C++ class act as abstract parents We can’t use them directly But we can make children that use them We’re going to make a folder in the Content Browser for any actors we make

37 Creating an Actor (2) Inside here, we’ll make the Blueprint
By right-clicking And choose New Blueprint Class Here, we usually select one of the main options This time we’re going to go into More Classes at the bottom

38 Creating an Actor (2) Here we can search for the class we made
RotatingCube It appears as an option We’ll select that one and click on Select

39 Creating an Actor (2) We’ll call this Blueprint RotatingCube_B
There’s nothing wrong with using the same name But if we’re making a C++ class that acts as an object, it’s best to give the Blueprint class a different name To show that it is the Blueprint version

40 Creating an Actor (2) With it made we can open it in the Blueprint editor Let’s give it a StaticMesh So it’s visible in the scene

41 Creating an Actor (2) With the Blueprint set up, let’s add one of these to the scene Once done, we can play the game

42 Creating an Actor (2) When we play, we can see the output in the log! We’re now ready to add some more complex logic to this Actor!

43 Creating an Actor (2) Ex Make a folder in the Content Browser for your “Actors” Create a Blueprint in this folder Using the C++ class you made as its parent Call it RotatingCube_B Open it in the Blueprint Editor, and give it a StaticMesh To make it visible in the scene Add one of these to the scene, and play the game Make sure you see the output for the Actor in the output log

44 Transforming Actors in C++
If we want to make this cube rotate, we would have added a AddRotation node to the Tick event in the Blueprint However, this C++ class is made for this rotating-ness So we can ‘hard-code’ it into the Tick event in C++

45 Transforming Actors in C++
To do this, we’re going to add three instance variables to the header file The rotation speeds in the X, Y, and Z axes These are going to be floats As Unreal Engine uses floats to store accurate decimal values We’ll make them in a private section Remember that classes are private by default!

46 Transforming Actors in C++
We’ll then give these values in the constructor We’ll set the X to 180 And Y and Z to 0 These are going to be degrees per second

47 Transforming Actors in C++
If we want to rotate the Actor, we can use the AddActorWorldRotation function The same as the Blueprint node! For this though, we need a Rotator object

48 Transforming Actors in C++
We create a Rotate using the FRotator data-type It is a struct that contains variables for The pitch (Y rotation) The yaw (Z rotation) The roll (X rotation)

49 Transforming Actors in C++
As this is a value (not a reference), this Rotator will not exist outside this function So no memory management to worry about

50 Transforming Actors in C++
However, just this isn’t good enough As we’re going to add 180 degrees every tick We want it to be every second So we have to multiply each value by DeltaTime DeltaTime is the amount of time elapsed since the previous tick It keeps games running frame independently

51 Transforming Actors in C++
We can then pass this Rotator into the AddActorWorldRotation function!

52 Transforming Actors in C++
If we compile our C++ code and run the game, we’ll see the cube now rotates!

53 Transforming Actors in C++ Ex
Make three private instance variables in the Actor’s C++ class For the rotation speed in each axis Give them any values you want in the constructor In the Tick function, make a FRotator object By passing in these three variables Multiplied by the DeltaTime Run the AddActorWorldRotation function, passing in the FRotator Compile your C++ code, and run your game Make sure the cube rotates!

54 Random Rotation Ex Use FMath::RandRange(min, max) to get a random number 𝑀𝐼𝑁≤𝑥≤𝑀𝐴𝑋 Use this to assign random rotation speeds in the BeginPlay function Compile your code, and run your game a few times Make sure the rotation is randomised each time!

55 END OF UNIT!


Download ppt "Unreal Engine and C++ We’re finally at a point where we can start working in C++ with Unreal Engine To get everything set up for this properly, we’re going."

Similar presentations


Ads by Google