Unreal Engine: Difference between revisions

From Andreida
Line 382: Line 382:
CreateDefaultSubobject works only in constructor. Everywhere else use NewObject.
CreateDefaultSubobject works only in constructor. Everywhere else use NewObject.
</blockquote>
</blockquote>

====== Examples ======
* [[Unreal Engine, from C++ spawn Blueprint inherited from C++ class, very small example]]
* [[Unreal Engine, from C++ add an ActorComponent to an Actor, very small example]]


===== Delegates =====
===== Delegates =====

Revision as of 15:52, 25 May 2024

Installation

 https://dotnet.microsoft.com/en-us/download/dotnet/3.1
 https://dotnet.microsoft.com/en-us/download/dotnet/thank-you/runtime-desktop-3.1.32-windows-x64-installer
  • Visual Studio 2022: https://visualstudio.microsoft.com/downloads/
    • Workloads / Desktop development with C++ (was suggested by Visual Studio when I loaded a UE5 project)
    • Workloads / .NET desktop development (was suggested by Visual Studio when I loaded a UE5 project)
    • Workloads / Game Development with C++
      • don't unselect anything
      • Windows 10 SDK (1.0.20*)
      • Unreal Engine Test Adapter
      • Unreal Engine uproject support
    • Individual components / .NET 6.0 Runtime (Long Term Support) (was suggested by Visual Studio when I loaded a UE5 project)
    • Individual components / .NET Framework 4.8.1 SDK (or higher)
    • Individual components / .NET Framework 4.6.2 targeting pack (was suggested by Visual Studio when I loaded a UE5 project)
    • Individual components / MSVC 143 - VS 2022 C++ x64/x86 build tools (v14.36-17.6 or newer?) (was suggested by Visual Studio when I loaded a UE5 project)
    • After you install VS 2022, look in the VS Installer at the "Update" button. Enabled? UE5.4+? Yes, update... no problem.
  • Unreal Engine Visual Studio Integration Tool plugin: https://aka.ms/vsituemarketplace (repeat after new engine installation, as of 2024-05-10: not available for 5.4, only up to 5.3)
  • naming convention checker: ".editorconfig" parallel to your .uproject file.
    Content: https://raw.githubusercontent.com/microsoft/vc-ue-extensions/main/Source/.editorconfig
  • HLS configuration file: "shadertoolsconfig.json" parallel to your .uproject file.
    Content: automatically created, defaults to:
{
  "hlsl.preprocessorDefinitions": {
  },
  " hlsl.additionalIncludeDirectories": [
  ],
  "hlsl.virtualDirectoryMappings": {
  }
}

Links

Unreal Engine

Blender

  • Export Options (so that the object is not lying on the side after Export/Import):
    • Forward: Y Forward
    • Up: Z up
  • Changing the object's origin to its bottom:
    • Switch to Edit Mode if needed (Tab). With vertex selection on, choose the center vertex in the bottom. Press Shift+S and choose Cursor to Selected. The 3D cursor will move to the selected vertex. Deselect all and switch to Object Mode (Tab).
    • In Tools choose Set Origin and then Origin to 3D Cursor.
    • Make sure the object's coordinates are set to 0,0,0. Now the object should be placed properly on the XY plane.

C++

Variable names

If you want to use real variable names but have nice names in the UE editor too, then use meta/DisplayName

UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = Camera, Meta = (DisplayName = "First Person Camera Component") )
    UCameraComponent* m_pFirstPersonCameraComponent;

owner / client /server

The instance that spawned the class is always ROLE_Authority. If an Actor is spawned on the client, the client is the authority. 
-- OmaManfred

OmaManfred::

 
And concerning the owner stuff:


When you spawn an instance of a class on the server FOR Client A, you have to set him as the owner.

FActorSpawnParameters params;
params.Owner = PlayerAPlayerController;

SpawnActor<Class>(Class:StaticClass(),params);

Then on Client B you can say IsOwner(PlayerBPlayerController), which should return false. 

Logging

The following is just a copy/paste/change from the UE4 wiki and was really done from different persons, I am just using the work of different people here.

Put it in a Log.h and include it in .cpp files where you want to use these macros.

#pragma once

#include "CoreMinimal.h"

// macros
// - LOG_LINE;
// - PLOG_LINE;
// - LOG -> output log
// -- LOG("text");
// -- LOG("textStatic %s %.2f %d", L"textDynamic", 1.2, 5);
// - PRINT -> screen
// -- PRINT("text");
// -- PRINT("textStatic %s %.2f %d", L"textDynamic", 1.2, 5);
// - PLOG -> output log + screen
// -- PLOG("textStatic %s %.2f %d", L"textDynamic", 1.2, 5);

#if _MSC_VER
#define FUNC_NAME    TEXT(__FUNCTION__)
#else // FIXME - GCC?
#define FUNC_NAME    TEXT(__func__)
#endif

#define LOG_LINE UE_LOG(LogTemp, Warning, TEXT("%s, file: %s, line %d"), FUNC_NAME, *FString(__FILE__), __LINE__);

#define PLOG_LINE \
{ \
	LOG_LINE; \
	PRINT("%s, file: %s, line %d", FUNC_NAME, *FString(__FILE__), __LINE__); \
}

#define PRINT(format, ...) \
{ \
	const FString sFunc = FString::Printf(TEXT("%s"), FUNC_NAME); \
	const FString sText = FString::Printf(TEXT(format), ##__VA_ARGS__); \
	const FString sMsg = FString::Printf(TEXT("%s() : %s"), *sFunc, *sText); \
	if (GEngine) GEngine->AddOnScreenDebugMessage(-1, 5, FColor::White, sMsg); \
}

#define LOG(format, ...) \
{ \
    const FString sMsg = FString::Printf(TEXT(format), ##__VA_ARGS__); \
    UE_LOG(LogTemp, Warning, TEXT("%s() : %s"), FUNC_NAME, *sMsg);\
}

#define PLOG(format, ...) \
{ \
	LOG(format, ##__VA_ARGS__); \
	PRINT(format, ##__VA_ARGS__); \
}

Example Usage

#include "Log.h"
LOG_LINE;
PLOG_LINE; // still being tested
LOG("I am a line of text in the log,");
PRINT("but me, I am a line of text on the display screen, so there!");
LOG("textStatic %s %.2f %d", L"textDynamic", 1.2, 5);
PRINT("textStatic %s %.2f %d", L"textDynamic", 1.2, 5);
PLOG("textStatic %s %.2f %d", L"textDynamic", 1.2, 5);
 FString sPssst("a Secret");
 PLOG("I am telling you: %s", *sPssst);

Custom Log

If you want your own selectable category in the log filter to be able to only show your log messages, then you have to

  • add this line in the Log.h above any defines:
DECLARE_LOG_CATEGORY_EXTERN(aProjectLog, Log, All);
  • replace all "LogTemp" with "aProjectLog"
  • add this line to each .cpp where you want to use these macros:
DEFINE_LOG_CATEGORY(aProjectLog);

You will - in most cases - remove (nearly) all log messages when your code is running well. So... do this only if you need this, not because you think this is the "better" way to use it.

Am I client or server ?

https://answers.unrealengine.com/questions/416305/how-to-know-if-listenserverclient-owner-for-rpc.html

To answer questions 1 and 2: you can use AActor::GetNetMode() 
which returns whether you are a listen server, dedicated server or network client.

         if (GetNetMode() == ENetMode::NM_ListenServer)
         {
         }
         

The ENetMode values are:

    NM_Standalone = 0

    NM_DedicatedServer = 1

    NM_ListenServer = 2

    NM_Client = 3 // is only when you are actually connected to a server

The ordering of the ENetMode values allows you to check multiple things at once: 
GetNetMode() <= 2 means you are the server or standalone, so you have 'global authority'.

UMG with C++ base class

  • create a normal UMG widget
  • create a C++ class with base class UserWidget (UUserWidget)
  • build the project
  • set the parent class of the UMG widget to your C++ class
  • in <Project>.Build.cs add "UMG", "SLATE", "SLATECORE":
PublicDependencyModuleNames.AddRange(new string[] { "Core", "CoreUObject", "Engine", "InputCore", "UMG" });

		PrivateDependencyModuleNames.AddRange(new string[] {  });

		// Uncomment if you are using Slate UI
		 PrivateDependencyModuleNames.AddRange(new string[] { "Slate", "SlateCore" });
  • put into <Project>.h
#include "Runtime/UMG/Public/UMG.h"
#include "Runtime/UMG/Public/UMGStyle.h"
#include "Runtime/UMG/Public/Slate/SObjectWidget.h"
#include "Runtime/UMG/Public/IUMGModule.h"
#include "Runtime/UMG/Public/Blueprint/UserWidget.h"

SLATE

The "canvas" in UMG is "SConstraintCanvas" in Slate.

Multiplayer / Sessions

Do not search for "multiplayer", search for "sessions".

This is a Youtube video I found which gives you an overview, so you already have an idea how it works before you start learning the details: Multiplayer in Unreal Engine: How to Understand Network Replication.

String conversions

from: https://docs.unrealengine.com/en-us/Programming/UnrealArchitecture/StringHandling/FString

FString TestHUDString = FString(TEXT("This is my test FString."));
FName TestHUDName = FName(*TestHUDString);
FText TestHUDText = FText::FromString(TestHUDString);

Classes

  • You can create and use normal C++, like, create a normal C++ class and use new and delete with it.
  • But if you want to use UE stuff, you must use UE stuff and be sure to not mix it with normal C++ handling. This sounds complicated, but it is just a reminder to for example never use "new" for an actor.
  • You can create classes however you want:
    • Get the UE editor to create them (Slow, but it just works. If you don't know better, use this!)
    • Use the UI of your source editor (Visual Studio for example) to create them
    • create them manually (I still have to modify the properties of the created .cpp files, not funny)

UE classes

Please keep in mind that my information might be wrong.

A very common error seems to be the naming of the files. You have to know that common UE classes have an 'U' in front of the class name and actors have an 'A' there. So, if you'd like a 'MyFirstActor', then

  • the files are MyFirstActor.h and MyFirstActor.cpp
  • the class is "AMyFirstActor"
  • the generated file is "MyFirstActor.generated.h"

If you think you know what you are doing and use Visual Studio to create a new class "AMyFirstActor" it will default to the file names AMyFirstActor.h and AMyFirstActor.cpp which is wrong. I mean, it is not wrong, but it is not what you need here. :-)

About categories, there seem to be some ... reserved? ones. Whatever the reason, they don't work. I never got the category "Test" to show, there should be others. So, do not use "Test" as category if you have the same effect, just change the category name instead of trying to force it.

DLLEXPORT

Your classes will be inside a DLL and have to be exported to be usable. If you use UE to create a class you will see something like this:

class P01_TEST_API AMyFirstActor : public AActor

The "P01_TEST_API" between "class" and your class's name is probably just a define like

#define P01_TEST_API DLLEXPORT

Now you ask of course why there is a different define for each project if it is just "DLLEXPORT"? Good question. Next. Sorry. I just don't know (yet).

Update: Ok, I have an idea.
If you want to use the same header files but without "DLLEXPORT", then you just change the define. And that has - of course - to be done per project. So, yes, it does make sense.

minimum

The minimum of a UE class would be like this:

header file
#pragma once

#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "MyFirstActor.generated.h"

UCLASS()
class P01_TEST_API AMyFirstActor : public AActor
{
	GENERATED_BODY()
	
public:	
Source file
#include "MyFirstActor.h"
*.generated.h
#include "MyFirstActor.generated.h"

Write it just like that, you don't create it, you don't look into it, you don't really care about it. BUT you have to include it as LAST include file in your header file.

UCLASS()

This has just to be there. For certain things to work you have to add words inside the brackets. An example would be:

UCLASS(BlueprintType, Blueprintable)

These words do what they say. You can use the class as a type in a blueprint or you can create a blueprint from this class.

GENERATED_BODY()

The default for a struct is public and the default for a class is private. That means "GENERATED_BODY()" in this code

 class P01_TEST_API AMyFirstActor : public AActor
 {
	GENERATED_BODY()

is private.

Don't think, just put it before any access specifiers, it works.

Variables and the like

You might want certain functionality from UE for certain enums, member variables or methods. Like.. it should work as expected and not be cleared by the system which seems to be able to be happen to certain UE container classes if you don't make them UE properties.

Enums

UE enums must be outside of classes (since 4.12). This means you have to be creative with your naming or will slap yourself if you have two enum types which you called EStatus. (This might not even be possible now.)

Example: Header, before the class:

UENUM()
enum AMySuicideActor_EStatus
{
	Task	UMETA(DisplayName = "Task"),
	Idle	UMETA(DisplayName = "Idle"),
};

Inside the class:

protected:	
	UPROPERTY()
	TEnumAsByte<AMySuicideActor_EStatus> m_eStatus;

Inside the .cpp

m_eStatus = AMySuicideActor_EStatus::Idle;

Now, that is nice and all but why would you want it? BECAUSE! No, really, you can now change

UPROPERTY()
TEnumAsByte<AMySuicideActor_EStatus> m_eStatus;

to

UPROPERTY(BlueprintReadWrite, EditAnywhere, Category = "My Tests | Part 1", meta = (DisplayName = "Status") )
TEnumAsByte<AMySuicideActor_EStatus> m_eStatus;

and suddenly you fall in love with UE. This is an actor, so you can drag it into the world in the UE editor. And then you can see the category "My Tests" and below it the subcategory "Part 1" and inside you see a label "Status" and behind it a combo box with the two possible selections: "Idle" and "Task".

  • BlueprintReadWrite: blueprints can read and write this thing/property/variable.
  • EditAnywhere: Edit it in the "Details" and (sometimes) in the "class defaults" and .. oehm.. no idea if there are more places.
  • Category: The area where this data is put when you look at an instance/class/blueprint of it
  • DisplayName: The name which is used to show this data's label (instead of the variable name)
Variables

You can of course do the same things you did with the enums with variables

UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = Stats, Meta = (DisplayName = "Armor"))
float m_fArmor;

There you are. Now you can edit the m_fArmor variable of your instance directly in the editor, after you created an instance by dragging the class into the world, of course.

Sounds sweet? It is!

Functions

You can expose functions like this:

UFUNCTION(BlueprintCallable, Category = "Data Access")
int32 getArmor();
Instantiating UE classes
Actors

The most simple code that works is something like

GetWorld()->SpawnActor<AMyFirstActor>();

There are of course multiple ways to do it and just SpawnActor has like 7 or so overloads.

UClass

If you want to instantiate a U* class instead of an actor then you'll probably be using something like

UAcRandomScaleChange* pAcRandomScaleChange = CreateDefaultSubobject<UAcRandomScaleChange>("RandomScaleChange");

but I can't say anything about it (yet). It works but ... understanding.... hehe.

I found the following sentence, I don't know yet if it is true. But if it is true, you really want to know it:

CreateDefaultSubobject works only in constructor. Everywhere else use NewObject.

Examples
Delegates

If a class says, I have delegates

DECLARE_DELEGATE(FStandardDelegateSignature)
UCLASS()
class XY_API AWithDelegate : public ...

and then has members like

FStandardDelegateSignature m_standardDelegate01;

then another class can say, "hey, I want you to execute some code of me if your delegate01 is activated:

AWithDelegate*......->m_standardDelegate01.BindUObject(this, &AThisIsMe::coolFunctionYouShouldCall);

and then a third class can say, yo man, if you have something connected to your delegate01, pretty please execute that stuff now:

AWithDelegate*......->m_standardDelegate01.ExecuteIfBound();

Enhanced Input

  • An (U)InputAction is just a name. Like "action1", "jump" or "move". They have no meaning by itself.
    • A character might have multiple of these.
  • An (U)InputMappingContext has two reasons for existence:
    • Map a key/mouse/control to an (U)InputAction
    • Collect multiple InputActions
    • A (A)PlayerController might manage one or more of these
  • A character's "SetupPlayerInputComponent" will connect InputActions to methods.

So you have:

Input -> InputMappingContext -> InputAction -> Method

Example

UE Editor

Escape key stops simulation in editor

If you want to change this behavior, open "Edit/Editor Preferences" and search for "stop". Then change under "Play World/Stop" the shortcut "Escape" to whatever you want, for example shift-Escape.

Details/Text/Line-Break

If you are shown a text in the Details tab, chances are you can use shift-return to create a line break.

Removing projects from "Project Open" dialog

Open (use the correct engine version)

%USERPROFILE%\AppData\Local\UnrealEngine\5.3\Saved\Config\WindowsEditor\EditorSettings.ini

and modify/remove

CreatedProjectPaths
RecentlyOpenedProjectFiles

Volume of Sound after building in Visual Studio

  • Content Drawer / Settings / Show Engine Content
  • Engine / Content / EditorSounds / Notifications / *_Cue
  • Volume Multiplier

Source control

Don't use Mercurial (or git) for game development. That would be just plain stupid.
Use Subversion or something like that.

The minimum you have to put under source control:

/Config
/Content
/Source
Project.uproject

Depending on your project:

/.editorconfig
/shadertoolsconfig.json


For ignores, have a look at ignoring files with svn.

External Actors

Under "Content" you will probably after some time see "EXTERNAL_ACTOR" or stuff like that. These are your map's actors, so multiple people can work on different parts of the map at the same time. That is the theory I read. :-)

So, no, don't delete them.

Here is the documentation about it.

Developer Folder

If you have the developer folder(s) enabled "Content / View Options / Show Developers Content" then make sure to not reference it from outside when cooking a project.

Visual Studio

Show errors after build

Tools / Options / Projects and Solutions / Always show Error List if build finishes with errors

Generate Visual Studio Project file

To get a cleaner project you could create a "delete-temps.cmd" with content like the following (match it with your needs):

rmdir /s /q .vs
rmdir /s /q Binaries
rmdir /s /q Build
rmdir /s /q DerivedDataCache
rmdir /s /q Intermediate
rmdir /s /q Releases
rmdir /s /q Saved
rmdir /s /q Intermediate
del *.sln
del .vsconfig

After you cleaned away the trash you can right-click your *.uproject file and select "Generate Visual Studio Project file". This might remove some problems. Or create new ones. So far it only helped me, but it IS a bit brutal, right?

Finding the include file for a class

  • Press ctrl+, (press ctrl and "comma" at the same time)
  • Double-left-click the entry with the correct looking header file.
  • Then you right-click the tab name and select "copy full path".
  • Now you include that path and remove the beginning.
    • How much to leave... "you much have to learn".

If the includes are correct and you want to see the header of something again:

  • Press F12 with your cursor on the class you want to know about to go to the definition.

Just want to switch between header and source?

ctrl-k/-o

Meaning: hold ctrl, press 'k' once, after that press 'o' (letter, not digit) once while still holding ctrl.

Example 1

C:\ue\UE_5.4\Engine\Source\Runtime\Engine\Classes\Particles\ParticleSystemComponent.h

Correct include:

#include "Particles/ParticleSystemComponent.h"

Example 2

C:\ue\UE_5.4\Engine\Plugins\EnhancedInput\Source\EnhancedInput\Public\InputActionValue.h

Correct include:

#include "InputActionValue.h"

Misc

Other Licenses (examples)

not allowed

  • GNU General Public License (GPL),
  • Lesser GPL (LGPL) (unless you are merely dynamically linking a shared library), or
  • Creative Commons Attribution-ShareAlike License.

Code or content under the following licenses, for example, are allowed

  • BSD License,
  • MIT License,
  • Microsoft Public License, or
  • Apache License.

Other

Black Screens or flickering/invisible new action popup

Disable multiplane overlay

Errors

Visual Studio

cannot open source file "CoreMinimal.h"

Error List: "Build + Intelli Sense" => "Build Only"

(source)

I am pretty sure this is NOT a solution but a workaround.

Error MSB3073 [...] -WaitMutex -FromMsBuild" exited with code 6

If you want to build with Visual Studio and get something like

Error	MSB3073	The command "C:\ue\UE_5.3\[...]ShowActor.uproject" -WaitMutex -FromMsBuild" exited with code 6.	

then you can do this:

  • UE-Editor
    • Edit / Editor Preferences
      • Live Coding
        • Enable Live Coding: no
    • File / Save All

Yes, you do this change in the EDITOR's preferences and YES, you have to do it for every project and YES, that makes absolutely NO sense at all.

IntelliSense Error: "can't open file" for includes or "red squiggles under my includes"

Make sure the .cpp-files have in Properties/NMake:

Include Search Path: $(ClCompile_AdditionalIncludeDirectories)

Copy it from other .cpp-files to be sure to get the correct value for your UE-Version/system.

Can't build Solution

  • UE / Edit / Editor Preferences / Live Coding / Enable Life Coding: disable
  • Use the context menu on your project and select "Set as Startup Project".
  • Use "Build <project> (ctrl-B) to build.

This works for me but I am pretty sure there should be a way to have "Live Coding" enabled AND build from Visual Studio. I mean... the UE developers will themselves want to have both options, right? So it should be possible?

Update: I found out that I have to fix the errors in the other (there are ca. 7) projects in the solution to be able to build the solution and to do that I'd have to get the source version of UE. Currently I am very unhappy with all this. A UE5 installation on top of a functioning UE4 installation works. But if you only install UE 5.3 onto a new system, not working :-(

UE 5.4 works pretty well. If you have 5.3, switch to 5.4+, if you are told by someone to install 5.3, ignore him and block him for like ever. And curse him!