top of page

Results found for empty search

  • Execute OS-level terminal commands from within Unreal Engine

    We can execute console commands from the unreal engine editor via the FPlatformProcess::CreateProc function call. Execute commands from the Windows default Command Prompt: FString CommandToExecute = FString("//C mkdir NewFolder"); FPlatformProcess::CreateProc(TEXT("cmd.exe"), *CommandToExecute, true, false, false, nullptr, 0, nullptr, nullptr); The above code will automatically find the default install location of cmd on the Windows environment and execute any command that is passed via the CommandToExecute variable. Unreal Engine Command CMD Shell .exe Execute external .exe files from unreal: FString ExeFilePath = FString("C:\\Users\\TheUnrealGuy\\Desktop\\Test.exe"); FPlatformProcess::CreateProc(* ExeFilePath , nullptr, true, false, false, nullptr, 0, nullptr, nullptr); The above code will execute any exe file on the path mentioned in the string. to get the path of the exe: Select the exe file Right-click on the exe and click on copy as path Paste the copied path within the double quotes in the ExeFilePath variable The variable now should look something like this FString ExeFilePath = FString("C:\Users\TheUnrealGuy\Desktop\Test.exe"); We need to replace all the single backslashes (\) with double backslashes (\\) for cpp to recognize it as a single backslash (\) is used to specify an escape sequence character in cpp Similarly, we can also execute such console commands in Linux and Mac os: FString CommandToExecute = FString(""); FPlatformProcess::CreateProc(TEXT("/bin/sh"), *CommandToExecute, true, false, false, nullptr, 0, nullptr, nullptr); For Linux and Mac, we need to change the execution command and instead of cmd we will have to run shell Execute these commands from the blueprint: Go to the header file of your cpp class and add: protected: UFUNCTION(BlueprintCallable) void ExecuteConsoleCommand(const FString& Executable, const FString& CommandToExecute); Create a declaration for the above function in cpp void AExampleClassName::ExecuteConsoleCommand(const FString& Executable, const FString& CommandToExecute) { FPlatformProcess::CreateProc(*Executable, *CommandToExecute, true, false, false, nullptr, 0, nullptr, nullptr); } After compiling the code you can now right-click and search ExecuteConsoleCommand function in your blueprint and you can execute commands via blueprints. I would recommend creating a plugin or an interface call for easy access to this function

  • Getting Started With C++ In Unreal Engine | Unreal Engine C++

    Understanding Unreal's Class Structure: Unreal Engine’s C++ framework provides a robust system for building games, but it has its quirks. Unlike pure C++, Unreal leverages its own macro-based system and follows a specific hierarchy of classes, most of which inherit from the core UObject and AActor classes. Inheritance is very crucial in Unreal Engine, almost all of your classes will be inherited from a pre defined Unreal's class, UObject and AActor are an example. 1. The Core Classes in Unreal C++ UObject : The base class for most objects in Unreal Engine. Many classes inherit from UObject directly, which enables reflection and serialization—two core Unreal Engine features. Objects deriving from UObject are generally non-spatial (they don’t need a 3D representation). AActor : The base class for all objects that have a presence in the world. Unlike UObject, AActor has properties and methods suited for physical objects or entities in a level (such as characters, projectiles, or power-ups). For example if you create a character class with name HeroPlayer the inheritance hierarchy will be as follows, UObjectBase<UObjectBaseUtility<UObject<AActor<APawn<ACharacter<HeroPlayer 2. Core Class Macros Unreal C++ uses macros like UCLASS , UPROPERTY , and UFUNCTION  to define classes, properties, and functions that the Unreal Engine will recognize, exposing them to features like the Editor, Blueprints, and serialization. UCLASS : Use this macro at the start of any class that derives from UObject or AActor. It tells Unreal that this class can be used in the editor, making it visible to Blueprints, other C++ code, and various Unreal Engine systems. UCLASS() class MYGAME_API AMyActor : public AActor { public: // Class content here }; UPROPERTY : Adds metadata to class members, making them accessible to the Unreal Engine Editor. It’s especially useful for game designers to tune gameplay elements without requiring to modify the value of variables from the code thus saving recompilation time. UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Gameplay") float Health = 100.0f; A. EditAnwhere: Specifies that this property can be edited from the details panel of the blueprint and instance. Thus removing the need to change the value from code saving recompilation time. B. BlueprintReadWrite: Allows the blueprint to access this variable and, read and write data from/to it. C. Category: Specifies the family under which this variables falls in, helps to segregate different families of variables for easier editability. UFUNCTION : Use this to expose functions to Unreal, enabling blueprint access and setting specific parameters like replication for multiplayer games. UFUNCTION(BlueprintCallable, Category = "Actions") void TakeDamage(float DamageAmount); A. BlueprintCallable: Exposes the function to blueprint and thus enabling this cpp function to be invoked anywhere from the blueprint. B. Category: Helps to segregate functions which fall under similar sets functionality under a single name, hence making it easier to find them when searching from. The Actor Lifecycle: From Creation to Destruction In Unreal, actors follow a specific lifecycle from the moment they're spawned until they’re removed from the game. Let’s look at the most critical lifecycle methods you’ll need. 1. Spawning and Initialization Constructor : The constructor is called when an actor is created, and it’s where you initialize default values and actor components. AMyActor::AMyActor() { // Initialize default values here } BeginPlay : Called when the game starts or when the actor is spawned into the game world. This is a great place to initialize game-related logic, like setting health values or configuring weapon parameters. void AMyActor::BeginPlay() { Super::BeginPlay(); // Initialize game-specific code here } 2. Updating Actors Over Time Tick : The Tick function is called every frame, making it perfect for things like checking distances, debugging or monitoring player stats. Be cautious with Tick as it can heavily impact performance if used excessively or for complex calculations. void AMyActor::Tick(float DeltaTime) { Super::Tick(DeltaTime); // Code to run every frame } A. DeltaTime: Time elapsed between two consecutive tick calls. 3. Ending the Actor's Lifecycle EndPlay : Called right before the actor is removed from the game. Use this to clean up resources or finalize any state. Note this is different from destructor as destructor is called when a class is deallocated from the memory. void AMyActor::EndPlay(const EEndPlayReason::Type EndPlayReason) { Super::EndPlay(EndPlayReason); // Clean up code here } Destroy : This function removes an actor from the game world. If you need to remove an actor from the level at any moment, calling Destroy() will properly clear it. void AMyActor::DestroyActor() { Destroy(); } Putting It All Together: Example Actor Class Here’s a simple example of an actor that spawns with default health, updates it over time, and logs when it is destroyed. Header File (.h) UCLASS() class MYGAME_API AHealthActor : public AActor { GENERATED_BODY() public: AHealthActor(); protected: virtual void BeginPlay() override; virtual void Tick(float DeltaTime) override; virtual void EndPlay(const EEndPlayReason::Type EndPlayReason) override; UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Health") float Health; private: void DecreaseHealth(float DeltaTime); }; Cpp File (.cpp) AHealthActor::AHealthActor() { Health = 100.0f; // Default health } void AHealthActor::BeginPlay() { Super::BeginPlay(); UE_LOG(LogTemp, Warning, TEXT("Actor spawned with health: %f"), Health); } void AHealthActor::Tick(float DeltaTime) { Super::Tick(DeltaTime); DecreaseHealth(DeltaTime); } void AHealthActor::EndPlay(const EEndPlayReason::Type EndPlayReason) { Super::EndPlay(EndPlayReason); UE_LOG(LogTemp, Warning, TEXT("Actor is being destroyed")); } void AHealthActor::DecreaseHealth(float DeltaTime) { Health -= DeltaTime * 5.0f; if (Health <= 0) { UE_LOG(LogTemp, Warning, TEXT("Actor health depleted")); Destroy(); } } Wrapping Up: Understanding Unreal Engine’s class structure and actor lifecycle is essential when working with C++. From UObject to AActor, each class serves a unique role, and lifecycle methods help you manage actors throughout their lifetime in your game. Start simple, play around with these core concepts, and soon you’ll be ready to tackle more advanced Unreal C++ features. If you found this post helpful, subscribe  to receive weekly updates and insights into Unreal C++ programming! Your feedback is incredibly valuable, so feel free to leave a comment below. Are there specific Unreal Engine topics you'd like me to cover next? Let me know—I’d love to tailor future posts to help you in your development journey!

github.png

Github

LinkedIn

Discord

YouTube

© 2035 The Unreal Guy. Powered and secured by Wix

bottom of page