Chapter 2 - Converting the BeginPlay hook into C++ script
- The Event BeginPlay in blueprint/visual scritp
This BeginPlay event for the Bot Player Blueprint does the following:
- Sequence Node: Splits execution into three parallel branches.
- SetInputMappingContext: Calls a collapsed graph to set up input mappings.
- InitFireMode: Calls a collapsed graph to initialize the fire mode.
- CreateCrosshairWidget: Calls a collapsed graph to create the crosshair UI.
- SetTag: Sets a tag on the actor (likely for identification).
- Get All Actors of Class (BP_Enemy_Spawn): Finds all enemy spawn actors in the level.
- Bind Event to On All Enemies Defeated: Binds a custom event (Event_ShowLevelClearedMessage) to an event dispatcher on the enemy spawner(s).
- Event_ShowLevelClearedMessage: Custom event that, when triggered, calls ShowLevelClearedMessage on self, passing a message to display.
How to Convert This to C++
Below is a C++ outline for your BeginPlay
logic. You’ll need to implement or stub out the collapsed graphs as C++ functions.
#include "BotPlayer.h" #include "Kismet/GameplayStatics.h" #include "BP_Enemy_Spawn.h" // Replace with your actual enemy spawn class header void ABotPlayer::BeginPlay() { Super::BeginPlay(); // 1. Set up input mapping context SetInputMappingContext(); // 2. Initialize fire mode InitFireMode(); // 3. Create crosshair widget CreateCrosshairWidget(); // 4. Set tag on this actor this->Tags.Add(FName("PlayerTag")); // Replace "PlayerTag" with the actual tag // 5. Get all enemy spawners in the level TArray<AActor*> EnemySpawners; UGameplayStatics::GetAllActorsOfClass(GetWorld(), ABP_Enemy_Spawn::StaticClass(), EnemySpawners); // 6. Bind event to OnAllEnemiesDefeated for each spawner for (AActor* SpawnerActor : EnemySpawners) { ABP_Enemy_Spawn* Spawner = Cast<ABP_Enemy_Spawn>(SpawnerActor); if (Spawner) { // Assuming the spawner has a delegate like: // DECLARE_DYNAMIC_MULTICAST_DELEGATE(FOnAllEnemiesDefeated); // UPROPERTY(BlueprintAssignable) FOnAllEnemiesDefeated OnAllEnemiesDefeated; Spawner->OnAllEnemiesDefeated.AddDynamic(this, &ABotPlayer::ShowLevelClearedMessage); } } } // Example stub implementations for the collapsed graphs: void ABotPlayer::SetInputMappingContext() { // Implement input mapping setup here } void ABotPlayer::InitFireMode() { // Implement fire mode initialization here } void ABotPlayer::CreateCrosshairWidget() { // Implement crosshair widget creation here } // This function will be called when all enemies are defeated void ABotPlayer::ShowLevelClearedMessage() { // Implement logic to show the level cleared message }
Key Points:
- Replace
"YourTag"
with the actual tag you want to set. - Replace
ABP_Enemy_Spawn
with your actual enemy spawner C++ class. - The delegate/event system assumes your enemy spawner has a
BlueprintAssignable
multicast delegate for "all enemies defeated." - Implement the collapsed graph logic as C++ member functions.
Next is to convert each variable, function, node graph or macro into C++ code.
For example in the blueprint we have the "CreateCrosshairWidget" that creates the target/crosshair widget in the center of the screen
The CreateCrosshairWidget
function does the following:
- Gets the Player Controller (index 0).
- Creates a User Widget of a specified Widget Blueprint Class, with the player controller as the owning player.
- Stores the created widget in a variable (
WidgetBlueprint
). - Adds the widget to the viewport so it appears on the screen.
C++ Conversion
Header File (BotPlayer.h
)
// ...existing code... #include "Blueprint/UserWidget.h" UCLASS() class YOURGAME_API ABotPlayer : public APawn { GENERATED_BODY() public: ABotPlayer(); // The crosshair widget class to use (set this in the editor) UPROPERTY(EditAnywhere, BlueprintReadWrite, Category="UI") TSubclassOf<UUserWidget> WidgetBlueprintClass; // The created widget instance UPROPERTY() UUserWidget* WidgetBlueprint; // Function to create the crosshair widget void CreateCrosshairWidget();
Source File (BotPlayer.cpp
)
#include "BotPlayer.h" #include "Blueprint/UserWidget.h" #include "Kismet/GameplayStatics.h" void ABotPlayer::CreateCrosshairWidget() { // Get the player controller APlayerController* PC = UGameplayStatics::GetPlayerController(GetWorld(), 0); if (PC && WidgetBlueprintClass) { // Create the widget WidgetBlueprint = CreateWidget<UUserWidget>(PC, WidgetBlueprintClass); if (WidgetBlueprint) { // Add it to the viewport WidgetBlueprint->AddToViewport(); } } }
Summary:
- The function gets the player controller, creates the widget, stores it in a variable, and adds it to the viewport—just like the Blueprint version.
- Make sure to set
WidgetBlueprintClass
in the editor to your crosshair widget Blueprint.
Top comments (0)