Equip function parameter is null...why?

Hello!

I am trying to setup a basic function to equip a sword (Skeletal Mesh) to my character. I have setup all the code in c++ and have the function call in blueprints (for now). My parameter (AWeaponBase* WeaponToEquip) returns a nullptr and I’m not sure why.

I’m trying to call the function from my main character, HeroBase.cpp and my WeaponBase class is pretty basic. I’ve only created a Mesh/BoxComponent.

HeroBase.cpp

void AHeroBase::EquipWeapon(AWeaponBase* WeaponToEquip)
{
	if (!WeaponToEquip)
	{
		if (GEngine)
		{
			GEngine->AddOnScreenDebugMessage(
				-1,
				15.f,
				FColor::Red,
				FString::Printf(TEXT("WEAPON NOT EQUIPPED!!"))
			);
		}
		return;
	}

	const USkeletalMeshSocket* WeaponSocket = GetMesh()->GetSocketByName(FName("WeaponSocket_r"));
	
	if (WeaponSocket)
	{
		WeaponSocket->AttachActor(WeaponToEquip, GetMesh());
		if (GEngine)
		{
			GEngine->AddOnScreenDebugMessage(
				-1,
				15.f,
				FColor::Green,
				FString::Printf(TEXT("WEAPON EQUIPPED!!"))
			);
		}
	}
}

WeaponBase.h

UCLASS()
class OATHBOUND_API AWeaponBase : public AActor
{
	GENERATED_BODY()
	
public:	
	AWeaponBase();
	virtual void Tick(float DeltaTime) override;
	
protected:
	virtual void BeginPlay() override;
	virtual void PostInitializeComponents() override;

private:
	/*
	 *	Weapon Components
	 */
	UPROPERTY(VisibleAnywhere, Category = "Components")
	USkeletalMeshComponent* WeaponMesh;

	UPROPERTY(VisibleAnywhere, Category = "Components")
	UBoxComponent* WeaponCollision;

public:
	UFUNCTION()
	void WeaponCollisionOnBeginOverlap(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult);
	UFUNCTION()
	void WeaponCollisionOnEndOverlap(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex);
};

HeroBase BP call:

If there is anything I’m doing wrong or need to add, please let me know!
I apologize if this is something that all beginners should know, but I’ve been stuck for awhile now.

Thank you in advance!!

1 Like

Most likely the Weapon to Equip variable in your BP isn’t being set- if it’s unset in the blueprint it’ll be nullptr in C++. Perhaps you are forgetting to set it? Or the conditions required to set it aren’t being met.

By the way, you can check if something is nullptr in BPs by using the IsValid nodes. :wink:

1 Like

From the blueprints, I created a variable that’s an object reference to my AWeaponBase class. I don’t understand why it’s not referencing the class because it is a direct reference to it.

I did use the IsValid node, but it gives me the same output as I have in my equip function conditional.

I don’t know what else to do.

Okay okay I see.

A class reference is different from an object reference. For a class reference, you would set it to point to the UClass* object generated to represent your AWeaponBase class. You can change between class references and object references in the BP variable menu. Object references are blue and class references are purple in BP.

Right now, you are using an object reference in C++ and BP, and you’ve set the type to AWeaponBase.

You need to change your C++ function to take a Class reference and not an Object reference. You’ll need to use TSubclassOf<AWeaponBase> as the type. Then, you’ll first need to spawn an Actor of that class, then you can AttachActor.

What you’re doing is totally possible, but it is a little tricky to deal with classes/class references. There should be plenty of good threads on the forums to help.

This should help too:

Also, you’ll probably need to use this:

It takes a UClass* class reference as a parameter to spawn the actor.

This has been very insightful! Thank you for your help!

1 Like