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"

TSubClassOf is NULL

I tried to follow the Shooter example's bullet spawning method. I create a c++ class for the weapon. The header contains the TSubClassof fot the spawn event public:

     UPROPERTY(EditDefaultsOnly, Category = Projectile)
     TSubclassOf<class AW01Projectile> ProjectileClass;

The blueprint ProjectileClass is setted to BP_AW01Projectile. When I use ProjectileClass it says the class is NULL

Any solution?

Product Version: UE 4.8
more ▼

asked Jul 13 '15 at 04:48 PM in C++ Programming

avatar image

16 3 10 12

avatar image Shadowriver Jul 13 '15 at 04:52 PM

You sure you setting ProjectileClass in right object? Where do you call it?

avatar image [Shai] Jul 13 '15 at 05:10 PM

I call it from the weapon class W01Weapon.cpp

avatar image Janyx Jul 13 '15 at 05:20 PM

You'll need to show how you are calling it. I am thinking that is where the mistake is.

avatar image [Shai] Jul 13 '15 at 07:01 PM

It doesn't mind how I call.. I add a function to check the state of the projectile class and it return false while GameplayStatics::BeginSpawningActorFromClassgive me warning log saying the class is NULL

 bool FPSCharacter::GetProjectile()
        if (ProjectileClass)
            return true;
            return false;

avatar image Shadowriver Jul 13 '15 at 07:38 PM

Ok then where you set it?

avatar image [Shai] Jul 13 '15 at 08:24 PM

BP_W01Weapon, ProjectileClass box

avatar image IDON_EAT_DOGS Jul 14 '15 at 03:32 PM

posting your code would be nice

avatar image [Shai] Jul 15 '15 at 12:59 PM


 #pragma once
 #include "GameFramework/Actor.h"
 #include "W01Weapon.generated.h"
 class W01_API AW01Weapon : public AActor
     virtual void PostInitializeComponents() override;
     UPROPERTY(EditDefaultsOnly, Category = Projectile)
     TSubclassOf<class AJanoProjectile> ProjectileClass;

 void SetOwningPawn(AW01Character* AW01Character);
     UPROPERTY(VisibleDefaultsOnly, Category = Mesh)
     USkeletalMeshComponent* Mesh1P;


 UPROPERTY(Transient, ReplicatedUsing = OnRep_MyPawn)
 class AW01Character* MyPawn;

     UPROPERTY(Transient, Replicated)
     int32 CurrentAmmo;
     int32 MaxAmmo;

 void OnRep_MyPawn();

     bool GetProjectile();

avatar image [Shai] Jul 15 '15 at 01:03 PM


 #include "W01.h"
 #include "W01Weapon.h"
 #include "UnrealNetwork.h"
 #include "W01Character.h"
 #include "Engine.h"
 #include "W01Projectile.h"
 AW01Weapon::AW01Weapon(const FObjectInitializer& ObjectInitializer)
     Mesh1P = CreateDefaultSubobject<USkeletalMeshComponent>(TEXT("WeaponMesh1P"));
     Mesh1P->MeshComponentUpdateFlag = EMeshComponentUpdateFlag::OnlyTickPoseWhenRendered;
     Mesh1P->bChartDistanceFactor = false;
     Mesh1P->bReceivesDecals = false;
     Mesh1P->CastShadow = false;
     RootComponent = Mesh1P;
     CurrentAmmo = 0;
     PrimaryActorTick.bCanEverTick = true;
     PrimaryActorTick.TickGroup = TG_PrePhysics;
     bReplicates = true;
     bNetUseOwnerRelevancy = true;
 void AW01Weapon::PostInitializeComponents()
     MaxAmmo = 100;
     CurrentAmmo = 40;

avatar image [Shai] Jul 15 '15 at 01:03 PM
 void AW01Weapon::GetLifetimeReplicatedProps(TArray< FLifetimeProperty > & OutLifetimeProps) const
     DOREPLIFETIME(AW01Weapon, MyPawn);
     DOREPLIFETIME_CONDITION(AW01Weapon, CurrentAmmo, COND_OwnerOnly);
 void AW01Weapon::OnRep_MyPawn()
     if (MyPawn)
     void AW01Weapon::OnEnterInventory(AW01Character* NewOwner)
 void AW01Weapon::SetOwningPawn(AW01Character* NewOwner)
     if (MyPawn != NewOwner)
         Instigator = NewOwner;
         MyPawn = NewOwner;
 bool AW01Weapon::GetProjectile()
         if (ProjectileClass)
             return true;
             return false;
avatar image Shadowriver Jul 15 '15 at 01:58 PM

Eveything seems ok to me if you set class in defaults, so only possible things is you refrenceing to UClass in wrong place, so where do you call GetProjectile()?

avatar image [Shai] Jul 15 '15 at 06:16 PM

In the Character.cpp I use

 void AW01Character::SpawnDefaultWeapon()
     if (Role < ROLE_Authority)
     FActorSpawnParameters SpawnInfo;
     SpawnInfo.bNoCollisionFail = true;
     AW01Weapon* NewWeapon = GetWorld()->SpawnActor<AW01Weapon>(SpawnInfo);
avatar image IDON_EAT_DOGS Jul 15 '15 at 02:38 PM

try using normal pointer instead of TSubClassOf ans see if it works. My guess would be type error

avatar image IDON_EAT_DOGS Jul 15 '15 at 02:43 PM

also, in your weapon class you inherited from AActor, so try changing GENERATED_UCLASS_BODY() to GENERATED_BODY()

avatar image Shadowriver Jul 15 '15 at 02:48 PM

I still using GENERATED_UCLASS_BODY() and didn't have issue UClass'es

(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