pat-lawre
pat-lawre

Reputation: 107

TSubclassOf<> isn't storing the class type

I'm trying to create a laser beam actor that spawns when the player presses the "Fire" action mapping. I can't get TSubclassOf<> to work so that I can spawn the actor that creates the laser. I can declare the variable and compile the project, but I can't seem to use the template anywhere else in the project because the variable isn't initialized??? I'll include my code to help explain what's happening.

Here is my LaserTagCharacter.h file. I've declared the LaserClass variable and I can compile this code with no problems.

#pragma once

#include "CoreMinimal.h"
#include "GameFramework/Character.h"
#include "LaserTagCharacter.generated.h"

UCLASS()
class LASERTAG_API ALaserTagCharacter : public ACharacter
{
    GENERATED_BODY()

public:
    // Sets default values
    ALaserTagCharacter();

    UPROPERTY()
    TSubclassOf<class ALaserTagLaser> LaserClass;

protected:
    // Called when player fires a laser beam
    void OnFire();
};

Moving over to the LaserTagCharacter.cpp file, I've included some debug code and my main objective with creating the template class. I created the template class to use it in a SpawnActor function that will spawn an instance of LaserTagLaser.

#include "LaserTagCharacter.h"
#include "Camera/CameraComponent.h"
#include "Components/CapsuleComponent.h"
#include "Components/InputComponent.h"
#include "LaserTagLaser.h"
#include "Engine/World.h"

// Sets default values
ALaserTagCharacter::ALaserTagCharacter()
{

}

// Called when player uses fire action binding
void ALaserTagCharacter::OnFire()
{
    UWorld* World = GetWorld();
    FVector SpawnLocation = GetActorLocation();
    FRotator SpawnRotation = GetControlRotation();

    if (LaserClass)
    {
        UE_LOG(LogTemp, Warning, TEXT("Yes"))
    }
    else
    {
        UE_LOG(LogTemp, Warning, TEXT("No"))
    }

    World->SpawnActor<ALaserTagLaser>(LaserClass, SpawnLocation, SpawnRotation);
}

The 2 messages I get from the output log when I compile, play, and press the "Fire" action mapping are:

LogTemp: Warning: No

and...

LogSpawn: Warning: SpawnActor failed because no class was specified

If you have any insight on how I can fix this and get TSubclassOf<> to work properly, that would be awesome!

Upvotes: 1

Views: 6027

Answers (1)

Powelo
Powelo

Reputation: 121

Your code seems ok, but you should add in the header file a specifier inside UPROPERTY, and maybe a Category just give it a title

UPROPERTY(EditDefaultsOnly, Category = "Setup")
TSubclassOf<class ALaserTagLaser> LaserClass;

above the TSubclassOf so that UPROPERTY will be editable in the blueprint editor inside unreal as a drop-menu. Then you must a create a new blueprint based on the ALaserTagLaser. After that, open the drop-menu and you will see only classes and childs of classes of the ALaserTagLaser. Select the blueprint you created, click the compile button inside the blueprintEditor, and then it should be working...

Upvotes: 2

Related Questions