I am working with unreal engine c++ and wonder what the purpose of the controller is and why the input is not handled by the controller but the pawn. Can you help me please?
Assistant
User
What is the default tick rate in unreal? Is it fps dependant? Is there a function like FixedUpdate() in unity?
Assistant
User
What does the super::tick function do?
Assistant
User
Does the UGameInstance have an tick function?
Assistant
In Unreal Engine, the concepts of Controllers and Pawns (including Characters) are central to understanding how input and interactions are managed within the game world. These classes are part of Unreal's Actor system and play distinct roles in gameplay logic, especially concerning player control and AI behavior. Let's break down the purpose of each and why input handling is typically associated with Pawns rather than Controllers.
Controllers
Controllers are an abstract concept that represents the will behind actions taken by characters or entities in the game world, whether they be players (through PlayerControllers) or AI (AIControllers). Controllers do not have a physical representation in the world themselves. Their primary purpose is to decide what actions the entity they control should undertake. In a more concrete sense:
PlayerControllers handle input from human players and translate that input into actions performed by Pawns.
AIControllers dictate the behavior of AI Pawns, determining how they navigate the environment and interact with the player and other AI.
Pawns
Pawns, including Characters (a specialized subclass of Pawn), are the entities that possess a physical representation in the game world. They can be anything from human characters, animals, vehicles, or any controllable entity. Pawns are designed to respond to commands or inputs, executing movements or actions in the game space.
Input Handling
The pattern in Unreal Engine where input is typically handled by Pawns instead of Controllers is primarily due to the separation of concerns and flexibility. Here's why:
Decoupling Control from Action: By separating Controller logic from the Pawn, Unreal provides a clean separation between deciding on an action (Controller) and executing it (Pawn). This design allows for easily switching the control between different Pawns without changing the core logic of either the Controller or the Pawn, enhancing modularity and reusability.
Character/Camera Control: In many games, players directly control a character or vehicle. Unreal's default setup lets Pawns receive input because this fits the common scenario where direct control over movement and actions (e.g., jumping, shooting) is needed. Pawns are designed with components (like a CharacterMovementComponent in the case of Characters) that already have sophisticated handling for movement and physics interactions, which is why they naturally fit for processing inputs related to these actions.
Flexibility for AI and Player Control: This design also simplifies the switching between AI and player control. A Pawn might initially be controlled by an AIController but then come under player control (PlayerController) if, for example, the player character possesses the Pawn. The underlying Pawn doesn't need to be altered; the Controller changes.
Input Abstraction and Reusability: Handling input in Pawns allows for easier reuse of Pawn classes across different types of games or scenarios within the same game. A single Pawn class can respond to inputs from a PlayerController or directives from an AIController without modification, making it a versatile and reusable entity within the engine.
However, it is essential to note that while Pawns typically handle input due to these design principles, Unreal Engine is highly flexible, and developers can architect their systems differently if their game's design necessitates it. For example, a Controller could interpret input to modify the game state directly or control multiple entities simultaneously, bypassing the need for direct Pawn control. The choice depends on the specific requirements and design of your game.
In Unreal Engine, the concept of a "tick rate" refers to how often the engine's "Tick" function is called. The Tick function is a core part of many classes, including Actors, and it's where you put code that you want to be executed regularly, such as every frame or on a fixed interval. The frequency of the Tick function can indeed be seen as analogous to frames per second (FPS) in the context of rendering, but it's essential to understand that ticking behavior extends beyond just rendering frames.
Default Tick Rate and FPS Dependency
The default behavior in Unreal Engine is that the Tick function is called once per frame. Therefore, you could say it is "FPS dependent" because if the game is running at 60 FPS, the Tick function for a given Actor will, by default, be called 60 times per second. Conversely, if the game's framerate drops to 30 FPS, the Tick function will be called 30 times per second for that Actor. This makes the tick rate variable by default, as it's tied to the game's performance and framerate.
Fixed Time Step (Similar to Unity's FixedUpdate)
Unlike Unity, which has a FixedUpdate function that is called at a consistent interval regardless of the frame rate, Unreal Engine does not have an exact equivalent function built into the framework. FixedUpdate in Unity is designed for physics updates and runs at a consistent rate, ensuring that physics simulation remains consistent regardless of the game's frame rate.
In Unreal Engine, to achieve similar behavior to Unity's FixedUpdate (i.e., a fixed time step for game logic, often used for physics calculations to maintain consistency), you can implement your own system within the Tick function. Here's a basic approach:
Accumulate Delta Time: Accumulate the delta time passed to the Tick function.
Fixed Time Step Check: Regularly check if the accumulated time has surpassed your desired fixed time step threshold (e.g., 1/60th of a second for 60 updates per second).
Execute Logic: If the accumulated time has reached or surpassed the threshold, execute your fixed timestep logic, and subtract the fixed timestep value from your accumulated time.
Here's a simplified example of how you might implement this in a Tick function:
In this code, FixedDeltaTime would be your desired time step (e.g., 1.0f / 60.0f for 60 times per second), and AccumulatedTime is a float member variable you've added to your Actor class to track the accumulation of delta time.
Remember, this manual approach gives you control over which parts of your game logic should run at a fixed time step, but it also means you need to manage the potential complexities of dealing with accumulated time and ensuring your game logic can handle variable execution intervals cleanly.
Physics and Fixed Time Steps
It's also worth noting that Unreal Engine's physics engine (PhysX) does indeed operate on a fixed time step by default to maintain consistency in the simulation. This is handled internally by the engine.
Conclusion
While Unreal Engine doesn't provide a built-in FixedUpdate function like Unity, you can achieve similar functionality through the technique described above. This allows for consistent game and physics logic updates, independent of the frame rate, which is essential for certain types of games or simulations requiring precise and consistent behavior.
The Super::Tick function call is an example of calling a base class's implementation of a method in Unreal Engine, specifically within the context of C++ programming. When you see Super::Tick(DeltaTime);, it's invoking the Tick function of the base (or parent) class from which the current class is derived.
Purpose
The purpose of calling Super::Tick(DeltaTime); at the beginning of an overridden Tick function is to ensure that the base class's tick functionality is executed. This is crucial because many Unreal Engine classes, especially those deriving from AActor or its subclasses, rely on the base Tick implementation to perform essential updates that keep the engine's internal systems running correctly. Failing to call the superclass's Tick might lead to unexpected behavior or bugs, as vital updates that the base class performs are skipped.
Example:
Suppose you have a custom actor class named AMyActor that derives from AActor. AActor has a Tick method that Unreal Engine calls every frame. If you want to add custom behavior to your actor every frame, you'd override the Tick method in your AMyActor class.
Here's a simplified example:
cpp
void AMyActor::Tick(float DeltaTime)
{
Super::Tick(DeltaTime); // Call the base class (AActor) Tick function
// Add your custom tick code here.
// This might include movement updates, gameplay logic, etc., specific to this actor.
}
By calling Super::Tick(DeltaTime);:
You ensure that all the ticking behavior defined in AActor and its hierarchy (if AMyActor is not directly derived from AActor but from a class that itself derives from AActor) is executed. This can include important engine-level updates, component updates, and more, which your AMyActor might indirectly rely on.
After the Super::Tick call, you can safely add your additional tick-related logic, knowing that the actor is in a stable state with all standard Unreal Engine updates applied for that tick.
Conclusion
Calling the base class's implementation of a method, such as Super::Tick, is a best practice whenever you override a method in Unreal Engine (or in general C++ programming), unless explicitly intended not to. This ensures that all intended functionality from the base class is preserved, allowing you to extend or customize behavior safely. Unreal Engine relies on these patterns heavily, given its architecture and the way it's designed to allow developers to build modular and extensible gameplay systems.
As of my last update in 2023, UGameInstance in Unreal Engine does not inherently have a Tick function like AActor and some other classes do. This is because UGameInstance is designed to persist across different levels or maps in your game, serving as a container for data and functions that should not be reloaded or reset between level loads. Its purpose is more about persisting information and providing functionality across the game session, rather than performing per-frame updates tied to the game world.
Why UGameInstance Doesn't Have a Tick Function
Scope and Purpose: The primary role of UGameInstance does not align with the need for frame-by-frame updates. It's more about session-wide state management rather than real-time updates.
Performance: Not having a built-in tick mechanism avoids unnecessary performance overhead for objects that do not need to update every frame.
Implementing Periodic Updates in UGameInstance
If you find yourself needing to perform periodic or time-based updates within a UGameInstance, there are several alternative approaches you might consider:
Timers: Unreal Engine's FTimerManager allows you to easily schedule repetitive or delayed tasks. This can be accessed in UGameInstance through GetWorld()->GetTimerManager().SetTimer() if you have access to a world context.
Custom Tick System: You can implement your own ticking system by overriding functions that are regularly called (e.g., by checking in certain overridden functions that are expected to be called frequently due to game activity).
Tick in Another Class: Often, the need to tick can be delegated to other classes more suited to regular updates, like AActor derivatives or UActorComponent derivatives, which inherently support ticking. This design keeps the UGameInstance focused on session management while leveraging Unreal's architecture for game object updates.
Event Dispatchers / Delegates: For changes that aren't strictly time-dependent but react to game events, using Unreal's event system (through event dispatchers or delegates) can be an effective way to respond to changes without needing a tick.
Example of Using a Timer in UGameInstance
cpp
// In your UGameInstance subclass header
FTimerHandle CustomTimerHandle;
// In your UGameInstance subclass source file, perhaps in an initialization function
GetWorld()->GetTimerManager().SetTimer(CustomTimerHandle, this, &UMyGameInstanceSubclass::CustomTimerFunction, 1.0f, true);
// Implement CustomTimerFunction in your UGameInstance subclass
void UMyGameInstanceSubclass::CustomTimerFunction()
{
// This function will be called every second
}
Note: When dealing with UGameInstance and timers or custom tick systems, ensure you properly clean up or invalidate your timers or custom systems when the game instance is being destroyed or when they are no longer needed, to avoid any potential issues or leaks.
In summary, while UGameInstance itself does not tick, Unreal Engine provides flexible systems to achieve similar outcomes tailored to the unique needs of your game's architecture and performance considerations.