DEV Community

Iqram
Iqram

Posted on

Game Mechanics

Game mechanics in Unreal Engine involve various systems and components that define how a game operates and responds to player input. Here's an overview of some key game mechanics and how to implement them in Unreal Engine:

1. Character Movement

Basic Movement:

  • Use the Character class, which comes with built-in movement capabilities.
  • Set up input bindings in the project's settings to map player inputs to character movements.
cpp code
// In Character class (e.g., MyCharacter.cpp)
void AMyCharacter::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
{
    Super::SetupPlayerInputComponent(PlayerInputComponent);

    // Bind movement functions
    PlayerInputComponent->BindAxis("MoveForward", this, &AMyCharacter::MoveForward);
    PlayerInputComponent->BindAxis("MoveRight", this, &AMyCharacter::MoveRight);
}

void AMyCharacter::MoveForward(float Value)
{
    AddMovementInput(GetActorForwardVector() * Value);
}

void AMyCharacter::MoveRight(float Value)
{
    AddMovementInput(GetActorRightVector() * Value);
}

Enter fullscreen mode Exit fullscreen mode

2. Health System

Basic Health System:

  • Create variables for health and implement damage and healing functions.
cpp code
// In Character class (e.g., MyCharacter.h)
public:
    float Health;
    float MaxHealth;

    void TakeDamage(float DamageAmount);
    void Heal(float HealAmount);

// In Character class (e.g., MyCharacter.cpp)
AMyCharacter::AMyCharacter()
{
    Health = 100.0f;
    MaxHealth = 100.0f;
}

void AMyCharacter::TakeDamage(float DamageAmount)
{
    Health -= DamageAmount;
    if (Health <= 0.0f)
    {
        Health = 0.0f;
        // Handle character death
    }
}

void AMyCharacter::Heal(float HealAmount)
{
    Health += HealAmount;
    if (Health > MaxHealth)
    {
        Health = MaxHealth;
    }
}

Enter fullscreen mode Exit fullscreen mode

3. Inventory System

Basic Inventory System:

  • Use an array or a list to store inventory items.
cpp code
// In Character class (e.g., MyCharacter.h)
public:
    TArray<FString> Inventory;

    void AddItemToInventory(FString ItemName);
    void RemoveItemFromInventory(FString ItemName);

// In Character class (e.g., MyCharacter.cpp)
void AMyCharacter::AddItemToInventory(FString ItemName)
{
    Inventory.Add(ItemName);
}

void AMyCharacter::RemoveItemFromInventory(FString ItemName)
{
    Inventory.Remove(ItemName);
}

Enter fullscreen mode Exit fullscreen mode

4. Combat System

Basic Combat System:

  • Implement functions for attacking and handling enemy interaction.
cpp code
// In Character class (e.g., MyCharacter.h)
public:
    float AttackDamage;

    void Attack();

// In Character class (e.g., MyCharacter.cpp)
AMyCharacter::AMyCharacter()
{
    AttackDamage = 20.0f;
}

void AMyCharacter::Attack()
{
    // Perform a line trace or overlap to detect enemies
    FHitResult HitResult;
    FVector Start = GetActorLocation();
    FVector End = Start + (GetActorForwardVector() * 100.0f);

    FCollisionQueryParams CollisionParams;
    CollisionParams.AddIgnoredActor(this);

    if (GetWorld()->LineTraceSingleByChannel(HitResult, Start, End, ECC_Visibility, CollisionParams))
    {
        AActor* HitActor = HitResult.GetActor();
        if (HitActor)
        {
            // Apply damage to the hit actor
            UGameplayStatics::ApplyDamage(HitActor, AttackDamage, GetController(), this, UDamageType::StaticClass());
        }
    }
}

Enter fullscreen mode Exit fullscreen mode

5. Save/Load System

Basic Save/Load System:

  • Use Unreal Engine's SaveGame class to implement save and load functionality.
cpp code
// In custom SaveGame class (e.g., MySaveGame.h)
UCLASS()
class MYGAME_API UMySaveGame : public USaveGame
{
    GENERATED_BODY()

public:
    UPROPERTY(VisibleAnywhere, Category = Basic)
    float PlayerHealth;

    UPROPERTY(VisibleAnywhere, Category = Basic)
    FVector PlayerLocation;
};

// In Character class (e.g., MyCharacter.cpp)
void AMyCharacter::SaveGame()
{
    UMySaveGame* SaveGameInstance = Cast<UMySaveGame>(UGameplayStatics::CreateSaveGameObject(UMySaveGame::StaticClass()));
    SaveGameInstance->PlayerHealth = Health;
    SaveGameInstance->PlayerLocation = GetActorLocation();

    UGameplayStatics::SaveGameToSlot(SaveGameInstance, "PlayerSaveSlot", 0);
}

void AMyCharacter::LoadGame()
{
    UMySaveGame* LoadGameInstance = Cast<UMySaveGame>(UGameplayStatics::LoadGameFromSlot("PlayerSaveSlot", 0));
    if (LoadGameInstance)
    {
        Health = LoadGameInstance->PlayerHealth;
        SetActorLocation(LoadGameInstance->PlayerLocation);
    }
}

Enter fullscreen mode Exit fullscreen mode

Additional Resources

  • Unreal Engine Documentation: Detailed guides and references for various Unreal Engine systems and features.
  • Unreal Engine Forums and Community: Engage with other developers, ask questions, and share knowledge.
  • Unreal Engine Marketplace: Find assets, plugins, and tools to enhance your projects.

Here you can read my Journal
Notion : https://www.notion.so/Unreal-engine-game-dev-61550dd61f0d41948863ac7e802296f8?pvs=4

Top comments (0)