My First Function

I’ve refactored the code so it’s cleaner, placing the existing code in to functions that do a specific task.

My header file:

// Fill out your copyright notice in the Description page of Project Settings.

#pragma once

#include "CoreMinimal.h"
#include "Console/Cartridge.h"
#include "BullCowCartridge.generated.h"

UCLASS(ClassGroup=(Custom), meta=(BlueprintSpawnableComponent))
class BULLCOWGAME_API UBullCowCartridge : public UCartridge
{
	GENERATED_BODY()

	public:
	virtual void BeginPlay() override;
	virtual void OnInput(const FString& Input) override;

	private:
	void InitGame();
	void ProcessInput(const FString& Input);
	void Welcome();

	// Your declarations go below!
	private:
	FString HiddenWord;
	int32 Lives;
	
};

My CPP file:

// Fill out your copyright notice in the Description page of Project Settings.
#include "BullCowCartridge.h"

void UBullCowCartridge::BeginPlay() // When the game starts
{
    Super::BeginPlay();
    InitGame();
    Welcome();    
}

void UBullCowCartridge::Welcome()
{
    PrintLine(TEXT("Welcome to Bulls & Cows"));
    PrintLine(TEXT("Enter input and press enter"));
}

void UBullCowCartridge::InitGame()
{
    HiddenWord = TEXT("keyboard");
    Lives = 3;
}

void UBullCowCartridge::OnInput(const FString& Input) // When the player hits enter
{
    ClearScreen();
    ProcessInput(Input);    
}

void UBullCowCartridge::ProcessInput(const FString& Input)
{
    if (Input == HiddenWord)
    {
        PrintLine(TEXT("Congratulations you found the word!"));
    }
    else
    {
        PrintLine(TEXT("That's not the hidden word, guess again."));
    }
}

Very good for your first function!

Privacy & Terms