How to access CPP parent of Blueprint Actor Class?

I’ve been using UE5 with Blueprints for about a year now, and have just started a new project using C++. I’m pretty new to C++ (I come from Unity) and I have a question.

I have a class “CPP_Bullet” which is a C++ actor class and the parent to BP_Bullet. This line spawns the BP_Bullet actor into the world:

AActor* spawnedBUllet = GetWorld()->SpawnActor<AActor>(bulletActor, spawnTransform);

From that actor I need to be able to access the CPP Class in order to set properties of the bullet. Is there anyway I can get/cast the CPP class from the Actor, or an alternative I should be using instead?

Assuming bulletActor is a UClass* to your BP_Bullet class (or at least a child of CPP_Bullet), you can just change it to be.

ACPP_Bullet* spawnedBullet = GetWorld()->SpawnActor<ACPP_Bullet>(bulletActor, spawnTransform);

You could also simply cast it, but that isn’t necessary in this case.

1 Like

Thank you this is very helpful and the code is now compiling! However, the game now crashes Unreal when trying to create the bullet.

The decloration for bulletActor is below, and is set in editor to BP_Bullet which is a child of CPP_Bullet

	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Weapon Properties")
		// The Bullet actor to be fired
		UClass* bulletActor;

I think that’s unrelated to the spawning. Something in the bullet is likely causing a crash. Try disabling anything that happens on creation (ie the rest of the spawning function, constructors, begin play, and overlap events).

If none of that is the problem, launch the editor in debug mode via your IDE. I use Rider, so I’m not sure how good the debug mode on alternatives are, but it’ll probably help.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.