Udemy: Unreal Engine C The Ultimate Game Developer Course: Section 3 - jgoffeney/Cesium4Unreal GitHub Wiki

Back

C++ Refresher and UE4 Hierarchy

Unreal Engine Hierarchy

  • Object
    • Can store data but can not be placed in level
    • Derived examples: UStaticMesh, UTexture
  • Actor
    • An Object that can be placed in the level with a visual representation
    • Derived examples: ACameraActor, AHUD
  • Pawn
    • An Actor that can be possessed by a controller (takes user inputs to affect the Pawn's position)
    • Derived examples: AWheeledVehicle
  • Character
    • A Pawn with character movement appropriate for its character type (car, person, dog, etc.). You don't just press space to move up but instead to jump with the appropriate motions.
    • Derived examples: APaperCharacter

Unreal Class Nesting

The list below represents the nesting of classes within an Unreal project with the Package being the outermost container. Each lower level is a container for zero or more of the one below it.

  • Package
  • World
  • Level
  • Actor
  • Actor Component

Class Creation in Unreal Engine

When a class is created within the Unreal Editor the name is prepended with a letter based its derived class automatically. All actor children or grandchildren are prepended with the letter 'A'. Anything with a 'U' is derived from UObject.

ACharacter Derived Classes

An ACharacter derived class starts with the following functions:

  • BeginPlay(): call when level starts
  • Tick(): call every frame
    • If you don't need this you can disable Tick() within the constructor
  • SetupPlayerInputComponent(): binds the character functionality to the player input

Reflection and Garbage Collection

Unreal Engine has its own reflection system to expose data to the editor and the blueprints and handle garbage collection.

Garbage Collection

Unreal handles reference counting and when the count for an object is 0 then it is deleted automatically. A macro has to be included prior to the class definition for any class to be included in garbage collection. The macros is UCLASS(). Within the class each variable has to have a UPROPERTY() macro and each function a UFUNCTION() macro to be recognized by the reflection system (to expose to the blueprint system).

Also the final include in the header has to be:

#include "MyClassName.generated.h"

The Unreal Header Tool is invoked at compile time to generate the necessary reflection code.

Creating a UObject

Create a new C++ class using Object as the parent class.

Sidenote: When Visual Studio Does Not Understand the Unreal Macros

  • Close VS and Unreal editor Go to your Unreal project folder and delete folders:
    • Saved
    • Intermediate
    • Binaries
  • Right click on the project file and select "Generate Visual Studio Project".
  • Reopen project and select "Yes" to regenerate missing files.
  • Open Visual Studio project and in the Solution Explorer right click the project name to "Set as StartUp Project".

Allow UObject To Create Blueprint

A UObject derived class cannot be used to generate a blueprint out of the box. To enable this the UCLASS() macro can take parameters. Use UCLASS(Blueprintable) to enable it. Compile the code (I did it within Unreal) and now the option to create the blueprint from the UObject class is enabled within Unreal.

Enable Variables and Functions to be Accessible to Blueprint

The variables and functions have to be publicly accessible

  • Variables
    • Above the variable declaration include UPROPERTY() macro
    • Supply the parameter BlueprintReadWrite to expose the variable for the editor
    • For a variable MyFloat the nodes Get My Float and Set My Float are not available within the blueprint editor.
    • Use the parameter BlueprintReadOnly to only expose Get My Float.
  • Function
    • Above the function declaration include UFUNCTION() macro
    • Supply the parameter BlueprintCallable to make it useable by the Blueprint editor
    • For a function MyFunction a My Function node is available

Note: In the blueprint editor the top white pins are the execution pins and are responsible for the node execution order. Setting and getting values have different colored pins.

Using UObject in Blueprints

Most of this is about creating a blueprint flow to call a constructor for a local variable within an actor object and call a function on that variable (to write a warning to a log).

Blueprint Categories

To group variables and functions within the blueprint selection menu add the parameter Category="SomeCategoryName" to each macro. This will put all the variables and functions with the same category name in the same section.