Hey guys!
I’m trying to grant a character the ability to make spells.
To do this, I’m using an HUD I created beforehand. Here’s how it goes :
- I pick up an item that contains the spell.
- The item is saved in my inventory.
- I open my inventory, and the items in it are shown.
- I right-click on the item of the spell, which casts the spell.
For now, I crash at step 4, the other stuff seems to work.
The relevant classes here would be Spell.cpp and Character.cpp.
Spell.h
UCLASS()
class GOLDENEGGREDUX_API ASpell : public AActor
{
GENERATED_BODY()
public:
ASpell(const class FObjectInitializer& ObjectInitializer);
// Sets default values for this actor's properties
ASpell();
//Box defining the area of damage
UPROPERTY(VisibleDefaultsOnly, BlueprintReadOnly, Category = Spell)
TSubobjectPtr<UBoxComponent> ProxBox;
//The particle visualization
UPROPERTY(VisibleDefaultsOnly, BlueprintReadOnly, Category = Spell)
TSubobjectPtr<UParticleSystemComponent> Particles;
//The lizard wizard
AActor* Caster;
//links the spell and its caster
void SetCaster(AActor* caster);
// Called when the game starts or when spawned
virtual void BeginPlay() override;
// Called every frame
virtual void Tick( float DeltaSeconds ) override;
};
Spell.cpp
// Fill out your copyright notice in the Description page of Project Settings.
#include "GoldenEggRedux.h"
#include "Enemy.h"
#include "Spell.h"
// Sets default values
ASpell::ASpell(const class FObjectInitializer& ObjectInitializer) : Super(ObjectInitializer)
{
ProxBox = ObjectInitializer.CreateDefaultSubobject<UBoxComponent>(this, TEXT("ProxBox"));
Particles = ObjectInitializer.CreateDefaultSubobject<UParticleSystemComponent>(this, TEXT("ParticleSystem"));
RootComponent = Particles;
ProxBox->AttachTo(RootComponent);
PrimaryActorTick.bCanEverTick = true;
}
// Called when the game starts or when spawned
void ASpell::BeginPlay()
{
Super::BeginPlay();
}
// Called every frame
void ASpell::Tick( float DeltaTime )
{
Super::Tick( DeltaTime );
}
void ASpell::SetCaster(AActor* caster)
{
Caster = caster;
AttachRootComponentTo(caster->GetRootComponent());
}
Character.h
UCLASS()
class GOLDENEGGREDUX_API ATorcheOlympique : public ACharacter
{
GENERATED_BODY()
public:
// Sets default values for this character's properties
ATorcheOlympique();
//Map to remember spells learned
TMap<FString, UClass*> Spells;
void Pickup(APickupItem *item);
void ToggleInventory();
void MouseRightClicked();
void CastSpell(UClass* bpSpell);
// Called when the game starts or when spawned
virtual void BeginPlay() override;
// Called every frame
virtual void Tick( float DeltaSeconds ) override;
// Called to bind functionality to input
virtual void SetupPlayerInputComponent(class UInputComponent* InputComponent) override;
};
Character.cpp
ACharacter::ACharacter()
{
// Set this character to call Tick() every frame. You can turn this off to improve performance if you don't need it.
PrimaryActorTick.bCanEverTick = true;
}
// Called when the game starts or when spawned
void ATCharacter::BeginPlay()
{
Super::BeginPlay();
}
void ACharacter::Pickup(APickupItem* item)
{
if (Inventory.Find(item->name))
{
Inventory[item->name] += item->qty;//If its there, get more
}
else
{
Inventory.Add(item->name, item->qty);//it wasnt there, get one
Images.Add(item->name, item->icon);
Spells.Add(item->name, item->Spell);
}
}
void ACharacter::MouseRightClicked()
{
if (inventoryVisible)
{
APlayerController* PController = GetWorld()->GetFirstPlayerController();
AMuhHUD* hud = Cast<AMuhHUD>(PController->GetHUD());
hud->MouseRightClicked();
}
}
//Used to cast spell
void ACharacter::CastSpell(UClass* bpSpell)
{
//instantiate the spell and attach to character
ASpell *spell = GetWorld()->SpawnActor<ASpell>(bpSpell, FVector(0), FRotator(0));
if (spell)
{
spell->SetCaster(this);
}
}
// Called to bind functionality to input
void ATorcheOlympique::SetupPlayerInputComponent(class UInputComponent* InputComponent)
{
Super::SetupPlayerInputComponent(InputComponent);
InputComponent->BindAction("Inventory", IE_Pressed, this, &ATorcheOlympique::ToggleInventory);
InputComponent->BindAction("MouseClickedRMB", IE_Pressed, this, &ATorcheOlympique::MouseRightClicked);
}
I think that’s it. The HUD class isn’t there because it seems to be working well. Basically, when I right-click the icon of the spell on my HUD, the CastSpell function of my character runs. CastSpell is supposed to spawn and attach the spell (ParticleSystem + ProxBox) to the character. However UE then crashes.
The log is quite strange as well. I’m used to my crashes originating from one of my classes, but it isn’t quite so clear here, although it’s probably because I have no idea what I’m doing as far as crash logs go. Here it is. In my project, Character.cpp is TorcheOlympique.cpp,
Anyone got an idea what’s causing those troubles? Thanks in advance!