guide-unrealengine

repository·main·Indexed 23 days ago

https://github.com/mrrobinofficial/guide-unrealengine

A comprehensive learning resource for developers working with C++ in Unreal Engine. The guide covers basic C++ syntax, the Reflection System, Module architecture, and engine-specific systems. It includes practical implementations for creating modules, using Unreal Engine containers (TArray, TSet, TMap), managing delegates, performing line traces from the camera, and handling component overlap and hit events.

Tokens
105.4K
Snippets
241
Records
455
Agent score
79%

What's inside guide-unrealengine

  1. Overview of Unreal Engine C++ Guide

    main

    This repository is a comprehensive guide for developers interested in creating games with Unreal Engine using C++. It covers fundamental C++ programming (data types, pointers) applied to game development, the Unreal Engine module system, and various engine-specific features like the Reflection System, Garbage Collection, and the Unreal Header Tool.

    Compatibility Note: Examples and documentation are intended to work on UE 5.0 and upwards. Code may not be compatible with older versions.

  2. Use Unreal Engine Containers: TArray, TSet, and TMap

    main

    Unreal Engine provides specialized container classes for managing collections of data. The three primary containers are:

    • TArray: A dynamic array that stores elements in contiguous memory. Use this for ordered collections where you need fast access by index.
    • TSet: A collection of unique elements. Use this when you need to ensure no duplicate values exist and require fast lookups.
    • TMap: A key-value pair container. Use this to associate unique keys with specific values for efficient retrieval.

    To use these, ensure you include the appropriate headers from the Containers/ directory.

    #include "Containers/Array.h"
    #include "Containers/Set.h"
    #include "Containers/Map.h"
    
    // Using TArray to create an array of integers
    TArray<int32> IntArray;
    IntArray.Add(1);
    IntArray.Add(2);
    IntArray.Add(3);
    
    // Using TSet to create a set of strings
    TSet<FString> StringSet;
    StringSet.Add(TEXT("Apple"));
    StringSet.Add(TEXT("Banana"));
    StringSet.Add(TEXT("Orange"));
    
    // Using TMap to create a map of integers and strings
    TMap<int32, FString> IntStringMap;
    IntStringMap.Add(1, TEXT("One"));
    IntStringMap.Add(2, TEXT("Two"));
    IntStringMap.Add(3, TEXT("Three"));
  3. What is the Unreal Header Tool (UHT)?

    main
    The Unreal Header Tool (UHT) is a code generator and reflection system in Unreal Engine. It processes special macros and meta tags within C++ header files to generate the additional code required for Unreal Engine's reflection system. This system enables critical engine features such as Blueprint integration, serialization, and networking.
  4. Overview of Unreal Engine Collections (TArray, TSet, TMap)

    main

    Unreal Engine provides three primary container types for managing data:

    ContainerDescriptionUse Case
    TArrayDynamic array supporting random access and iteration.Storing collections where size changes frequently and quick access is needed.
    TSetSet structure storing unique elements in no particular order.Maintaining distinct elements and performing fast membership checks.
    TMapAssociative container storing key-value pairs.Creating dictionaries or associative arrays for efficient lookup via unique keys.
  5. Use USubsystem to provide engine services

    main

    Subsystems provide modular services or functionality that can be accessed by other parts of the engine or game. They handle their own initialization and shutdown.

    There are four primary types of subsystems, categorized by their lifetime:

    1. Engine: Lives for the duration of the engine's lifetime.
    2. Editor: Lives for the duration of the editor's lifetime.
    3. GameInstance: Lives for the duration of the game instance (persists across level changes).
    4. LocalPlayer: Shared lifetime of local players.
  6. Understand Class and Struct Members

    main

    Members are the variables and functions that belong to a class or object.

    Variable Members

    Variables store data (numbers, strings, booleans, etc.). You can use assignment abbreviations for common operations:

    • n += k $\rightarrow$ n = n + k
    • n -= k $\rightarrow$ n = n - k
    • ++n $\rightarrow$ Increment n and return the new value.
    • n++ $\rightarrow$ Return the current value of n, then increment it.
    • --n $\rightarrow$ Decrement n and return the new value.
    • n-- $\rightarrow$ Return the current value of n, then decrement it.

    Function Members

    Functions are blocks of code that perform tasks. They can be standalone or member functions (defined within a class). Functions can accept input parameters and return values.

  7. Understand the Unreal Engine Reflection System

    main

    The Reflection System allows properties and functions to be accessed and modified at runtime. It is powered by the Unreal Header Tool (UHT), which generates metadata during compilation.

    Key components:

    • GENERATED_BODY() macro: Must be included in class definitions.
    • [FileName].generated.h: The header file generated by UHT that must be included in your source file.

    This system enables essential engine features like serialization (saving/loading), networking, and editor visibility.

  8. C++ Syntax: Typing, Semicolons, and Braces

    main

    C++ follows a structured syntax with specific rules for statement termination and scoping:

    • Strong Typing: C++ is strongly typed. You must explicitly declare types (e.g., int a = 5;). Unlike weak typing languages (like Python), you cannot assign a value to an undeclared identifier.
    • Semicolons (;): Used to mark the end of a statement. Missing a semicolon will result in a compilation error.
    • Curly Braces ({}): Used as block delimiters to define the boundaries of code blocks, such as the body of functions, classes, or namespaces. These braces define the scope of variables.
  9. Common C++ Design Patterns and Principles

    main

    When structuring C++ code, several industry-standard patterns and principles can improve maintainability and scalability:

    • SOLID Principles: A mnemonic for five principles (Single Responsibility, Open-Closed, Liskov Substitution, Interface Segregation, and Dependency Inversion) designed to make software more flexible and understandable.
    • KISS (Keep It Simple, Stupid): Emphasizes simplicity and avoiding unnecessary complexity to enhance readability.
    • Singleton Pattern: Ensures a class has only one instance and provides a global access point (useful for shared resources).
    • Observer Pattern: Establishes a one-to-many dependency where 'observers' are notified automatically when a 'subject' changes state (common in event-driven systems).
    • Factory Pattern: Provides an interface for creating objects without exposing the specific creation logic to the client.
    • Strategy Pattern: Encapsulates algorithms into separate classes, allowing them to be swapped at runtime.
    • MVC (Model-View-Controller): Separates an application into Data (Model), Presentation (View), and Input Handling (Controller).
  10. Compare Static and Dynamic Libraries

    main

    The linker resolves undefined symbols to create executable files. You can choose between static and dynamic linking depending on your project needs.

    Static Libraries

    Static libraries are merged into the final .exe file at compile time. This increases the executable size but ensures all code is available at runtime.

    • Windows: .lib
    • Linux: .a
    • macOS: .a

    Use case: Use static libraries when you want to ensure code is available at compile time and want to avoid runtime dependency issues.

    Dynamic Libraries

    Dynamic libraries are read by the .exe at runtime. They can reduce disk and memory usage and allow for easier patching/updates.

    • Windows: .dll
    • Linux: .so
    • macOS: .dynlib

    Drawbacks: You must ensure the correct versions of the library files exist and are accessible at runtime.

  11. Using Dynamic Libraries

    main

    A dynamic library (or DLL) is compiled from source code into a file that is read by the .exe file at runtime rather than being merged into the executable at compile time.

    Benefits:

    • Can reduce disk and memory space usage.
    • Improved serviceability (easier bug fixes and security patching).
    • Improved maintainability.

    Challenges: Developing with dynamic libraries requires ensuring that all required files exist, are the correct versions, and can be executed correctly at runtime.

    File Extensions:

    • Windows: .dll
    • Linux: .so
    • macOS: .dynlib
  12. Implement Generic Programming with Templates

    main

    Generic programming uses the template keyword to write code that works with any data type. This allows you to create functions or classes that are instantiated with specific types at compile-time, reducing code duplication.

    template <typename T>
    T add(T a, T b)
    {
        return a + b;
    }
    
    int result1 = add(5, 10);        // Instantiated as add<int>(5, 10)
    double result2 = add(3.5, 2.7);  // Instantiated as add<double>(3.5, 2.7)