GetWorld()->SpawnActor crashes Editor when i click Play button

My problem is the following:

I had one class wich was the base class for my character blueprint, but i wanted to add an enemy AI to the game wich had some behavior of the main character. So i created two new classes (EnemyCharacter and PlaybleCharacter) that inherit from a GeneralCharacter class.
I believe I have changed everything that referenced my previous player class to the new one.

  • The problem:
    On BeginPlay() function overwritten on my GenericCharacter Class i call a function to give a default weapon to my character. I copied the exact code of this function to the new class, wich was working before, but now it crashes when i hit the play button on the engine with the following error:

As you can see it points to line 232. Heres is the rest of the code wich may cause conflict, although it worked previously:

  • GenericCharacter.h

    #include “GameFramework/Character.h”
    #include “Weapon.h”
    #include “GenericCharacter.generated.h”

    (…)

    /** Weapon Spawned on character */
    UPROPERTY(EditDefaultsOnly, Category = “Default Inventory”)
    TSubclassOf DefaultWeapon;

     /** Current Weapon on Character */
     UPROPERTY(VisibleAnywhere, Category = Weapon)
     	AWeapon *CurrentWeapon;
    
     /** Create Inventory Array with Weapons */
     UPROPERTY(VisibleAnywhere, Category = Inventory)
     	TArray<class AWeapon*> Inventory;
    
  • GenericCharacter.cpp

    #include “WeaponTest.h”
    #include “GenericCharacter.h”
    #include “Pistol.h”
    #include “Shotgun.h”
    #include “RocketLauncher.h”
    #include “Engine/Blueprint.h”
    #include “Engine.h”

    AGenericCharacter::AGenericCharacter()
    {

    }

    // Sets default values
    AGenericCharacter::AGenericCharacter(const FObjectInitializer& ObjectInitializer)
    : Super(ObjectInitializer)
    {
    // 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;

     // Set size of Inventory Array
     Inventory.SetNum(4, false);
    
     // Set size for collision capsule
     GetCapsuleComponent()->InitCapsuleSize(42.f, 96.0f);
    
     // Configure default character state and movement
     this->bDisableFiring = true;
     this->bIsAiming = false;
     this->bIsFiring = false;
     this->bIsSprinting = false;
     this->bIsReloading = false;
     this->SprintSpeed = 700.0f; // in centimeters / second
     this->JogSpeed = 450.0f; // in centimeters / second
     this->AimSpeed = 200.0f; // in centimeters / second
     this->GetCharacterMovement()->MaxWalkSpeed = this->JogSpeed;
     this->GetCharacterMovement()->bOrientRotationToMovement = true;
     this->GetCharacterMovement()->RotationRate = FRotator(0.0f, 540.0f, 0.0f); // The character rotates at this rotation rate
     this->GetCharacterMovement()->JumpZVelocity = 350.0f;
     this->GetCharacterMovement()->AirControl = 0.2f; // 20% of the total control
    
     CollisionComp = ObjectInitializer.CreateDefaultSubobject<UBoxComponent>(this, "CollisionComp");
     CollisionComp->AttachTo(RootComponent);
     CollisionComp->OnComponentBeginOverlap.AddDynamic(this, &AGenericCharacter::OnCollision);
    

    }

     // Called when the game starts or when spawned
     void AGenericCharacter::BeginPlay()
     {
     	Super::BeginPlay();
     	
     	this->GetCharacterMovement()->MaxWalkSpeed = this->JogSpeed;
     
     	GiveDefaultWeapon();
     }
    
     void AGenericCharacter::GiveDefaultWeapon()
     {
     	AWeapon *Spawner = GetWorld()->SpawnActor<AWeapon>(this->DefaultWeapon->GetClass());
     	
     	if (Spawner)
     	{
     		Inventory[Spawner->WeaponConfig.Slot] = Spawner;
     		CurrentWeapon = Inventory[Spawner->WeaponConfig.Slot];
     		CurrentWeapon->SetOwningPawn(this);
     		CurrentWeapon->OnEquip();
     	}
     }
    

Could the problem be the fact that i’m spawning the weapon on my base class (GenericCharacter) instead of the Playable character class wich is the base for my character blueprint? Any help would be realy apreciated, thanks :slight_smile:

This was the only way i could spawn a actor with out a crash.

			Level.Add(GetWorld()->SpawnActor< ABuildingModLevel >());

I’m not an expert in coding inside C++ but shouldn’t you use
GetWorld()->SpawnActor(); or GetWorld->SpawnActor(AWeapon::StaticClass);
Or maybe your this->GetDefaultWeapon() returns a nullptr have checked that already?

I don’t know how to check it because it crashes the editor, tryed writing to the log but it doesn’t write anything… And it used to work before when I had just one class for the character so I believe you can spawn it the way I did. I’m kind of getting bored of trying to solve it, I think I’m going to create an empty project and recreate everything… But thanks anyway :slight_smile:

Do you remember which engine version you got this working on?

The latest version at the time of this post, most likely 4.7

Don’t give up man, look you can assign a var to that this->GetDefaultWeapon() and place a breakpoint just after it to check it’s value, just run DebugGameEditor on VS and execute the action to spawn the actor, this function also gives me a lot of headaches.

This is quite old. I eventually fixed it, don’t remember how though…