x

Search in
Sort by:

Question Status:

Search help

  • Simple searches use one or more words. Separate the words with spaces (cat dog) to search cat,dog or both. Separate the words with plus signs (cat +dog) to search for items that may contain cat but must contain dog.
  • You can further refine your search on the search results page, where you can search by keywords, author, topic. These can be combined with each other. Examples
    • cat dog --matches anything with cat,dog or both
    • cat +dog --searches for cat +dog where dog is a mandatory term
    • cat -dog -- searches for cat excluding any result containing dog
    • [cats] —will restrict your search to results with topic named "cats"
    • [cats] [dogs] —will restrict your search to results with both topics, "cats", and "dogs"

Spawning and attaching an actor to the player character

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 : 1. I pick up an item that contains the spell. 2. The item is saved in my inventory. 3. I open my inventory, and the items in it are shown. 4. 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,

link text

Anyone got an idea what's causing those troubles? Thanks in advance!

Product Version: UE 4.7
Tags:
crashlog.txt (33.3 kB)
more ▼

asked Jun 06 '15 at 04:59 PM in C++ Programming

avatar image

Xylord
41 9 14 16

(comments are locked)
10|2000 characters needed characters left
Viewable by all users

0 answers: sort voted first
Be the first one to answer this question
toggle preview:

Up to 5 attachments (including images) can be used with a maximum of 5.2 MB each and 5.2 MB total.

Follow this question

Once you sign in you will be able to subscribe for any updates here

Answers to this question