Add Weapon Inventory Component

This commit is contained in:
baz 2024-07-18 01:39:58 +01:00
parent d185f032bb
commit f25e6366f5
2 changed files with 87 additions and 0 deletions

View File

@ -0,0 +1,46 @@
// Fill out your copyright notice in the Description page of Project Settings.
#include "WeaponInventoryComponent.h"
#include "Kismet/GameplayStatics.h"
// Sets default values for this component's properties
UWeaponInventoryComponent::UWeaponInventoryComponent()
{
// Set this component to be initialized when the game starts, and to be ticked every frame. You can turn these features
// off to improve performance if you don't need them.
PrimaryComponentTick.bCanEverTick = false;
// ...
}
// Called when the game starts
void UWeaponInventoryComponent::BeginPlay()
{
Super::BeginPlay();
// ...
}
void UWeaponInventoryComponent::InitializeInventory()
{
inventory.Empty();
for (TSubclassOf<AWeapon> weapon : initialInventory)
{
AddWeaponToInventory(weapon);
}
}
void UWeaponInventoryComponent::AddWeaponToInventory(TSubclassOf<AWeapon> Weapon)
{
AWeapon* weapon = GetWorld()->SpawnActor<AWeapon>(Weapon);
weapon->SetActorTransform(GetOwner()->GetTransform());
weapon->SetOwner(GetOwner());
weapon->AttachToActor(GetOwner(), FAttachmentTransformRules::KeepRelativeTransform);
inventory.Add(weapon);
}

View File

@ -0,0 +1,41 @@
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "CoreMinimal.h"
#include "Weapon.h"
#include "Components/ActorComponent.h"
#include "WeaponInventoryComponent.generated.h"
UCLASS( ClassGroup=(Custom), meta=(BlueprintSpawnableComponent) )
class VAMPIRES_API UWeaponInventoryComponent : public UActorComponent
{
GENERATED_BODY()
public:
UPROPERTY()
TArray<TSubclassOf<AWeapon>> initialInventory;
private:
UPROPERTY()
TArray<AWeapon*> inventory;
public:
// Sets default values for this component's properties
UWeaponInventoryComponent();
protected:
// Called when the game starts
virtual void BeginPlay() override;
public:
UFUNCTION()
void InitializeInventory();
UFUNCTION()
void AddWeaponToInventory(TSubclassOf<AWeapon> Weapon);
};