Hello all,
I’m new to coding and am attempting the first challenge of Lesson 60 ‘Booleans’. The challenge was to:
- create an EndGame() function we can call
- Get it to set bGameOver to true and instruct the player to press enter to continue
- Implement an if statement checking if the game is over or not and run our existing code if not
- Get the game to welcome the player again
The code I ended up with keeps crashing my Unreal Engine and I’ve watched the video over again up until this point just for clarification. I don’t want to continue past this point in the video though until I understand what I’m doing wrong and why it’s wrong.
Here is my code:
// 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(); // Setting Up Game
}
void UBullCowCartridge::OnInput(const FString& Input) // When the player hits enter
{
ClearScreen();
if(Input == HiddenWord)
{
PrintLine(TEXT("You Win!"));
bGameOver = true;
}
else
{
if(Input.Len() != HiddenWord.Len())
{
PrintLine(TEXT("The hidden word is %i characters long"), HiddenWord.Len());
}
PrintLine(TEXT("You have lost!"));
bGameOver = true;
}
}
void UBullCowCartridge::InItGame()
{
HiddenWord = TEXT("canker"); // Set the HiddenWord
Lives = 5; // Set Lives
// PrintLine((TEXT("The Hidden Word is: %s"), *HiddenWord));
// PrintLine(FString::Printf(TEXT("The Hidden Word is: %s"), *HiddenWord)); //Debug Line
PrintLine(TEXT("Welcome to the Bull Cow Game!")); // Welcome Player
PrintLine(TEXT("Please guess a %i letter word:"), HiddenWord.Len()); // Prompt for guess
bGameOver = false;
}
void UBullCowCartridge::EndGame() // When the game ends
{
if(true)
{
PrintLine(TEXT("Game over. Press enter to continue."));
BeginPlay();
}
else {
InItGame();
}
}
// 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;
void InItGame();
void EndGame();
// Your declarations go below!
private:
FString HiddenWord;
int32 Lives;
bool bGameOver;
};